Class: Vidispine::API::Utilities::CLI

Inherits:
CLI
  • Object
show all
Defined in:
lib/vidispine/api/utilities/cli.rb

Constant Summary

Constants inherited from CLI

CLI::LOGGING_LEVELS

Instance Attribute Summary collapse

Attributes inherited from CLI

#arguments, #initial_arguments, #initial_options, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CLI

clear_cached_arguments, default_options_file_path, executable_name, help, help_usage_append, help_usage_default, #initialize, #initialize_logger, log_to_as_string, parse_arguments, parse_arguments_from_command_line, parse_arguments_from_options_file, run

Constructor Details

This class inherits a constructor from Vidispine::CLI

Instance Attribute Details

#apiObject

Returns the value of attribute api.



43
44
45
# File 'lib/vidispine/api/utilities/cli.rb', line 43

def api
  @api
end

#loggerObject

Returns the value of attribute logger.



43
44
45
# File 'lib/vidispine/api/utilities/cli.rb', line 43

def logger
  @logger
end

Class Method Details

.define_parametersObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/vidispine/api/utilities/cli.rb', line 16

def self.define_parameters
  default_http_host_address = Vidispine::API::Client::HTTPClient::DEFAULT_HTTP_HOST_ADDRESS
  default_http_host_port = Vidispine::API::Client::HTTPClient::DEFAULT_HTTP_HOST_PORT
  default_accepts_header = Vidispine::API::Client::HTTPClient::DEFAULT_HEADER_ACCEPTS
  default_content_type_header = Vidispine::API::Client::HTTPClient::DEFAULT_HEADER_CONTENT_TYPE

  argument_parser.on('--host-address HOSTADDRESS', 'The address of the server to communicate with.', "\tdefault: #{default_http_host_address}") { |v| arguments[:http_host_address] = v }
  argument_parser.on('--host-port HOSTPORT', 'The port to use when communicating with the server.', "\tdefault: #{default_http_host_port}") { |v| arguments[:http_host_port] = v }
  argument_parser.on('--username USERNAME', 'The account username to authenticate with.') { |v| arguments[:username] = v }
  argument_parser.on('--password PASSWORD', 'The account password to authenticate with.') { |v| arguments[:password] = v }

  argument_parser.on('--accept-header VALUE', 'The value for the Accept header sent in each request.', "\tdefault: #{default_accepts_header}") { |v| arguments[:accepts_header] = v }
  argument_parser.on('--content-type VALUE', 'The value for the Content-Type header sent in each request.', "\tdefault: #{default_content_type_header}") { |v| arguments[:content_type] = v }
  argument_parser.on('--method-name METHODNAME', 'The name of the method to call.') { |v| arguments[:method_name] = v }
  argument_parser.on('--method-arguments JSON', 'The arguments to pass when calling the method.') { |v| arguments[:method_arguments] = v }
  argument_parser.on('--storage-map JSON', 'A map of file paths to storage ids to use in utility methods.') { |v| arguments[:storage_map] = v }
  argument_parser.on('--metadata-map JSON', 'A map of field aliases to field names to use in utility methods.') { |v| arguments[:metadata_map] = v }
  argument_parser.on('--pretty-print', 'Will format the output to be more human readable.') { |v| arguments[:pretty_print] = v }

  argument_parser.on('--log-to FILENAME', 'Log file location.', "\tdefault: #{log_to_as_string}") { |v| arguments[:log_to] = v }
  argument_parser.on('--log-level LEVEL', LOGGING_LEVELS.keys, "Logging level. Available Options: #{LOGGING_LEVELS.keys.join(', ')}",
        "\tdefault: #{LOGGING_LEVELS.invert[arguments[:log_level]]}") { |v| arguments[:log_level] = LOGGING_LEVELS[v] }

  argument_parser.on('--[no-]options-file [FILENAME]', 'Path to a file which contains default command line arguments.', "\tdefault: #{arguments[:options_file_path]}" ) { |v| arguments[:options_file_path] = v}
  argument_parser.on_tail('-h', '--help', 'Display this message.') { puts help; exit }
end

Instance Method Details

#after_initializeObject



45
46
47
# File 'lib/vidispine/api/utilities/cli.rb', line 45

def after_initialize
  initialize_api(arguments)
end

