Class: TextFSM::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/textfsm/parser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, options: {}) ⇒ Parser

Returns a new instance of Parser.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/textfsm/parser.rb', line 16

def initialize(template, options: {})
  @states = {}
  @fields = {}
  @option_types = Options::BUILTINS.merge(options.transform_keys(&:to_s)).freeze
  @option_types.each_value do |type|
    raise ArgumentError, "options must inherit from TextFSM::Options::Base" unless type.is_a?(Class) && type < Options::Base
  end
  parse_template(read_template(template))
  @fields.freeze
  @states.each_value(&:freeze)
  @states.freeze
  @output_fields = @fields.each_value.select(&:visible?).freeze
  @header = @output_fields.map(&:name).freeze
  reset
end

Instance Attribute Details

#current_state ⇒ Object (readonly)

Returns the value of attribute current_state.



14
15
16
# File 'lib/textfsm/parser.rb', line 14

def current_state
  @current_state
end

#header ⇒ Object (readonly)

Returns the value of attribute header.



14
15
16
# File 'lib/textfsm/parser.rb', line 14

def header
  @header
end

#states ⇒ Object (readonly)

Returns the value of attribute states.



14
15
16
# File 'lib/textfsm/parser.rb', line 14

def states
  @states
end

Class Method Details

.from_file(path, **options) ⇒ Object



32
33
34
# File 'lib/textfsm/parser.rb', line 32

def self.from_file(path, **options)
  new(File.read(path, encoding: "UTF-8"), **options)
end

Instance Method Details

#fields_with_option(name) ⇒ Object

Raises:

  • (ArgumentError)


71
72
73
74
75
76
# File 'lib/textfsm/parser.rb', line 71

def fields_with_option(name)
  name = name.to_s
  raise ArgumentError, "Unknown option #{name.inspect}" unless @option_types.key?(name)

  @fields.each_value.filter_map { |field| field.name if field.option?(name) }
end

#fill_up(field) ⇒ Object

Option callbacks use the same visible-column mapping as record output.



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/textfsm/parser.rb', line 79

def fill_up(field)
  column = @output_fields.index(field)
  return unless column

  @rows.reverse_each do |row|
    value = row[column]
    break if value && !(value.respond_to?(:empty?) && value.empty?)

    row[column] = Data.copy(field.value)
  end
end

#parse(text, eof: true) ⇒ Object

Calls accumulate state until reset. Chunks must end on a line boundary; eof: false defers the implicit final Record. Results are frozen snapshots.



61
62
63
64
# File 'lib/textfsm/parser.rb', line 61

def parse(text, eof: true)
  consume_input(text, eof: eof)
  rows
end

#parse_hashes(text, eof: true) ⇒ Object



66
67
68
69
# File 'lib/textfsm/parser.rb', line 66

def parse_hashes(text, eof: true)
  consume_input(text, eof: eof)
  to_hashes
end

#reset ⇒ Object



52
53
54
55
56
57
# File 'lib/textfsm/parser.rb', line 52

def reset
  @current_state = "Start"
  @rows = []
  @fields.each_value(&:reset)
  self
end

#rows ⇒ Object



40
41
42
# File 'lib/textfsm/parser.rb', line 40

def rows
  Data.copy(@rows, immutable: true)
end

#state_names ⇒ Object



36
37
38
# File 'lib/textfsm/parser.rb', line 36

def state_names
  @states.keys
end

#to_a ⇒ Object



44
45
46
# File 'lib/textfsm/parser.rb', line 44

def to_a
  Data.copy(@rows)
end

#to_hashes ⇒ Object



48
49
50
# File 'lib/textfsm/parser.rb', line 48

def to_hashes
  to_a.map { |row| @header.zip(row).to_h }
end

#to_s ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/textfsm/parser.rb', line 91

def to_s
  output = "#{@fields.values.join("\n")}\n"
  @states.each do |state, rules|
    output << "\n#{state}\n"
    rules.each do |rule|
      output << "#{rule}\n"
    end
  end
  output
end