Class: AutomateIt::TagManager::TagParser
- Defined in:
- lib/automateit/tag_manager/tag_parser.rb
Overview
TagManager::TagParser
Helper class for parsing tags. Not useful for users – for internal use only.
Instance Attribute Summary collapse
-
#is_trace ⇒ Object
Returns the value of attribute is_trace.
-
#struct ⇒ Object
Returns the value of attribute struct.
Class Method Summary collapse
-
.expand(struct, is_trace = false) ⇒ Object
Expand the
struct
. -
.normalize(text) ⇒ Object
Normalize a block of text to replace shortcut symbols that cause YAML to choke.
Instance Method Summary collapse
-
#expand ⇒ Object
Expand the include/exclude/group rules and return a struct with only the hosts these rules produce.
-
#expand! ⇒ Object
Replace the internal struct with an expanded version, see #expand.
-
#hosts_for(tag) ⇒ Object
Return array of hosts for the
tag
. -
#initialize(struct, is_trace = false) ⇒ TagParser
constructor
Create a parser for the
struct
, a hash of tag keys to values with arrays of items. -
#normalize! ⇒ Object
Normalize the contents of the internal struct.
-
#tags ⇒ Object
Return array of tags.
-
#trace(msg) ⇒ Object
Display debugging information if
is_trace
is enabled.
Constructor Details
#initialize(struct, is_trace = false) ⇒ TagParser
Create a parser for the struct
, a hash of tag keys to values with arrays of items.
9 10 11 12 13 |
# File 'lib/automateit/tag_manager/tag_parser.rb', line 9 def initialize(struct, is_trace=false) self.struct = struct self.is_trace = is_trace normalize! end |
Instance Attribute Details
#is_trace ⇒ Object
Returns the value of attribute is_trace.
6 7 8 |
# File 'lib/automateit/tag_manager/tag_parser.rb', line 6 def is_trace @is_trace end |
#struct ⇒ Object
Returns the value of attribute struct.
5 6 7 |
# File 'lib/automateit/tag_manager/tag_parser.rb', line 5 def struct @struct end |
Class Method Details
.expand(struct, is_trace = false) ⇒ Object
Expand the struct
.
88 89 90 |
# File 'lib/automateit/tag_manager/tag_parser.rb', line 88 def self.(struct, is_trace=false) self.new(struct, is_trace). end |
.normalize(text) ⇒ Object
Normalize a block of text to replace shortcut symbols that cause YAML to choke.
16 17 18 19 20 21 |
# 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 |
Instance Method Details
#expand ⇒ Object
Expand the include/exclude/group rules and return a struct with only the hosts these rules produce.
74 75 76 77 78 79 80 |
# File 'lib/automateit/tag_manager/tag_parser.rb', line 74 def result = {} for tag in result[tag] = hosts_for(tag) end result end |
#expand! ⇒ Object
Replace the internal struct with an expanded version, see #expand.
83 84 85 |
# File 'lib/automateit/tag_manager/tag_parser.rb', line 83 def struct.replace() end |
#hosts_for(tag) ⇒ Object
Return array of hosts for the tag
.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/automateit/tag_manager/tag_parser.rb', line 42 def hosts_for(tag) raise IndexError.new("Unknown tag - #{tag}") unless struct[tag] trace "\nAA %s" % tag hosts = Set.new for item in struct[tag] case item when /^INCLUDE_TAG (\w+)$/ trace "+g %s" % $1 hosts.merge(hosts_for($1)) when /^EXCLUDE_TAG (\w+)$/ trace "-g %s" % $1 hosts.subtract(hosts_for($1)) when /^EXCLUDE_HOST (\w+)$/ trace "-h %s" % $1 hosts.delete($1) else trace "+h %s" % item hosts << item end end result = hosts.to_a trace "ZZ %s for %s" % [result.inspect, tag] return result end |
#normalize! ⇒ Object
Normalize the contents of the internal struct.
24 25 26 27 28 29 30 31 32 33 34 |
# 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 ⇒ Object
Return array of tags.
68 69 70 |
# File 'lib/automateit/tag_manager/tag_parser.rb', line 68 def return struct.keys end |
#trace(msg) ⇒ Object
Display debugging information if is_trace
is enabled.
37 38 39 |
# File 'lib/automateit/tag_manager/tag_parser.rb', line 37 def trace(msg) puts msg if is_trace end |