Class: RobotConfigParser
- Inherits:
-
Object
- Object
- RobotConfigParser
- Defined in:
- lib/robot-controller/robots.rb
Constant Summary collapse
- ROBOT_INSTANCE_MAX =
16
Instance Method Summary collapse
-
#build_queues(robot, lanes) ⇒ Object
build_queues(‘z’,‘A’) => [‘z_A’] build_queues(‘z’,‘A,C’) => [‘z_A’, ‘z_C’].
-
#load(robots_fn, dir = 'config/environments', host = nil) ⇒ Object
main entry point.
-
#parse_instances(n) ⇒ Object
parse_instances(1) == 1 parse_instances(16) == 16 parse_instances(0) == 1 parse_instances(99) => RuntimeError.
-
#parse_lanes(lanes_spec) ⇒ Object
parse_lanes(”) == [‘default’] parse_lanes(‘ ’) == [‘default’] parse_lanes(‘ , ’) == [‘default’] parse_lanes(‘ , ,’) == [‘default’] parse_lanes(‘*’) == [‘*’] parse_lanes(‘1’) == [‘1’] parse_lanes(‘A’) == [‘A’] parse_lanes(‘A , B’) == [‘A’, ‘B’] parse_lanes(‘A,B,C’) == [‘A’,‘B’,‘C’] parse_lanes(‘A-C,E’) == [‘A-C’, ‘E’].
Instance Method Details
#build_queues(robot, lanes) ⇒ Object
build_queues(‘z’,‘A’) => [‘z_A’] build_queues(‘z’,‘A,C’) => [‘z_A’, ‘z_C’]
35 36 37 38 39 40 41 |
# File 'lib/robot-controller/robots.rb', line 35 def build_queues(robot, lanes) queues = [] parse_lanes(lanes).each do |i| queues << [robot, i].join('_') end queues end |
#load(robots_fn, dir = 'config/environments', host = nil) ⇒ Object
main entry point
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/robot-controller/robots.rb', line 44 def load(robots_fn, dir = 'config/environments', host = nil) # Validate parameters robots_fn = File.join(dir, robots_fn) if dir unless File.file?(robots_fn) raise RuntimeError, "FileNotFound: #{robots_fn}" end # read the YAML file puts "Loading #{robots_fn}" robots = YAML.load_file(robots_fn) # puts robots # determine current host host = `hostname -s`.strip unless host # puts host # host = 'sul-robots1-dev' # XXX unless robots.include?(host) raise RuntimeError, "HostMismatch: #{host} not defined in #{robots_fn}" end # parse YAML lines for host where i is robot[:lane[:instances]] r = [] robots[host].each do |i| robot = i.split(/:/).collect {|j| j.strip} robot.each do |j| if j.strip == '' raise RuntimeError, "SyntaxError: #{i}" end end # add defaults if robot.size == 1 robot << 'default' end if robot.size == 2 robot << '1' end # build queues for robot instances unless robot.size == 3 raise RuntimeError, "SyntaxError: #{i}" end robot[2] = parse_instances(robot[2].to_i) # puts robot.join(' : ') queues = build_queues(robot[0], robot[1]) # puts queues r << {:robot => robot[0], :queues => queues, :n => robot[2] } end r end |
#parse_instances(n) ⇒ Object
parse_instances(1) == 1 parse_instances(16) == 16 parse_instances(0) == 1 parse_instances(99) => RuntimeError
10 11 12 13 14 15 16 |
# File 'lib/robot-controller/robots.rb', line 10 def parse_instances(n) if n > ROBOT_INSTANCE_MAX raise RuntimeError, "TooManyInstances: #{n} > #{ROBOT_INSTANCE_MAX}" end n = 1 if n < 1 n end |
#parse_lanes(lanes_spec) ⇒ Object
parse_lanes(”) == [‘default’] parse_lanes(‘ ’) == [‘default’] parse_lanes(‘ , ’) == [‘default’] parse_lanes(‘ , ,’) == [‘default’] parse_lanes(‘*’) == [‘*’] parse_lanes(‘1’) == [‘1’] parse_lanes(‘A’) == [‘A’] parse_lanes(‘A , B’) == [‘A’, ‘B’] parse_lanes(‘A,B,C’) == [‘A’,‘B’,‘C’] parse_lanes(‘A-C,E’) == [‘A-C’, ‘E’]
28 29 30 31 |
# File 'lib/robot-controller/robots.rb', line 28 def parse_lanes(lanes_spec) return ['default'] if lanes_spec.split(/,/).collect {|l| l.strip}.join('') == '' lanes_spec.split(/,/).collect {|l| l.strip }.uniq end |