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
def test_line(): '''Pass a string to the PlogLine, it should __eq__ a string correctly.''' string = 'Device' line = PlogLine(string) assert line == string
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:
def test_block_header(): ''' Add the first line to a block and check it's added as a header''' line = PlogLine('Device') block = PlogBlock(line) assert block.header == line
from api import Plog from patterns import PlogLine, PlogBlock f = open('test_data2.txt', 'r') # Capture something block = PlogBlock('Device ID:', ref='Device') block.header.ref = 'device_id' block.footer = PlogLine('----------', ref='footer').anything() # new parser plog = Plog(f, whitespace='|') # run it plog.add_block(block) blocks = plog.run() for block in blocks: if block.valid(): print block.as_dict()
class CDPBlock(object): '''Given as a 'block' through PlugBlockMixin.add_block. The add_block method looks for the `block` attribute and appends it to the running blocks. Lines represent each attribute to dicover within the _Start_ and _stop_ content of a block. Each `PlogLine` extracts an explicitly designed value, adding it to the 'lines' of a block. A block returns the line value as a dictionary attribute in `Plug.data_block`. ''' # Define a PlogBlock and its starting value. 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)
from api import Plog from patterns import PlogLine, PlogBlock f = open('test_data2.txt', 'r') # Capture something block = PlogBlock('Device ID:', ref='Device') block.header.ref = 'device_id' block.footer = PlogLine('----------', ref='footer').anything() ip_address = PlogLine('IP address:') block.add_lines(ip=ip_address) # new parser plog = Plog(f, whitespace='|') # run it plog.add_block(block) blocks = plog.run() for block in blocks: if block.valid(): print block.as_dict()
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()
from api import Plog from patterns import PlogLine, PlogBlock as Block block = Block('Device ID:', ref='Device') #block.header.ref='device_id' line = PlogLine(ref='ip') line.startswith('IP address:') block.add_line(line) block.footer = PlogLine('----------', ref='footer').anything() # 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: if block.valid(): print block.as_dict()