Merge branch '1943-client-sdk-config-files'
authorTim Pierce <twp@curoverse.com>
Tue, 21 Jan 2014 22:14:40 +0000 (17:14 -0500)
committerTim Pierce <twp@curoverse.com>
Tue, 21 Jan 2014 22:14:40 +0000 (17:14 -0500)
Conflicts:
sdk/python/arvados/__init__.py

doc/gen_api_method_docs.py
sdk/cli/bin/arv
sdk/perl/lib/Arvados.pm
sdk/ruby/lib/arvados.rb
services/api/app/controllers/arvados/v1/schema_controller.rb

index ddafeeade1165ac0f27aa58dca49442398552325..ac8fe032ff9c962b4bb4a533b46f33a65b5dd546 100755 (executable)
@@ -9,13 +9,35 @@
 # and will generate Textile documentation files in the current
 # directory.
 
-import requests
+import argparse
+import pprint
 import re
+import requests
 import os
-import pprint
+import sys #debugging
+
+p = argparse.ArgumentParser(description='Generate Arvados API method documentation.')
+
+p.add_argument('--host',
+               type=str,
+               default='localhost',
+               help="The hostname or IP address of the API server")
+
+p.add_argument('--port',
+               type=int,
+               default=9900,
+               help="The port of the API server")
+
+p.add_argument('--output-dir',
+               type=str,
+               default='.',
+               help="Directory in which to write output files.")
+
+args = p.parse_args()
+
+api_url = 'https://{host}:{port}/discovery/v1/apis/arvados/v1/rest'.format(**vars(args))
 
-r = requests.get('https://localhost:9900/discovery/v1/apis/arvados/v1/rest',
-                 verify=False)
+r = requests.get(api_url, verify=False)
 if r.status_code != 200:
     raise Exception('Bad status code %d: %s' % (r.status_code, r.text))
 
@@ -23,15 +45,19 @@ if 'application/json' not in r.headers.get('content-type', ''):
     raise Exception('Unexpected content type: %s: %s' %
                     (r.headers.get('content-type', ''), r.text))
 
-api = r.json
+api = r.json()
 
 resource_num = 0
 for resource in sorted(api[u'resources']):
     resource_num = resource_num + 1
-    out_fname = resource + '.textile'
+    out_fname = os.path.join(args.output_dir, resource + '.textile')
     if os.path.exists(out_fname):
