def _parse_progression(progression_str): items = funcy.re_all(r'\[([^\]]+)\]', progression_str) ret = [] for item in items: tokens = item.split(':') ret.append({'chord': tokens[0], 'duration': []}) if len(tokens) == 1: ret[-1]['duration'].append({'number': 1, 'unit': MEASURE}) else: for number, unit in funcy.re_all(r'([\d\.]+)([{0}{1}{2}])'.format(MEASURE, BEAT, HALFBEAT), tokens[1]): ret[-1]['duration'].append({'number': int(number), 'unit': unit}) return ret
def get_groups(ip): try: child = telnet(ip) rslt = do_some(child, 'disp cu config | in link-aggregation') close(child) except (pexpect.EOF, pexpect.TIMEOUT) as e: return ('fail', None, ip) temp = re_all(r'(group\s+\d+)\s+mode\s+(\w+)', rslt) temp1 = dict(re_all(r'(group\s+\d+)\s+description\s+(\S+ *\S*)', rslt)) rslt1 = [dict(name=x[0], mode=x[1], desc=temp1.get(x[0], None)) for x in temp] rslt3 = [update_in(x, ['mode'], lambda y: 'yes' if y == 'static' else y) for x in rslt1] return ('success', rslt3, ip)
def get_infs(ip): def _get_info(child, inf): rslt = do_some(child, 'show int {inf}'.format(inf=inf)) desc = re_find(r'Description\sis\s(\S+)', rslt) state = re_find(r'{inf}\sis\s(\S+\s?\S+),'.format(inf=inf), rslt) bw = re_find(r'BW\s(\d+)\sKbits', rslt) bw = int(bw or 0) / 1000 inTraffic = re_find(r'seconds\sinput\srate\s?:\s+(\d+)\sBps', rslt) inTraffic = int(inTraffic or 0) * 8 / 1e6 outTraffic = re_find(r'seconds\soutput\srate:\s+(\d+)\sBps', rslt) outTraffic = int(outTraffic or 0) * 8 / 1e6 return dict(name=inf, desc=desc, state=state, bw=bw, inTraffic=inTraffic, outTraffic=outTraffic) try: child = telnet(ip) rslt = do_some(child, 'show run | in interface', timeout=180) rslt = re_all(r'interface\s+(x?gei_\d+/\d+/\d+)', rslt) infs = lmap(partial(_get_info, child), rslt) close(child) except (pexpect.EOF, pexpect.TIMEOUT) as e: return ('fail', None, ip) return ('success', infs, ip)
def _get_inf(child, board): slot, boardName = board rslt = do_some(child, 'disp board 0/{slot}'.format(slot=slot)) rslt = [ re_find(r'(\d+).*-\s+(?:auto_)?(\d+)', x) for x in rslt.split('\r\n') if 'online' in x ] if boardName.lower() == 'gic': boardName = 'giu' child.sendline('conf') child.expect(prompter) child.sendline('interface {boardName} 0/{slot}'.format( boardName=boardName, slot=slot)) child.expect(prompter) temp = [] for x, y in rslt: traffic = do_some(child, 'disp port traffic {port}'.format(port=x)) inTraffic, outTraffic = re_all(r'\(octets/s\)\s+=(\d+)', traffic) inTraffic = int(inTraffic) * 8 / 1e6 outTraffic = int(outTraffic) * 8 / 1e6 bw = int(y or 0) temp.append( dict( name='0/{slot}/{port}'.format(slot=slot, port=x), desc='cannot set', bw=bw, state='up', inTraffic=inTraffic, outTraffic=outTraffic)) child.sendline('quit') child.expect(prompter) child.sendline('quit') child.expect(prompter) return temp
def get_vlans_a(ip): try: child = telnet(ip) rslt = do_some( child, 'disp cu | in (port trunk allow|port hybrid tagged)') close(child) except (pexpect.EOF, pexpect.TIMEOUT) as e: return ('fail', None, ip) vlan_sgmt = re_all(r'(\d+) to (\d+)', rslt) vlans = re_all(r'(\d+)', re.sub(r'\d+ to \d+', '', rslt)) vlan_sgmt = [(int(x[0]), int(x[1]) + 1) for x in vlan_sgmt] vlans = [(int(x), int(x) + 1) for x in vlans if x != '1'] vlan_sgmt = lconcat(vlan_sgmt, vlans) _vlan = lambda s, e: range(s, e) vlans = reduce(lambda x, y: x | set(_vlan(*y)), vlan_sgmt, set()) return ('success', list(vlans), ip)
def _get_inf(child, board): slot, boardName = board rslt = do_some(child, 'disp board 0/{slot}'.format(slot=slot)) rslt = [re_find(r'(\d+).*-\s+(?:auto_)?(\d+)', x) for x in rslt.split('\r\n') if 'online' in x] if boardName.lower() == 'gic': boardName = 'giu' child.sendline('conf') child.expect(prompter) child.sendline( 'interface {boardName} 0/{slot}'.format(boardName=boardName, slot=slot)) child.expect(prompter) temp = [] for x, y in rslt: traffic = do_some(child, 'disp port traffic {port}'.format(port=x)) inTraffic, outTraffic = re_all(r'\(octets/s\)\s+=(\d+)', traffic) inTraffic = int(inTraffic) * 8 / 1e6 outTraffic = int(outTraffic) * 8 / 1e6 bw = int(y or 0) temp.append(dict(name='0/{slot}/{port}'.format(slot=slot, port=x), desc='cannot set', bw=bw, state='up', inTraffic=inTraffic, outTraffic=outTraffic)) child.sendline('quit') child.expect(prompter) child.sendline('quit') child.expect(prompter) return temp
def get_annotations(case_query, control_query, modifier_query=""): # Fetch all relevant data queries = [case_query, control_query, modifier_query] tokens = set(cat(re_all('[a-zA-Z]\w*', query) for query in queries)) df = query_tags_annotations(tokens) # Make tag columns df.tag_name = df.tag_name.str.lower() df.annotation = df.annotation.str.lower() for tag in tokens: tag_name = tag.lower() df[tag_name] = df[df.tag_name == tag_name].annotation # Select only cells with filled annotations df = df.drop(['tag_name', 'annotation'], axis=1) df = df.groupby(['sample_id', 'series_id', 'platform_id', 'gsm_name', 'gpl_name'], as_index=False).first() df = df.convert_objects(convert_numeric=True) # Apply case/control/modifier if modifier_query: df = df.query(modifier_query.lower()) case_df = df.query(case_query.lower()) control_df = df.query(control_query.lower()) # Set 0 and 1 for analysis overlap_df = df.ix[set(case_df.index).intersection(set(control_df.index))] df['sample_class'] = None df['sample_class'].ix[case_df.index] = 1 df['sample_class'].ix[control_df.index] = 0 df['sample_class'].ix[overlap_df.index] = -1 return df.dropna(subset=["sample_class"])
def get_groups(ip): try: child = telnet(ip) rslt = do_some(child, 'disp cu config | in link-aggregation') close(child) except (pexpect.EOF, pexpect.TIMEOUT) as e: return ('fail', None, ip) temp = re_all(r'(group\s+\d+)\s+mode\s+(\w+)', rslt) temp1 = dict(re_all(r'(group\s+\d+)\s+description\s+(\S+ *\S*)', rslt)) rslt1 = [ dict(name=x[0], mode=x[1], desc=temp1.get(x[0], None)) for x in temp ] rslt3 = [ update_in(x, ['mode'], lambda y: 'yes' if y == 'static' else y) for x in rslt1 ] return ('success', rslt3, ip)
def get_analysis_df(case_query, control_query, modifier_query=""): # Fetch all relevant data queries = [case_query, control_query, modifier_query] tokens = set(cat(re_all('[a-zA-Z]\w*', query) for query in queries)) df = pd.read_sql_query( ''' SELECT sample_id, sample.gsm_name, annotation, series_annotation.series_id, series.gse_name, series_annotation.platform_id, platform.gpl_name, tag.tag_name FROM sample_annotation JOIN sample ON (sample_annotation.sample_id = sample.id) JOIN series_annotation ON (sample_annotation.serie_annotation_id = series_annotation.id) JOIN platform ON (series_annotation.platform_id = platform.id) JOIN tag ON (series_annotation.tag_id = tag.id) JOIN series ON (series_annotation.series_id = series.id) WHERE tag.tag_name ~* %(tags)s ''', conn, params={'tags': '^(%s)$' % '|'.join(map(re.escape, tokens))}) # Make tag columns df.tag_name = df.tag_name.str.lower() df.annotation = df.annotation.str.lower() for tag in tokens: tag_name = tag.lower() df[tag_name] = df[df.tag_name == tag_name].annotation # Select only cells with filled annotations df = df.drop(['tag_name', 'annotation'], axis=1) df = df.groupby( ['sample_id', 'series_id', 'platform_id', 'gsm_name', 'gpl_name'], as_index=False).first() df = df.convert_objects(convert_numeric=True) # Apply case/control/modifier if modifier_query: df = df.query(modifier_query.lower()) case_df = df.query(case_query.lower()) control_df = df.query(control_query.lower()) # Set 0 and 1 for analysis overlap_df = df.ix[set(case_df.index).intersection(set(control_df.index))] df['sample_class'] = None df['sample_class'].ix[case_df.index] = 1 df['sample_class'].ix[control_df.index] = 0 df['sample_class'].ix[overlap_df.index] = -1 return df.dropna(subset=["sample_class"])
def get_power_info(ip): try: child = telnet(ip) rslt = do_some(child, 'disp power') close(child) except (pexpect.EOF, pexpect.TIMEOUT) as e: return ('fail', None, ip) temp = re_all(r'PWR\d\s+YES\s+DC\s+Supply', rslt) return ('success', len(temp), ip)
def get_main_card(ip): try: child = telnet(ip) rslt = do_some(child, 'show version') close(child) except (pexpect.EOF, pexpect.TIMEOUT) as e: return ('fail', None, ip) temp = re_all(r'MEC\d,\spanel\s\d,\s(?:slave|master)', rslt) return ('success', len(temp), ip)
def get_power_info(ip): try: child = telnet(ip) rslt = do_some(child, 'show powerfanstate') close(child) except (pexpect.EOF, pexpect.TIMEOUT) as e: return ('fail', None, ip) temp = re_all(r'Power\d\s+:\sNormal Work', rslt) return ('success', len(temp), ip)
def get_main_card(ip): try: child = telnet(ip) rslt = do_some(child, 'disp device') close(child) except (pexpect.EOF, pexpect.TIMEOUT) as e: return ('fail', None, ip) temp = re_all(r'LSB\dSRP\dN\d\s+(?:Master|Slave)', rslt) return ('success', len(temp), ip)
def _get_group(child, group): rslt = do_some( child, 'disp link-aggregation {group}'.format(group=group)) desc = re_find(r'description:(\S+)', rslt) mode = re_find(r'work mode:\s+(\S+)', rslt) temp = re_all(r'(\d+/\d+)\s+(\d\S+)', rslt) temp1 = lmapcat(lambda x: ['{0}/{1}'.format(x[0], y) for y in x[1].split(',')], temp) return dict(name=group, desc=desc, mode=mode, infs=temp1)
def get_main_card(ip): try: child = telnet(ip) rslt = do_some(child, 'show version') close(child) except (pexpect.EOF, pexpect.TIMEOUT) as e: return ('fail', None, ip) temp = re_all(r'MEC,\spanel\s\d,\s(?:slave|master)', rslt) return ('success', len(temp), ip)
def get_main_card(ip): try: child = telnet(ip) rslt = do_some(child, 'display board 0') close(child) except (pexpect.EOF, pexpect.TIMEOUT): return ('fail', None, ip) cards = re_all(r'(SCUL|SCUN)\s+(?:Standby_normal|Active_normal)', rslt) return ('success', len(cards), ip)
def get_main_card(ip): try: child = telnet(ip) rslt = do_some(child, 'display board 0') close(child) except (pexpect.EOF, pexpect.TIMEOUT) as e: return ('fail', None, ip) cards = re_all(r'(SCUL|SCUN)\s+(?:Standby_normal|Active_normal)', rslt) return ('success', len(cards), ip)
def get_main_card(ip): try: child = telnet(ip) rslt = do_some(child, 'show board-info') close(child) except (pexpect.EOF, pexpect.TIMEOUT) as e: return ('fail', None, ip) temp = re_all(r'MP\((?:M|S)\)\s+\d\s+\d\s+on', rslt) return ('success', len(temp), ip)
def get_analysis_df(case_query, control_query, modifier_query=""): # Fetch all relevant data queries = [case_query, control_query, modifier_query] tokens = set(cat(re_all("[a-zA-Z]\w*", query) for query in queries)) df = pd.read_sql_query( """ SELECT sample_id, sample.gsm_name, annotation, series_annotation.series_id, series.gse_name, series_annotation.platform_id, platform.gpl_name, tag.tag_name FROM sample_annotation JOIN sample ON (sample_annotation.sample_id = sample.id) JOIN series_annotation ON (sample_annotation.serie_annotation_id = series_annotation.id) JOIN platform ON (series_annotation.platform_id = platform.id) JOIN tag ON (series_annotation.tag_id = tag.id) JOIN series ON (series_annotation.series_id = series.id) WHERE tag.tag_name ~* %(tags)s """, conn, params={"tags": "^(%s)$" % "|".join(map(re.escape, tokens))}, ) # Make tag columns df.tag_name = df.tag_name.str.lower() df.annotation = df.annotation.str.lower() for tag in tokens: tag_name = tag.lower() df[tag_name] = df[df.tag_name == tag_name].annotation # Select only cells with filled annotations df = df.drop(["tag_name", "annotation"], axis=1) df = df.groupby(["sample_id", "series_id", "platform_id", "gsm_name", "gpl_name"], as_index=False).first() df = df.convert_objects(convert_numeric=True) # Apply case/control/modifier if modifier_query: df = df.query(modifier_query.lower()) case_df = df.query(case_query.lower()) control_df = df.query(control_query.lower()) # Set 0 and 1 for analysis overlap_df = df.ix[set(case_df.index).intersection(set(control_df.index))] df["sample_class"] = None df["sample_class"].ix[case_df.index] = 1 df["sample_class"].ix[control_df.index] = 0 df["sample_class"].ix[overlap_df.index] = -1 return df.dropna(subset=["sample_class"])
def get_epba_card(ip): try: child = telnet(ip) rslt = do_some(child, 'display board 0') close(child) except (pexpect.EOF, pexpect.TIMEOUT): return ('fail', None, ip) cards = re_all(r'(\d{1,2})\s+\w+EPBA\s+Normal', rslt) return ('success', cards, ip)
def get_power_info(ip): try: child = telnet(ip) rslt = do_some(child, 'disp power') close(child) except (pexpect.EOF, pexpect.TIMEOUT) as e: return ('fail', None, ip) temp = re_all(r'Power\s+\d\sState:\sNormal', rslt) return ('success', len(temp), ip)
def get_main_card(ip): try: child = telnet(ip) rslt = do_some(child, 'show card') close(child) cards = re_all( r'\d\s+\d\s+\d{1,2}\s+(SCXM|GCSA).*(?:INSERVICE|STANDBY)', rslt) except (pexpect.EOF, pexpect.TIMEOUT) as e: return('fail', None, ip) return ('success', len(cards), ip)
def get_vlans(ip): try: child = telnet(ip) rslt = do_some(child, 'disp vlan | in common') close(child) vlans = re_all(r'(\d+)\s+common +\S+', rslt) vlans = [int(x) for x in vlans if x != '1'] except (pexpect.EOF, pexpect.TIMEOUT) as e: return ('fail', None, ip) return ('success', vlans, ip)
def get_main_card(ip): try: child = telnet(ip) rslt = do_some(child, 'disp device') close(child) except (pexpect.EOF, pexpect.TIMEOUT) as e: return ('fail', None, ip) temp = re_all( r'(?:SRU|MCU)[A-Z]\s+Present\s+PowerOn\s+Registered\s+Normal\s+(?:Master|Slave)', rslt) return ('success', len(temp), ip)
def _get_group(child, group): rslt = do_some( child, 'disp link-aggregation {group}'.format(group=group)) desc = re_find(r'description:(\S+)', rslt) mode = re_find(r'work mode:\s+(\S+)', rslt) temp = re_all(r'(\d+/\d+)\s+(\d\S+)', rslt) temp1 = lmapcat( lambda x: ['{0}/{1}'.format(x[0], y) for y in x[1].split(',')], temp) return dict(name=group, desc=desc, mode=mode, infs=temp1)
def get_main_card(ip): try: child = telnet(ip) rslt = do_some(child, 'show card') close(child) cards = re_all( r'\d\s+\d\s+\d{1,2}\s+(SCXM|GCSA).*(?:INSERVICE|STANDBY)', rslt) except (pexpect.EOF, pexpect.TIMEOUT) as e: return ('fail', None, ip) return ('success', len(cards), ip)
def get_vlans(ip): try: child = telnet(ip) rslt = do_some(child, 'show vlan | in vlan') close(child) vlans = re_all(r'(\d+)\s+vlan\d{4} +\S+', rslt) if '1' in vlans: vlans.remove('1') vlans = [int(x) for x in vlans] except (pexpect.EOF, pexpect.TIMEOUT) as e: return ('fail', None, ip) return ('success', vlans, ip)
def save(self, **kwargs): # Only set specie when it's non-controversial taxid = ldistinct( keep(self.attrs.get, ['platform_taxid', 'sample_taxid'])) if len(taxid) == 1: self.specie = SPECIES.get(taxid[0]) else: self.specie = '' self.platforms = re_all(r'GPL\d+', self.attrs['platform_id']) self.samples_count = len(self.attrs['sample_id'].split()) super(Series, self).save(**kwargs)
def get_ip_pool(ip): def _get_sections(child, name): rslt = do_some(child, 'disp cu configuration ip-pool {name}'.format(name=name)) sections = re_all(r'section \d+ (\S+) (\S+)', rslt) return sections try: child = telnet(ip) rslt = do_some(child, 'disp domain 163.js | in pool-name') poolNames = re_all(r'pool-name\s+:\s(\S+)', rslt) ips = lmapcat(partial(_get_sections, child), poolNames) close(child) except(pexpect.EOF, pexpect.TIMEOUT) as e: return ('fail', None, ip) return ('success', ips, ip)
def get_vlan_users(ip, inf): def _get_users(child, i): rslt = do_some(child, 'show subscriber interface {i} | in external-vlan'.format(i=i)) vlans = re_all(r'external-vlan\s+:(\d+)', rslt) return vlans try: child = telnet(ip) rslt = do_some(child, 'show running-config | in smartgroup{inf}\.'.format(inf=inf)) infs = distinct(re_all(r'(smartgroup\S+)', rslt)) vlans = lmapcat(partial(_get_users, child), infs) close(child) vlans = count_by(int, vlans) except (pexpect.EOF, pexpect.TIMEOUT) as e: return ('fail', None, ip) return ('success', vlans, ip)
def get_vlan_users(ip, inf): def _get_users(child, i): rslt = do_some(child, 'disp access-user interface {i} | in /'.format(i=i)) users = re_all(r'(\d+)/', rslt) return users try: child = telnet(ip) infs = do_some(child, 'disp cu interface | in Eth-Trunk{inf}\.'.format(inf=inf)) infs = re_all(r'interface (\S+)', infs) rslt = lmapcat(partial(_get_users, child), infs) close(child) rslt = count_by(int, rslt) except (pexpect.EOF, pexpect.TIMEOUT) as e: return ('fail', None, ip) return ('success', rslt, ip)
def get_ip_pool(ip): def _get_sections(child, name): rslt = do_some( child, 'disp cu configuration ip-pool {name}'.format(name=name)) sections = re_all(r'section \d+ (\S+) (\S+)', rslt) return sections try: child = telnet(ip) rslt = do_some(child, 'disp domain 163.js | in pool-name') poolNames = re_all(r'pool-name\s+:\s(\S+)', rslt) ips = lmapcat(partial(_get_sections, child), poolNames) close(child) except (pexpect.EOF, pexpect.TIMEOUT) as e: return ('fail', None, ip) return ('success', ips, ip)
def get_vlans(ip): def _vlan(v): if '-' in v: s, e = [int(x) for x in v.split('-')] else: s = e = int(v) return range(s, e + 1) try: child = telnet(ip) rslt = do_some(child, 'show run | in (hybrid|trunk) vlan') close(child) vlans = re_all(r'vlan\s(\d+(?:-\d+)?)', rslt) vlans = reduce(lambda x, y: x | set(_vlan(y)), vlans, set()) except (pexpect.EOF, pexpect.TIMEOUT) as e: return ('fail', None, ip) return ('success', list(vlans), ip)
def get_analysis_df(analysis): # Fetch all relevant data queries = [ analysis.case_query, analysis.control_query, analysis.modifier_query ] tokens = set(cat(re_all(r'[a-zA-Z]\w*', query) for query in queries)) tags = Tag.objects.filter(tag_name__iregex='^(%s)$' % '|'.join(map(re.escape, tokens))) qs = SampleAnnotation.objects.filter(series_annotation__tag__in=tags) if analysis.specie: qs = qs.filter(series_annotation__series__specie=analysis.specie) df = qs.to_dataframe(list(COLUMNS.keys())).rename(columns=COLUMNS) # Make tag columns df.tag_name = df.tag_name.str.lower() df.annotation = df.annotation.str.lower() for tag in tags: tag_name = tag.tag_name.lower() df[tag_name] = df[df.tag_name == tag_name].annotation # Select only cells with filled annotations df = df.drop(['tag_name', 'annotation'], axis=1) df = df.groupby([ 'sample_id', 'series_id', 'platform_id', 'gsm_name', 'gse_name', 'gpl_name' ], as_index=False).first().fillna('') df = df.apply(pd.to_numeric, errors='ignore') # Apply case/control/modifier if analysis.modifier_query: df = df.query(analysis.modifier_query.lower()) case_df = df.query(analysis.case_query.lower()) control_df = df.query(analysis.control_query.lower()) # Set 0 and 1 for analysis overlap_df = df.ix[set(case_df.index).intersection(set(control_df.index))] df['sample_class'] = None df.loc[case_df.index, 'sample_class'] = 1 df.loc[control_df.index, 'sample_class'] = 0 df.loc[overlap_df.index, 'sample_class'] = -1 return df.dropna(subset=["sample_class"])
def get_infs(ip): def _get_inf(child, board): slot, boardName = board rslt = do_some(child, 'disp board 0/{slot}'.format(slot=slot)) rslt = [ re_find(r'(\d+).*-\s+(?:auto_)?(\d+)', x) for x in rslt.split('\r\n') if 'online' in x ] if boardName.lower() == 'gic': boardName = 'giu' child.sendline('conf') child.expect(prompter) child.sendline('interface {boardName} 0/{slot}'.format( boardName=boardName, slot=slot)) child.expect(prompter) temp = [] for x, y in rslt: traffic = do_some(child, 'disp port traffic {port}'.format(port=x)) inTraffic, outTraffic = re_all(r'\(octets/s\)\s+=(\d+)', traffic) inTraffic = int(inTraffic) * 8 / 1e6 outTraffic = int(outTraffic) * 8 / 1e6 bw = int(y or 0) temp.append( dict( name='0/{slot}/{port}'.format(slot=slot, port=x), desc='cannot set', bw=bw, state='up', inTraffic=inTraffic, outTraffic=outTraffic)) child.sendline('quit') child.expect(prompter) child.sendline('quit') child.expect(prompter) return temp try: child = telnet(ip) rslt = do_some(child, 'disp board 0') boards = re_all(r'(\d+)\s+\w+(eth|gic)\w+\s+normal', rslt, flags=re.I) infs = lmapcat(partial(_get_inf, child), boards) close(child) except (pexpect.EOF, pexpect.TIMEOUT): return ('fail', None, ip) return ('success', infs, ip)
def get_vlan_users(ip, inf): def _get_users(child, i): rslt = do_some(child, 'disp access-user interface {i} | in /'.format(i=i)) users = re_all(r'(\d+)/', rslt) return users try: child = telnet(ip) infs = do_some( child, 'disp cu interface | in Eth-Trunk{inf}\.'.format(inf=inf)) infs = re_all(r'interface (\S+)', infs) rslt = lmapcat(partial(_get_users, child), infs) close(child) rslt = count_by(int, rslt) except (pexpect.EOF, pexpect.TIMEOUT) as e: return ('fail', None, ip) return ('success', rslt, ip)
def get_groups(ip): def _get_group(child, group): rslt = do_some( child, 'disp link-aggregation {group}'.format(group=group)) desc = re_find(r'description:(\S+)', rslt) mode = re_find(r'work mode:\s+(\S+)', rslt) temp = re_all(r'(\d+/\d+)\s+(\d\S+)', rslt) temp1 = lmapcat(lambda x: ['{0}/{1}'.format(x[0], y) for y in x[1].split(',')], temp) return dict(name=group, desc=desc, mode=mode, infs=temp1) try: child = telnet(ip) temp = re_all(r'(\d+/\d+/\d+)', do_some(child, 'disp link-aggregation all')) groups = lmap(partial(_get_group, child), temp) close(child) except (pexpect.EOF, pexpect.TIMEOUT) as e: return ('fail', None, ip) return ('success', groups, ip)
def get_groups(ip): def _get_group(child, group): rslt = do_some( child, 'disp link-aggregation {group}'.format(group=group)) desc = re_find(r'description:(\S+)', rslt) mode = re_find(r'work mode:\s+(\S+)', rslt) temp = re_all(r'(\d+/\d+)\s+(\d\S+)', rslt) temp1 = lmapcat( lambda x: ['{0}/{1}'.format(x[0], y) for y in x[1].split(',')], temp) return dict(name=group, desc=desc, mode=mode, infs=temp1) try: child = telnet(ip) temp = re_all(r'(\d+/\d+/\d+)', do_some(child, 'disp link-aggregation all')) groups = lmap(partial(_get_group, child), temp) close(child) except (pexpect.EOF, pexpect.TIMEOUT): return ('fail', None, ip) return ('success', groups, ip)
def get_infs(ip): def _get_desc(child, inf): name = inf['name'] rslt = do_some(child, 'show run interface {name}'.format(name=name)) desc = re_find(r'description\s(\S+ *\S*)', rslt) group = re_find(r'(smartgroup\s\d+)', rslt) if group is not None: group = group.replace(' ', '') inf['desc'] = desc inf['group'] = group return inf try: child = telnet(ip) rslt = do_some(child, 'show run | in interface (xg|g)ei_') temp = [dict(name=x) for x in re_all(r'interface\s(\S+)', rslt)] infs = lmap(partial(_get_desc, child), temp) close(child) except (pexpect.EOF, pexpect.TIMEOUT) as e: return ('fail', None, ip) return ('success', infs, ip)
def get_groups(ip): def _get_desc(child, group): name = group['name'] rslt = do_some(child, 'show run interface {name}'.format(name=name)) desc = re_find(r'description\s(\S+ *\S*)', rslt) mode = re_find(r'smartgroup mode (\S+)', rslt) if mode == '802.3ad': mode = 'yes' group['desc'] = desc group['mode'] = mode return group try: child = telnet(ip) rslt = do_some(child, 'show lacp internal') temp = re_all(r'Smartgroup:(\d+)', rslt) temp1 = [dict(name='smartgroup' + x) for x in temp] groups = lmap(partial(_get_desc, child), temp1) close(child) except (pexpect.EOF, pexpect.TIMEOUT) as e: return ('fail', None, ip) return ('success', groups, ip)
def get_infs(ip): def _get_inf(child, board): slot, boardName = board rslt = do_some(child, 'disp board 0/{slot}'.format(slot=slot)) rslt = [re_find(r'(\d+).*-\s+(?:auto_)?(\d+)', x) for x in rslt.split('\r\n') if 'online' in x] if boardName.lower() == 'gic': boardName = 'giu' child.sendline('conf') child.expect(prompter) child.sendline( 'interface {boardName} 0/{slot}'.format(boardName=boardName, slot=slot)) child.expect(prompter) temp = [] for x, y in rslt: traffic = do_some(child, 'disp port traffic {port}'.format(port=x)) inTraffic, outTraffic = re_all(r'\(octets/s\)\s+=(\d+)', traffic) inTraffic = int(inTraffic) * 8 / 1e6 outTraffic = int(outTraffic) * 8 / 1e6 bw = int(y or 0) temp.append(dict(name='0/{slot}/{port}'.format(slot=slot, port=x), desc='cannot set', bw=bw, state='up', inTraffic=inTraffic, outTraffic=outTraffic)) child.sendline('quit') child.expect(prompter) child.sendline('quit') child.expect(prompter) return temp try: child = telnet(ip) rslt = do_some(child, 'disp board 0') boards = re_all(r'(\d+)\s+\w+(eth|gic)\w+\s+normal', rslt, flags=re.I) infs = lmapcat(partial(_get_inf, child), boards) close(child) except (pexpect.EOF, pexpect.TIMEOUT) as e: return ('fail', None, ip) return ('success', infs, ip)
def get_infs(ip): def _get_desc(child, inf): name = inf['name'] rslt = do_some(child, 'show run interface {name}'.format(name=name)) desc = re_find(r'description\s(\S+ *\S*)', rslt) group = re_find(r'(smartgroup\s\d+)', rslt) if group: group = group.replace(' ', '') inf['desc'] = desc inf['group'] = group return inf try: child = telnet(ip) rslt = do_some(child, 'show run | in interface (xg|g|f)ei_') temp = [dict(name=x) for x in re_all('interface\s(\S+)', rslt)] get_desc = partial(_get_desc, child) infs = lmap(get_desc, temp) close(child) except (pexpect.EOF, pexpect.TIMEOUT) as e: return ('fail', None, ip) return ('success', infs, ip)
def get_groups(ip): def _get_desc(child, group): name = group['name'] rslt = do_some( child, 'show running-config-interface {name} all'.format(name=name)) desc = re_find(r'description\s(\S+ *\S*)', rslt) mode = re_find(r'lacp\smode\s(\S+)', rslt) if mode == '802.3ad': mode = 'yes' group['desc'] = desc group['mode'] = mode return group try: child = telnet(ip) rslt = do_some(child, 'show interface | in smartgroup') temp = [dict(name=x) for x in re_all(r'(smartgroup\d+)', rslt)] groups = lmap(partial(_get_desc, child), temp) close(child) except (pexpect.EOF, pexpect.TIMEOUT) as e: return ('fail', None, ip) return ('success', groups, ip)
def _get_sections(child, name): rslt = do_some( child, 'disp cu configuration ip-pool {name}'.format(name=name)) sections = re_all(r'section \d+ (\S+) (\S+)', rslt) return sections
def _get_users(child, i): rslt = do_some(child, 'disp access-user interface {i} | in /'.format(i=i)) users = re_all(r'(\d+)/', rslt) return users
def _get_infs(record): name = re_find(r'(Smartgroup:\d+)', record) if name: name = name.lower().replace(':', '') infs = re_all(r'(x?gei_\d+/\d+/\d+)\s?selected', record) return dict(name=name, infs=infs)
def get_port_onus(child, port): rslt = do_some(child, 'show run {port}'.format(port=port)) rslt1 = re_all(r'onu\s(\d+)\stype\sE8C[PG]24\sloid\s([A-F0-9]{16})', rslt) return (port, rslt1)
def _get_sections(child, name): rslt = do_some(child, 'disp cu configuration ip-pool {name}'.format(name=name)) sections = re_all(r'section \d+ (\S+) (\S+)', rslt) return sections
def _parse_chord(progression_substr): """ Parse a progression_substr for a chord directive, returning both the chord definition and the index where it ended .. doctests :: >>> _parse_chord('C#-') # doctest: +ELLIPSIS Traceback (most recent call last): ... ValueError: reached end of string... >>> parsed, end_i = _parse_chord('C#-]') >>> parsed['chord'] Chord(C#-) >>> len(parsed['duration']) 1 >>> parsed['duration'][0].count 1 >>> parsed['duration'][0].unit == constants.DURATION_UNIT_MEASURE True >>> end_i 3 >>> parsed, end_i = _parse_chord('rest]') >>> parsed['chord'] == constants.REST True >>> parsed, end_i = _parse_chord('riff]') >>> parsed['chord'] == constants.RIFF True >>> parsed, end_i = _parse_chord('?G:1m1b]') >>> parsed['optional'] True >>> len(parsed['duration']) 2 :param progression_substr: part of a progression string :rtype: tuple """ end_i = _find_i(progression_substr, CHORD_MARKUP['close_char']) tokens = progression_substr[:end_i].split(CHORD_MARKUP['separator']) chord_def = { 'chord': None, 'duration': [], 'optional': False } chord_content = tokens[0] if chord_content == 'rest': chord_def['chord'] = constants.REST elif chord_content == 'riff': chord_def['chord'] = constants.RIFF else: if chord_content.startswith('?'): chord_def['optional'] = True chord_content = chord_content[1:] chord_def['chord'] = models.Chord(chord_content) if len(tokens) == 1: chord_def['duration'].append(models.ChordDuration(1, constants.DURATION_UNIT_MEASURE)) else: for number, unit in funcy.re_all(r'([\d\.]+)([{0}{1}{2}])'.format( constants.DURATION_UNIT_MEASURE, constants.DURATION_UNIT_BEAT, constants.DURATION_UNIT_HALFBEAT ), tokens[1]): chord_def['duration'].append(models.ChordDuration(int(number), unit)) return chord_def, end_i
def _get_users(child, i): rslt = do_some( child, 'show subscriber interface {i} | in external-vlan'.format(i=i)) vlans = re_all(r'external-vlan\s+:(\d+)', rslt) return vlans