Esempio n. 1
0
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)
Esempio n. 2
0
def test_add_block():
    '''
    Add a basic empty block to the plog.blocks
    '''
    plog = Plog()
    block = PlogBlock()
    plog.add_block(block)
    assert len(plog.blocks) == 1
Esempio n. 3
0
    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
Esempio n. 4
0
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:
Esempio n. 5
0
def test_block_header_string_cast():
    '''Plog block is given a string, block.header should exist'''

    block = PlogBlock('Device')
    assert type(block.header) == PlogLine
Esempio n. 6
0
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
Esempio n. 7
0
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 as Line, PlogBlock as Block


block = Block('Device ID:', ref='Device')
block.header.ref='device_id'

block.add_lines(
    entry_address=Line('IP address:'),
    platform=Line('Platform:'),
    interface=Line('Interface:'),
    hold_time=Line('Holdtime').maybe(' ').then(':'),
    version=Line('Version').maybe(' ').then(':').multiline(),
    ad_version=Line('advertisement version:'),
    duplex=Line('Duplex:'),
    power_drawn=Line('Power drawn:'),
    power_request_id=Line('Power request id:'),
    power_management_id=Line('Power management id:'),
    power_request_levels=Line('Power request levels are:'),
)

block.footer = Line('----------', 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)
Esempio n. 9
0
from api import Plog
from patterns import PlogLine as Line, PlogBlock as Block


block = Block('Device ID:', ref='Device')
block.header.ref='device_id'

block.add_lines(
    entry_address=Line('IP address:'),
    platform=Line('Platform:'),
    interface=Line('Interface:'),
    hold_time=Line('Holdtime').maybe(' ').then(':'),
    version=Line('Version').maybe(' ').then(':').multiline(),
    ad_version=Line('advertisement version:'),
    duplex=Line('Duplex:'),
    power_drawn=Line('Power drawn:'),
    power_request_id=Line('Power request id:'),
    power_management_id=Line('Power management id:'),
    power_request_levels=Line('Power request levels are:'),
)

block.footer = Line('----------', 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)
Esempio n. 10
0
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()