<commit_before># PuppetX::Cisco - Common utility methods used by Cisco Types/Providers
#
# November 2015
#
# Copyright (c) 2015-2016 Cisco and/or its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

module PuppetX
  module Cisco
    # PuppetX::Cisco::Utils: - Common helper methods shared by any Type/Provider
    class Utils
      require 'ipaddr'
      # Helper utility method for ip/prefix format networks.
      # For ip/prefix format '1.1.1.1/24' or 'fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b/64',
      # we need to mask the address using the prefix length so that they
      # are converted to '1.1.1.0/24' or '2000:123:38::/64'
      def self.process_network_mask(network)
        mask = network.split('/')[1]
        address = IPAddr.new(network).to_s
        network = address + '/' + mask unless mask.nil?
        network
      end

      # Convert boolean symbols to strings
      def self.bool_sym_to_s(val)
        return val unless val == :true || val == :false
        (val == :true)
      end

      # Special handling for boolean properties.
      # This helper method returns true if the property
      # flush contains a TrueClass or FalseClass value.
      def self.flush_boolean?(prop)
        prop.is_a?(TrueClass) || prop.is_a?(FalseClass)
      end

      # Helper utility for checking if arrays are overlapping in a
      # give list.
      # For ex: if the list has '2-10,32,42,44-89' and '11-33'
      # then this will fail as they overlap
      def self.fail_array_overlap(list)
        array = []
        list.each do |range, _val|
          larray = range.split(',')
          larray.each do |elem|
            if elem.include?('-')
              elema = elem.split('-').map { |d| Integer(d) }
              ele = elema[0]..elema[1]
              if (array & ele.to_a).empty?
                array << ele.to_a
                array = array.flatten
              else
                fail 'overlapping arrays not allowed'
              end
            else
              elema = []
              elema << elem.to_i
              if (array & elema).empty?
                array << elema
                array = array.flatten
              else
                fail 'overlapping arrays not allowed'
              end
            end
          end
        end
      end

      # Helper utility method for range summarization of VLAN and BD ranges
      # Input is a range string. For example: '10-20, 30, 14, 100-105, 21'
      # Output should be: '10-21,30,100-105'
      def self.range_summarize(range_str, sort=true)
        ranges = []
        range_str.split(/,/).each do |elem|
          if elem =~ /\d+\s*\-\s*\d+/
            range_limits = elem.split(/\-/).map { |d| Integer(d) }
            ranges << (range_limits[0]..range_limits[1])
          else
            ranges << Integer(elem)
          end
        end
        # nrange array below will expand the ranges and get a single list
        nrange = []
        ranges.each do |item|
          # OR operations below will get rid of duplicates
          if item.class == Range
            nrange |= item.to_a
          else
            nrange |= [item]
          end
        end
        nrange.sort! if sort
        ranges = []
        left = nrange.first
        right = nil
        nrange.each do |obj|
          if right && obj != right.succ
            # obj cannot be included in the current range, end this range
            if left != right
              ranges << Range.new(left, right)
            else
              ranges << left
            end
            left = obj # start of new range
          end
          right = obj # move right to point to obj
        end
        if left != right
          ranges << Range.new(left, right)
        else
          ranges << left
        end
        ranges.join(',').gsub('..', '-')
      end
    end # class Utils

    # PuppetX::Cisco::PvlanUtils - Common BGP methods used by BGP Types/Providers
    class PvlanUtils
      def self.prepare_list(input)
        return [] if input.nil? || input.empty?
        result = []
        input.gsub!('-', '..')
        input.gsub!(/\s+/, '')
        new_list = input.split(',')<commit_msg>Add comment<commit_after># PuppetX::Cisco - Common utility methods used by Cisco Types/Providers
#
# November 2015
#
# Copyright (c) 2015-2016 Cisco and/or its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

