In Files

YALTools::CmdLine

Description

YALTools::CmdLine module is a collection of methods which are highly ad-hoc and depending on this project.

These are not intended not to reuse by another project. If a reusable method is developed, it should be moved to another module package.

Public Class Methods

getCouch(conf,label,debug=false) click to toggle source

returns the instance of YALTools::Main.

This method might execute “exit(1)” because of

  • missing configuration file.

  • missing label of configuration file.

  • failed to connect to the couchdb server.

This method is designed for interactive tools, not for a daemon process use.

     # File yalt/cmdline.rb, line 127
127:     def getCouch(conf,label,debug=false)
128:       ret = nil
129:       
130:       if not FileTest::exist?(conf)
131:         $stderr.puts "[error] conf file, #{conf}. No such file or directory."
132:         exit(1)
133:       end
134:       begin
135:         wrapper = YALTools::MainWrapper.new(conf, label)
136:         wrapper.debug = debug
137:         wrapper.set_master_pwfile(get_default_master_pwfile(conf))
138:         $stderr.puts "" if debug
139:         ret = wrapper.getCouch
140:       rescue YALTools::LabelNotFoundError
141:         $stderr.puts "[error] label name, #{label}, is not found."
142:         $stderr.puts "[error] reason: #{$!}" if debug
143:         exit(1)
144:       rescue YALTools::ServerConnectionError
145:         $stderr.puts "[error] wrong server setting corresponding to the label, #{label}."
146:         $stderr.puts "[error] reason: #{$!}" if debug
147:         exit(1)
148:       rescue
149:         $stderr.puts "[error] reason: #{$!}" if debug
150:       end
151:       return ret
152:     end
get_default_master_pwfile(conf) click to toggle source

returns the default master_pw filepath.

     # File yalt/cmdline.rb, line 109
109:     def get_default_master_pwfile(conf)
110:       require 'socket'
111:       basedir = File::dirname(conf)
112:       ret = File::join([basedir, "master_pw.#{Socket::gethostname}.json"])
113:       ret = File::join([basedir, "master_pw.json"]) if not FileTest::exist?(ret)
114:       return ret
115:     end
get_default_yaml_conf(basedir) click to toggle source

return the default yaml conf filepath.

     # File yalt/cmdline.rb, line 98
 98:     def get_default_yaml_conf(basedir)
 99:       ret = ""
100:       ret = ENV["YALTCONFIG"] if ENV["YALTCONFIG"] != nil
101:       ret = File::join([basedir,"..","conf","yalt.#{Socket::gethostname}.yaml"]) if ret.empty? or not FileTest::exist?(ret)
102:       ret = File::join([basedir,"..","conf","yalt.yaml"]) if ret.empty? or not FileTest::exist?(ret)
103:       
104:       return ret
105:     end
get_default_yaml_label() click to toggle source

returns the default label name, “default.user“

    # File yalt/cmdline.rb, line 91
91:     def get_default_yaml_label()
92:       return "default.user"
93:     end
line_to_json(line) click to toggle source

parses the line as json. returns the Hash object which converted by JSON::parse(line).

    # File yalt/cmdline.rb, line 37
37:     def line_to_json(line) # :yields: json_hash
38:       json = nil
39:       begin
40:         json = JSON::parse(line)
41:       rescue
42:         json = {}
43:       end
44:       return json
45:     end
load_line(file = '-') click to toggle source

iterates line which is read from $stdin or file.

The default value, ’-’, means that the $stdin is default source.

    # File yalt/cmdline.rb, line 22
22:     def load_line(file = '-') # :yields: line
23:       if file == "-"
24:         $stdin.each do |line|
25:           yield line.strip
26:         end
27:       elsif FileTest.exist?(file)
28:         open(file).each do |line|
29:           yield line.strip
30:         end
31:       end
32:     end
load_line_as_csv(file, sep) click to toggle source

returnds the Array object which generated by CSV class.

    # File yalt/cmdline.rb, line 49
49:     def load_line_as_csv(file, sep) # :yields: row
50:       require 'csv'
51:       input = nil
52:       input = $stdin if file == "-"
53:       input = open(file)  if FileTest.exist?(file)
54:       
55:       opts = {}
56:       opts[:col_sep] = sep
57:       opts[:headers] = true
58:       opts[:skip_blanks] = true
59:       CSV.new(input, opts).each do |row|
60:         yield row
61:       end
62:     end
save_data(data, file, mode="a+", perms=0755) click to toggle source

writes the data into file or $stdout. It assumes that the data is text data, that is the why it uses the puts() method instead of write().

    # File yalt/cmdline.rb, line 67
67:     def save_data(data, file, mode="a+", perms=0755)
68:       if file == "-"
69:         $stdout.puts(data)
70:         $stdout.flush
71:       else
72:         open(file, mode, perms) do |f|
73:           f.puts(data)
74:           f.flush
75:         end
76:       end
77:     end

Public Instance Methods

get_header(couch,uri) click to toggle source