-        print "PATH EXISTS ", out_fname
-        next
+        backup_name = out_fname + '.old'
+        try:
+            os.rename(out_fname, backup_name)
+        except OSError as e:
+            print "WARNING: could not back up {1} as {2}: {3}".format(
+                out_fname, backup_name, e)
     outf = open(out_fname, 'w')
     outf.write(
 """---
index 3fd3c57f07a679dfc024905cf3d2bb154c7324fa..3955c945766204696679a64b4577e51fb6beb4b5 100755 (executable)
@@ -12,7 +12,7 @@ end
 
 # read authentication data from ~/.config/arvados if present
 lineno = 0
-config_file = File.expand_path('~/.config/arvados')
+config_file = File.expand_path('~/.config/arvados/settings.conf')
 if File.exist? config_file then
   File.open(config_file, 'r').each do |line|
     lineno = lineno + 1
@@ -152,7 +152,10 @@ def help_methods(discovery_document, resource, method=nil)
   discovery_document["resources"][resource.pluralize]["methods"].
     each do |k,v|
     description = ''
-    description = '  ' + v["description"] if v.include?("description")
+    if v.include? "description"
+      # add only the first line of the discovery doc description
+      description = '  ' + v["description"].split("\n").first.chomp
+    end
     banner += "   #{sprintf("%20s",k)}#{description}\n"
   end
   banner += "\n"
@@ -171,9 +174,10 @@ def help_resources(discovery_document, resource)
   banner += "\n\n"
   discovery_document["resources"].each do |k,v|
     description = ''
-    if discovery_document["schemas"].include?(k.singularize.capitalize) and 
-        discovery_document["schemas"][k.singularize.capitalize].include?('description') then
-      description = '  ' + discovery_document["schemas"][k.singularize.capitalize]["description"]
+    resource_info = discovery_document["schemas"][k.singularize.capitalize]
+    if resource_info and resource_info.include?('description')
+      # add only the first line of the discovery doc description
+      description = '  ' + resource_info["description"].split("\n").first.chomp
     end
     banner += "   #{sprintf("%30s",k.singularize)}#{description}\n"
   end
index 8a643788b64f15ac743732deb8e9c41ea497d748..31258f51723db253f3e2dbc4dbaf266a7f900279 100644 (file)
@@ -82,9 +82,18 @@ sub new
 sub build
 {
     my $self = shift;
-    $self->{'authToken'} ||= $ENV{'ARVADOS_API_TOKEN'};
-    $self->{'apiHost'} ||= $ENV{'ARVADOS_API_HOST'};
-    $self->{'apiProtocolScheme'} ||= $ENV{'ARVADOS_API_PROTOCOL_SCHEME'};
+
+    $config = load_config_file("$ENV{HOME}/.config/arvados/settings.conf");
+
+    $self->{'authToken'} ||= 
+       $ENV{ARVADOS_API_TOKEN} || $config->{ARVADOS_API_TOKEN};
+
+    $self->{'apiHost'} ||=
+       $ENV{ARVADOS_API_HOST} || $config->{ARVADOS_API_HOST};
+
+    $self->{'apiProtocolScheme'} ||=
+       $ENV{ARVADOS_API_PROTOCOL_SCHEME} ||
+       $config->{ARVADOS_API_PROTOCOL_SCHEME};
 
     $self->{'ua'} = new Arvados::Request;
 
@@ -124,4 +133,21 @@ sub new_request
     Arvados::Request->new();
 }
 
+sub load_config_file ($)
+{
+    my $config_file = shift;
+    my %config;
+
+    if (open (CONF, $config_file)) {
+       while (<CONF>) {
+           next if /^\s*#/ || /^\s*$/;  # skip comments and blank lines
+           chomp;
+           my ($key, $val) = split /\s*=\s*/, $_, 2;
+           $config{$key} = $val;
+       }
+    }
+    close CONF;
+    return \%config;
+}
+
 1;
index 0b004635082968e5f6784b2c65d5cf08f89739c4..5be1fd939ea4ac797c1ceeb263d41b77bc709473 100644 (file)
@@ -32,18 +32,20 @@ class Arvados
     @application_version ||= 0.0
     @application_name ||= File.split($0).last
 
+    @config = self.load_config_file
+
     @arvados_api_version = opts[:api_version] ||
-      ENV['ARVADOS_API_VERSION'] ||
+      @config['ARVADOS_API_VERSION'] ||
       'v1'
     @arvados_api_host = opts[:api_host] ||
-      ENV['ARVADOS_API_HOST'] or
+      @config['ARVADOS_API_HOST'] or
       raise "#{$0}: no :api_host or ENV[ARVADOS_API_HOST] provided."
     @arvados_api_token = opts[:api_token] ||
-      ENV['ARVADOS_API_TOKEN'] or
+      @config['ARVADOS_API_TOKEN'] or
       raise "#{$0}: no :api_token or ENV[ARVADOS_API_TOKEN] provided."
 
-    if (opts[:api_host] ? opts[:suppress_ssl_warnings] :
-        ENV['ARVADOS_API_HOST_INSECURE'])
+    if (opts[:suppress_ssl_warnings] or
+        @config['ARVADOS_API_HOST_INSECURE'])
       suppress_warnings do
         OpenSSL::SSL.const_set 'VERIFY_PEER', OpenSSL::SSL::VERIFY_NONE
       end
@@ -132,6 +134,32 @@ class Arvados
     $stderr.puts "#{File.split($0).last} #{$$}: #{message}" if @@debuglevel >= verbosity
   end
 
+  def load_config_file(config_file_path="~/.config/arvados/settings.conf")
+    # Initialize config settings with environment variables.
+    config = {}
+    config['ARVADOS_API_HOST']          = ENV['ARVADOS_API_HOST']
+    config['ARVADOS_API_TOKEN']         = ENV['ARVADOS_API_TOKEN']
+    config['ARVADOS_API_HOST_INSECURE'] = ENV['ARVADOS_API_HOST_INSECURE']
+    config['ARVADOS_API_VERSION']       = ENV['ARVADOS_API_VERSION']
+
+    # Load settings from the config file.
+    lineno = 0
+    File.open(File.expand_path config_file_path).each do |line|
+      lineno = lineno + 1
+      # skip comments and blank lines
+      next if line.match('^\s*#') or not line.match('\S')
+      var, val = line.chomp.split('=', 2)
+      # allow environment settings to override config files.
+      if var and val
+        config[var] ||= val
+      else
+        warn "#{config_file}: #{lineno}: could not parse `#{line}'"
+      end
+    end
+
+    config
+  end
+
   class Model
     def self.arvados_api
       arvados.arvados_api
@@ -147,7 +175,7 @@ class Arvados
     end
     def self.api_exec(method, parameters={})
       parameters = parameters.
-        merge(:api_token => ENV['ARVADOS_API_TOKEN'])
+        merge(:api_token => @config['ARVADOS_API_TOKEN'])
       parameters.each do |k,v|
         parameters[k] = v.to_json if v.is_a? Array or v.is_a? Hash
       end
index 96e432388739531b47680447e286dfd3fc5912a7..e57ca52081969ed31aaf07109eafd83e9bfe455d 100644 (file)
@@ -189,7 +189,28 @@ class Arvados::V1::SchemaController < ApplicationController
               id: "arvados.#{k.to_s.underscore.pluralize}.list",
               path: k.to_s.underscore.pluralize,
               httpMethod: "GET",
-              description: "List #{k.to_s.underscore.pluralize}.",
+              description:
+                 %|List #{k.to_s.pluralize}.
+
+                   The <code>list</code> method returns a 
+                   <a href="/api/resources.html">resource list</a> of
+                   matching #{k.to_s.pluralize}. For example:
+
+                   <pre>
+                   {
+                    "kind":"arvados##{k.to_s.camelcase(:lower)}List",
+                    "etag":"",
+                    "self_link":"",
+                    "next_page_token":"",
+                    "next_link":"",
+                    "items":[
+                       ...
+                    ],
+                    "items_available":745,
+                    "_profile":{
+                     "request_time":0.157236317
+                    }
+                    </pre>|,
               parameters: {
                 limit: {
                   type: "integer",
@@ -197,17 +218,7 @@ class Arvados::V1::SchemaController < ApplicationController
                   default: 100,
                   format: "int32",
                   minimum: 0,
-                  location: "query"
-                },
-                pageToken: {
-                  type: "string",
-                  description: "Page token.",
-                  location: "query"
-                },
-                q: {
-                  type: "string",
-                  description: "Query string for searching #{k.to_s.underscore.pluralize}.",
-                  location: "query"
+                  location: "query",
                 },
                 where: {
                   type: "object",