module PuppetX
  module Cisco
    # PuppetX::Cisco::Utils: - Common helper methods shared by any Type/Provider
    class Utils
      require 'ipaddr'
      # Helper utility method for ip/prefix format networks.
      # For ip/prefix format '1.1.1.1/24' or 'fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b/64',
      # we need to mask the address using the prefix length so that they
      # are converted to '1.1.1.0/24' or '2000:123:38::/64'
      def self.process_network_mask(network)
        mask = network.split('/')[1]
        address = IPAddr.new(network).to_s
        network = address + '/' + mask unless mask.nil?
        network
      end

      # Convert boolean symbols to strings
      def self.bool_sym_to_s(val)
        return val unless val == :true || val == :false
        (val == :true)
      end

      # Special handling for boolean properties.
      # This helper method returns true if the property
      # flush contains a TrueClass or FalseClass value.
      def self.flush_boolean?(prop)
        prop.is_a?(TrueClass) || prop.is_a?(FalseClass)
      end

      # Helper utility for checking if arrays are overlapping in a
      # give list.
      # For ex: if the list has '2-10,32,42,44-89' and '11-33'
      # then this will fail as they overlap
      def self.fail_array_overlap(list)
        array = []
        list.each do |range, _val|
          larray = range.split(',')
          larray.each do |elem|
            if elem.include?('-')
              elema = elem.split('-').map { |d| Integer(d) }
              ele = elema[0]..elema[1]
              if (array & ele.to_a).empty?
                array << ele.to_a
                array = array.flatten
              else
                fail 'overlapping arrays not allowed'
              end
            else
              elema = []
              elema << elem.to_i
              if (array & elema).empty?
                array << elema
                array = array.flatten
              else
                fail 'overlapping arrays not allowed'
              end
            end
          end
        end
      end

      # Helper utility method for range summarization of VLAN and BD ranges
      # Input is a range string. For example: '10-20, 30, 14, 100-105, 21'
      # Output should be: '10-21,30,100-105'
      def self.range_summarize(range_str, sort=true)
        ranges = []
        range_str.split(/,/).each do |elem|
          if elem =~ /\d+\s*\-\s*\d+/
            range_limits = elem.split(/\-/).map { |d| Integer(d) }
            ranges << (range_limits[0]..range_limits[1])
          else
            ranges << Integer(elem)
          end
        end
        # nrange array below will expand the ranges and get a single list
        nrange = []
        ranges.each do |item|
          # OR operations below will get rid of duplicates
          if item.class == Range
            nrange |= item.to_a
          else
            nrange |= [item]
          end
        end
        nrange.sort! if sort
        ranges = []
        left = nrange.first
        right = nil
        nrange.each do |obj|
          if right && obj != right.succ
            # obj cannot be included in the current range, end this range
            if left != right
              ranges << Range.new(left, right)
            else
              ranges << left
            end
            left = obj # start of new range
          end
          right = obj # move right to point to obj
        end
        if left != right
          ranges << Range.new(left, right)
        else
          ranges << left
        end
        ranges.join(',').gsub('..', '-')
      end
    end # class Utils

    # PuppetX::Cisco::PvlanUtils - Common BGP methods used by BGP Types/Providers
    class PvlanUtils
      # This api is used by private vlan to prepare the massage the input.
      # The input can be in the following formats for vlans:
      # 10-12,14. Prepare_list api is transforming this input into a flat array.
      # In the example above the returned array will be 10, 11, 12, 13. Prepare
      # list is first splitting the input on ',' and the than expanding the vlan
      # range element like 10-12 into a flat array. The final result will
      # be a  flat array.

      def self.prepare_list(input)
        return [] if input.nil? || input.empty?
        result = []
        input.gsub!('-', '..')
        input.gsub!(/\s+/, '')
        new_list = input.split(',')
{'fraction_non_alphanumeric': 0.0966365206787928, 'fraction_numerical': 0.023778071334214, 'mean_word_length': 1.785734503255024, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 4, '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': '337708', 'n_tokens_mistral': 2941, 'n_tokens_neox': 2796, 'n_words': 985}