#initialize_api(args = { }) ⇒ Object



49
50
51
# File 'lib/vidispine/api/utilities/cli.rb', line 49

def initialize_api(args = { })
  @api = Vidispine::API::Utilities.new(args )
end

#prettify_json(json) ⇒ Object



111
112
113
# File 'lib/vidispine/api/utilities/cli.rb', line 111

def prettify_json(json)
  JSON.pretty_generate(JSON.parse(json))
end

#prettify_xml(xml, options = { }) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/vidispine/api/utilities/cli.rb', line 115

def prettify_xml(xml, options = { })
  document = REXML::Document.new(xml)
  document.write(output = '', options[:indent] || 1)

  return output unless options.fetch(:collapse_tags, true)

  last_open_tag = ''
  #last_matching_close_tag = ''
  last_was_tag = false
  last_was_matching_close_tag = false

  output.lines.map do |v|
    _v = v.strip

    is_tag = _v.start_with?('<') and _v.end_with?('>')
    is_open_tag = (is_tag and !_v.start_with?('</'))
    is_matching_close_tag = (is_tag and !is_open_tag and _v == "</#{(last_open_tag || '')[1..-1]}")

    _output = if is_open_tag
                "#{(last_was_tag and !last_was_matching_close_tag) ? "\n" : ''}#{v.rstrip}"
              elsif is_matching_close_tag
                "#{_v}\n"
              elsif !is_tag
                _v
              else
                v
              end
    #puts "V: '#{_v}' IT: #{is_tag} IOT: #{is_open_tag} ICT: #{is_matching_close_tag} LOT: '#{last_open_tag}' LCT: #{last_matching_close_tag} OUTPUT: '#{_output}'"

    last_was_tag = is_tag
    last_was_matching_close_tag = is_matching_close_tag
    if is_open_tag
      last_open_tag = _v.split(' ').first
      last_open_tag << '>' unless last_open_tag.end_with?('>')
    elsif is_matching_close_tag
      #last_matching_close_tag = _v
    end

    _output
  end.join('')
end

#run(args = arguments, opts = options) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/vidispine/api/utilities/cli.rb', line 53

def run(args = arguments, opts = options)
  storage_map = args[:storage_map]
  @api.default_storage_map = JSON.parse(storage_map) if storage_map.is_a?(String)

   = args[:metadata_map]
  @api. = JSON.parse() if .is_a?(String)

  method_name = args[:method_name]
  send(method_name, args[:method_arguments], :pretty_print => args[:pretty_print]) if method_name

  self
end

#send(method_name, method_arguments, params = {}) ⇒ Object



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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/vidispine/api/utilities/cli.rb', line 66

def send(method_name, method_arguments, params = {})
  method_name = method_name.to_sym
  logger.debug { "Executing Method: #{method_name}" }

  send_arguments = [ method_name ]

  if method_arguments
    method_arguments = JSON.parse(method_arguments, :symbolize_names => true) if method_arguments.is_a?(String) and method_arguments.start_with?('{', '[')
    send_arguments.concat  method_arguments.is_a?(Array) ? [ *method_arguments ] : [ method_arguments ]
  end
  #puts "Send Arguments: #{send_arguments.inspect}"
  response = api.__send__(*send_arguments)

  # if response.code.to_i.between?(500,599)
  #   puts parsed_response
  #   exit
  # end
  #
  # if ResponseHandler.respond_to?(method_name)
  #   ResponseHandler.client = api
  #   ResponseHandler.response = response
  #   response = ResponseHandler.__send__(*send_arguments)
  # end

  if params[:pretty_print]
    if response.is_a?(String)
      _response_cleaned = response.strip
      if _response_cleaned.start_with?('{', '[')
        puts prettify_json(response)
      elsif _response_cleaned.start_with?('<') and _response_cleaned.end_with?('>')
        puts prettify_xml(response)
      else
        #pp response.is_a?(String) ? response : JSON.pretty_generate(response) rescue response
        puts response.is_a?(String) ? response : JSON.pretty_generate(response) rescue response
      end
    else
      pp response.is_a?(String) ? response : JSON.pretty_generate(response) rescue response
    end
  else
    response = JSON.generate(response) if response.is_a?(Hash) or response.is_a?(Array)
    puts response
  end
  # send
end