<commit_before>module Rubyipmi::Ipmitool
  class Lan < Rubyipmi::Ipmitool::BaseCommand
    attr_accessor :info
    attr_accessor :channel
    MAX_RETRY = 1

    def initialize(opts = ObservableHash.new)
      super("ipmitool", opts)
      @info = {}
      @channel = 2
    end

    # sets the info var to be empty causing the variable to repopulate upon the next call to info
    def refresh
      @info = {}
    end

    def channel=(num)
      refresh
      @channel = num
    end

    def info
      retrycount = 0
      if @info.length < 1
        begin
          parse(print)
        rescue
          # sometimes we need to get the info from channel 1,
          # wait for error to occur then retry using channel 1
          if retrycount < MAX_RETRY
            @channel = 1
            retry
          end
        end
      else
        # return the cached info
        @info
      end
    end

    def snmp
      info.fetch("snmp_community_string", nil)
    end

    def ip
      info.fetch("ip_address", nil)
    end

    def mac
      info.fetch("mac_address", nil)
    end

    def netmask
      info.fetch("subnet_mask", nil)
    end

    def gateway
      info.fetch("default_gateway_ip", nil)
    end

    def vlanid
      info.fetch("802.1q_vlan_id", nil)
    end

    #  def snmp=(community)
    #    @options["cmdargs"] = "lan set #{channel} snmp #{community}"
    #    value = runcmd
    #    @options.delete_notify("cmdargs")
    #    return value
    #  end

    def ip=(address)
      @options["cmdargs"] = "lan set #{channel} ipaddr #{address}"
      value = runcmd
      @options.delete_notify("cmdargs")
      value
    end

    def netmask=(mask)
      @options["cmdargs"] = "lan set #{channel} netmask #{mask}"
      value = runcmd
      @options.delete_notify("cmdargs")
      value
    end

    def gateway=(address)
      @options["cmdargs"] = "lan set #{channel} defgw ipaddr #{address}"
      value = runcmd
      @options.delete_notify("cmdargs")
      value
    end

    def dhcp?
      info.fetch("ip_address_source", nil).match(/dhcp/i) != nil
    end

    def static?
      info.fetch("ip_address_source", nil).match(/static/i) != nil
    end

    def vlanid=(vlan)
      @options["cmdargs"] = "lan set #{channel} vlan id #{vlan}"
      value = runcmd
      @options.delete_notify("cmdargs")
      value
    end

    private

    def print
      @options["cmdargs"] = "lan print"
      value = runcmd
      @options.delete_notify("cmdargs")
      @result if value
    end

    def parse(landata)
      landata.lines.each do |line|
        # clean up the data from spaces
        item = line.split(':', 2)
        key = normalize(item.first.strip)
        value = item.last.strip
        @info[key] = value
      end
      @info
    end

    def normalize(text)
      text.gsub(/\ /, '_').gsub(/\./, '').downcase
    end
  end
end
<commit_msg>Fix infinite loop when executing 'lan print' command<commit_after>module Rubyipmi::Ipmitool
  class Lan < Rubyipmi::Ipmitool::BaseCommand
    attr_accessor :info
    attr_accessor :channel
    MAX_RETRY = 1

    def initialize(opts = ObservableHash.new)
      super("ipmitool", opts)
      @info = {}
      @channel = 2
    end

    # sets the info var to be empty causing the variable to repopulate upon the next call to info
    def refresh
      @info = {}
    end

    def channel=(num)
      refresh
      @channel = num
    end

    def info
      retrycount = 0
      if @info.length < 1
        begin
          parse(print)
        rescue
          # sometimes we need to get the info from channel 1,
          # wait for error to occur then retry using channel 1
          if retrycount < MAX_RETRY
            @channel = 1
            retrycount = retrycount.next
            retry
          else
            # failed to fetch info, return cached info
            @info
          end
        end
      else
        # return the cached info
        @info
      end
    end

    def snmp
      info.fetch("snmp_community_string", nil)
    end

    def ip
      info.fetch("ip_address", nil)
    end

    def mac
      info.fetch("mac_address", nil)
    end

    def netmask
      info.fetch("subnet_mask", nil)
    end

    def gateway
      info.fetch("default_gateway_ip", nil)
    end

    def vlanid
      info.fetch("802.1q_vlan_id", nil)
    end

    #  def snmp=(community)
    #    @options["cmdargs"] = "lan set #{channel} snmp #{community}"
    #    value = runcmd
    #    @options.delete_notify("cmdargs")
    #    return value
    #  end

    def ip=(address)
      @options["cmdargs"] = "lan set #{channel} ipaddr #{address}"
      value = runcmd
      @options.delete_notify("cmdargs")
      value
    end

    def netmask=(mask)
      @options["cmdargs"] = "lan set #{channel} netmask #{mask}"
      value = runcmd
      @options.delete_notify("cmdargs")
      value
    end

    def gateway=(address)
      @options["cmdargs"] = "lan set #{channel} defgw ipaddr #{address}"
      value = runcmd
      @options.delete_notify("cmdargs")
      value
    end

    def dhcp?
      info.fetch("ip_address_source", nil).match(/dhcp/i) != nil
    end

    def static?
      info.fetch("ip_address_source", nil).match(/static/i) != nil
    end

    def vlanid=(vlan)
      @options["cmdargs"] = "lan set #{channel} vlan id #{vlan}"
      value = runcmd
      @options.delete_notify("cmdargs")
      value
    end

    private

    def print
      @options["cmdargs"] = "lan print"
      value = runcmd
      @options.delete_notify("cmdargs")
      @result if value
    end

    def parse(landata)
      landata.lines.each do |line|
        # clean up the data from spaces
        item = line.split(':', 2)
        key = normalize(item.first.strip)
        value = item.last.strip
        @info[key] = value
      end
      @info
    end

    def normalize(text)
      text.gsub(/\ /, '_').gsub(/\./, '').downcase
    end
  end
end

{'fraction_non_alphanumeric': 0.10546743165710429, 'fraction_numerical': 0.004049949375632805, 'mean_word_length': 1.9458250497017893, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6944132', 'n_tokens_mistral': 1890, 'n_tokens_neox': 1817, 'n_words': 514}