def test_enumerate(): ''' Check to ensure the enumeration of a file or string is exaclty the length of lines for the supplied. ''' CDP_DATA = '''Device ID: SEP001F9EAB59F1 Entry address(es): IP address: 10.243.14.48 Platform: Cisco IP Phone 7941, Capabilities: Host Phone Interface: FastEthernet0/15, Port ID (outgoing port): Port 1 Holdtime : 124 sec''' plog = Plog(CDP_DATA) plog._c = 0 def counts(line, line_no): plog._c = plog._c + 1 plog.run(counts) assert plog._c == 6
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: if block.valid(): print block.as_dict()
# 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 pprint import pprint from api import Plog from patterns import PlogLine, PlogBlock from blocks import CDPBlock f = open('test_data2.txt', 'r') plog = Plog(f, whitespace='|', terminator=',') # import pdb; pdb.set_trace() plog.add_blocks(CDPBlock) plog.run() for block in plog.data_blocks: if block.valid(): d = block.as_dict() print d import pdb pdb.set_trace()