sends HEAD request and returns a Hash object of results.

     # File yalt/cmdline.rb, line 156
156:     def get_header(couch,uri)
157:       ret = couch.head(uri)
158:       if ret.kind_of?(Net::HTTPSuccess)
159:         json = {}
160:         ret.each do |i|
161:           json[i] = ret[i]
162:         end
163:         return json
164:       end
165:     end

Private Instance Methods

getCouch(conf,label,debug=false) click to toggle source

returns the instance of YALTools::Main.

This method might execute “exit(1)” because of

  • missing configuration file.

  • missing label of configuration file.

  • failed to connect to the couchdb server.

This method is designed for interactive tools, not for a daemon process use.

     # File yalt/cmdline.rb, line 127
127:     def getCouch(conf,label,debug=false)
128:       ret = nil
129:       
130:       if not FileTest::exist?(conf)
131:         $stderr.puts "[error] conf file, #{conf}. No such file or directory."
132:         exit(1)
133:       end
134:       begin
135:         wrapper = YALTools::MainWrapper.new(conf, label)
136:         wrapper.debug = debug
137:         wrapper.set_master_pwfile(get_default_master_pwfile(conf))
138:         $stderr.puts "" if debug
139:         ret = wrapper.getCouch
140:       rescue YALTools::LabelNotFoundError
141:         $stderr.puts "[error] label name, #{label}, is not found."
142:         $stderr.puts "[error] reason: #{$!}" if debug
143:         exit(1)
144:       rescue YALTools::ServerConnectionError
145:         $stderr.puts "[error] wrong server setting corresponding to the label, #{label}."
146:         $stderr.puts "[error] reason: #{$!}" if debug
147:         exit(1)
148:       rescue
149:         $stderr.puts "[error] reason: #{$!}" if debug
150:       end
151:       return ret
152:     end
get_default_master_pwfile(conf) click to toggle source

returns the default master_pw filepath.

     # File yalt/cmdline.rb, line 109
109:     def get_default_master_pwfile(conf)
110:       require 'socket'
111:       basedir = File::dirname(conf)
112:       ret = File::join([basedir, "master_pw.#{Socket::gethostname}.json"])
113:       ret = File::join([basedir, "master_pw.json"]) if not FileTest::exist?(ret)
114:       return ret
115:     end
get_default_yaml_conf(basedir) click to toggle source

return the default yaml conf filepath.

     # File yalt/cmdline.rb, line 98
 98:     def get_default_yaml_conf(basedir)
 99:       ret = ""
100:       ret = ENV["YALTCONFIG"] if ENV["YALTCONFIG"] != nil
101:       ret = File::join([basedir,"..","conf","yalt.#{Socket::gethostname}.yaml"]) if ret.empty? or not FileTest::exist?(ret)
102:       ret = File::join([basedir,"..","conf","yalt.yaml"]) if ret.empty? or not FileTest::exist?(ret)
103:       
104:       return ret
105:     end
get_default_yaml_label() click to toggle source

returns the default label name, “default.user“

    # File yalt/cmdline.rb, line 91
91:     def get_default_yaml_label()
92:       return "default.user"
93:     end
line_to_json(line) click to toggle source

parses the line as json. returns the Hash object which converted by JSON::parse(line).

    # File yalt/cmdline.rb, line 37
37:     def line_to_json(line) # :yields: json_hash
38:       json = nil
39:       begin
40:         json = JSON::parse(line)
41:       rescue
42:         json = {}
43:       end
44:       return json
45:     end
load_line(file = '-') click to toggle source

iterates line which is read from $stdin or file.

The default value, ’-’, means that the $stdin is default source.

    # File yalt/cmdline.rb, line 22
22:     def load_line(file = '-') # :yields: line
23:       if file == "-"
24:         $stdin.each do |line|
25:           yield line.strip
26:         end
27:       elsif FileTest.exist?(file)
28:         open(file).each do |line|
29:           yield line.strip
30:         end
31:       end
32:     end
load_line_as_csv(file, sep) click to toggle source

returnds the Array object which generated by CSV class.

    # File yalt/cmdline.rb, line 49
49:     def load_line_as_csv(file, sep) # :yields: row
50:       require 'csv'
51:       input = nil
52:       input = $stdin if file == "-"
53:       input = open(file)  if FileTest.exist?(file)
54:       
55:       opts = {}
56:       opts[:col_sep] = sep
57:       opts[:headers] = true
58:       opts[:skip_blanks] = true
59:       CSV.new(input, opts).each do |row|
60:         yield row
61:       end
62:     end
save_data(data, file, mode="a+", perms=0755) click to toggle source

writes the data into file or $stdout. It assumes that the data is text data, that is the why it uses the puts() method instead of write().

    # File yalt/cmdline.rb, line 67
67:     def save_data(data, file, mode="a+", perms=0755)
68:       if file == "-"
69:         $stdout.puts(data)
70:         $stdout.flush
71:       else
72:         open(file, mode, perms) do |f|
73:           f.puts(data)
74:           f.flush
75:         end
76:       end
77:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.