def parse_line(self, line, *args, **kwargs): ''' method is passed to the run and receives a single line string to parse and apply PlogLine and PlogBlock ''' # Make a plog line. pline = PlogLine(line, line_no=kwargs.get('line_no', -1)) self.line_count += 1 # Find header_line blocks matching this pline blocks, is_header = self.get_blocks_with_header_footer(pline) # one or more blocks detected pr = '' for block in blocks: if block.is_open is not True and is_header: block.open() bl = PlogBlock(ref=block) bl.header = block.header bl.footer = block.footer bl.lines = block.lines bl.line_refs = block.line_refs bl.open_lines = block.open_lines self.open_blocks[block] = bl pr = '#%s+' % colored(pline.line_no, 'grey') else: if block in self.open_blocks and is_header is not True: self.open_blocks[block].add_data(pline) data_blocks = self.close_data_blocks(block) pr = colored('-', 'red') s = '~%s#%s' % ( colored(len(data_blocks), 'grey'), colored(pline.line_no, 'grey'), ) # sys.stdout.write(s) for block in self.open_blocks: if block.is_open: added = self.open_blocks[block].add_data(pline) v = colored('_', 'grey') if added is not True: if pline.value != '': v = colored('_', 'red') self.open_blocks[block].add_missed(pline) else: v = '' pr = '%s%s' % (pr, v) bl = len(blocks) if bl > 0: ct = ( colored('[', 'grey'), colored('%s' % bl, 'white'), colored(']', 'grey'), ) d = "%s%s%s" % ct
from api import Plog from patterns import PlogLine, PlogBlock block = PlogBlock('Device ID:', ref='Device') block.header.ref = 'device_id' block.footer = PlogLine('----------', ref='footer').anything() lines = {} lines['entry_address'] = PlogLine('IP address:') lines['platform'] = PlogLine('Platform:') lines['interface'] = PlogLine('Interface:') lines['hold_time'] = PlogLine('Holdtime').maybe(' ').then(':') lines['version'] = PlogLine('Version').maybe(' ').then(':').multiline() lines['version'] = PlogLine('advertisement version:') lines['duplex'] = PlogLine('Duplex:') lines['power_drawn'] = PlogLine('Power drawn:') lines['power_request_id'] = PlogLine('Power request id:') lines['power_management_id'] = PlogLine('Power management id:') lines['power_request_levels'] = PlogLine('Power request levels are:') block.add_lines(**lines) # new parser f = open('test_data2.txt', 'r') # plog = Plog(f, whitespace='|') plog = Plog(f, whitespace='|', terminator=',') # run it plog.add_block(block) blocks = plog.run() for block in blocks:
from api import Plog from patterns import PlogLine, PlogBlock # block = PlogBlock('Device ID:', ref='Device') block.header.ref='device_id' block.footer = PlogLine('----------', ref='footer').anything() lines = {} lines['entry_address'] = PlogLine('IP address:') lines['platform'] = PlogLine('Platform:') lines['interface'] = PlogLine('Interface:') lines['hold_time'] = PlogLine('Holdtime').maybe(' ').then(':') lines['version'] = PlogLine('Version').maybe(' ').then(':').multiline() lines['ad_version'] = PlogLine('advertisement version:') block.add_lines(**lines) # new parser f = open('test_data2.txt', 'r') plog = Plog(f, whitespace='|') # run it plog.add_block(block) blocks = plog.run() for block in blocks: if block.valid(): print block.as_dict()