class AutomateIt::TagManager::TagParser

TagManager::TagParser

Helper class for parsing tags. Not useful for users – for internal use only.

Constants

HOSTS_FOR_EXCLUDE_HOST_RE
HOSTS_FOR_EXCLUDE_TAG_RE
HOSTS_FOR_INCLUDE_TAG_RE
HOSTS_FOR_VALUE

Attributes

struct[RW]

Public Class Methods

expand(struct) click to toggle source

Expand the struct.

# File lib/automateit/tag_manager/tag_parser.rb, line 90
def self.expand(struct)
  self.new(struct).expand
end
new(struct) click to toggle source

Create a parser for the struct, a hash of tag keys to values with arrays of items.

# File lib/automateit/tag_manager/tag_parser.rb, line 10
def initialize(struct)
  self.struct = struct
  normalize!
end
normalize(text) click to toggle source

Normalize a block of text to replace shortcut symbols that cause YAML to choke.

# File lib/automateit/tag_manager/tag_parser.rb, line 16
def self.normalize(text)
  return text \
    .gsub(/^(\s*-\s+)(!@)/, '\1EXCLUDE_TAG ') \
    .gsub(/^(\s*-\s+)(!)/, '\1EXCLUDE_HOST ') \
    .gsub(/^(\s*-\s+)(@)/, '\1INCLUDE_TAG ')
end

Public Instance Methods

expand() click to toggle source

Expand the include/exclude/group rules and return a struct with only the hosts these rules produce.

# File lib/automateit/tag_manager/tag_parser.rb, line 76
def expand
  result = {}
  for tag in tags
    result[tag] = hosts_for(tag)
  end
  result
end
expand!() click to toggle source

Replace the internal struct with an expanded version, see expand.

# File lib/automateit/tag_manager/tag_parser.rb, line 85
def expand!
  struct.replace(expand)
end
hosts_for(tag) click to toggle source

Return array of hosts for the tag.

# File lib/automateit/tag_manager/tag_parser.rb, line 42
def hosts_for(tag)
  raise IndexError.new("Unknown tag - #{tag}") unless struct.has_key?(tag)
  return [] if struct[tag].nil? # Tag has no leaves

  nitpick "\nAA %s" % tag
  hosts = Set.new
  for item in struct[tag]
    case item
    when HOSTS_FOR_INCLUDE_TAG_RE
      nitpick "+g %s" % $1
      hosts.merge(hosts_for($1))
    when HOSTS_FOR_EXCLUDE_TAG_RE
      nitpick "-g %s" % $1
      hosts.subtract(hosts_for($1))
    when HOSTS_FOR_EXCLUDE_HOST_RE
      nitpick "-h %s" % $1
      hosts.delete($1)
    else
      nitpick "+h %s" % item
      hosts << item
    end
  end
  result = hosts.to_a
  nitpick "ZZ %s for %s" % [result.inspect, tag]
  return result
end
normalize!() click to toggle source

Normalize the contents of the internal struct.

# File lib/automateit/tag_manager/tag_parser.rb, line 24
def normalize!
  for tag, items in struct
    next unless items
    for item in items
      next unless item
      item.gsub!(/^(\!@|\^@)\s*/, 'EXCLUDE_TAG ')
      item.gsub!(/^(\!|\^)\s*/, 'EXCLUDE_HOST ')
      item.gsub!(/^(@)\s*/, 'INCLUDE_TAG ')
    end
  end
end
tags() click to toggle source

Return array of tags.

# File lib/automateit/tag_manager/tag_parser.rb, line 70
def tags
  return struct.keys
end