1- #!/export/working/virtualenv/osdf/bin/python2
1+ #!/usr/bin/env python
2+
3+ """
4+ Command-line utility for interacting with OSDF servers.
5+ """
6+
7+ #pylint:disable=broad-except
28
39import argparse
410import json
5- from jsondiff import diff
6- import logging
711import os
812import sys
13+
14+ from jsondiff import diff
915from osdf import OSDF
1016
1117def parse_config ():
@@ -22,6 +28,7 @@ def parse_config():
2228 sys .exit (1 )
2329
2430 perms = oct (os .stat (config_file ).st_mode & 0777 )
31+
2532 if perms != '0400' :
2633 msg = "Permissions on config {} are too loose. Should be 0400."
2734 raise Exception (msg .format (config_file ))
@@ -38,7 +45,7 @@ def parse_config():
3845 ssl = config .getboolean (section , "ssl" )
3946
4047 return (server , username , password , ssl )
41-
48+
4249def get_client ():
4350 """
4451 Creates and retrieves an OSDF object that is used as the client for all
@@ -56,13 +63,14 @@ def init(args):
5663 file. We honor interrupts since this is an interactive process where we
5764 ask the user several questions.
5865 """
66+ #pylint:disable=unused-argument
5967 try :
60- init_helper (args )
68+ init_helper ()
6169 except KeyboardInterrupt :
6270 print ("\n Aborted\n ." )
6371 sys .exit (0 )
6472
65- def init_helper (args ):
73+ def init_helper ():
6674 """
6775 Utility function called by init(). Asks the user several questions,
6876 including a password and confirmation. If the process is successful, the
@@ -84,18 +92,18 @@ def init_helper(args):
8492
8593 # Get the server
8694 server = ""
87- while len ( server ) == 0 :
95+ while not server :
8896 server = raw_input ("What is the hostname or IP address of " + \
8997 "the OSDF server?\n " )
9098 # Get the username
9199 username = ""
92- while len ( username ) == 0 :
100+ while not username :
93101 username = raw_input ("What is your OSDF username?\n " )
94102
95103 # Get the password (twice) and compare
96104 password = ""
97105 password2 = ""
98- while len ( password ) == 0 or password != password2 :
106+ while not password or password != password2 :
99107 import getpass
100108
101109 print ("What is your OSDF password? (masked)" )
@@ -108,7 +116,7 @@ def init_helper(args):
108116 print ("Passwords did not match. Please try again..." )
109117
110118 ssl_answer = ""
111- while len ( ssl_answer ) == 0 :
119+ while not ssl_answer :
112120 ssl_answer = raw_input ("Is the OSDF server using SSL/TLS?\n " )
113121
114122 if (ssl_answer .lower () == "yes" or ssl_answer .lower () == "y" ):
@@ -152,7 +160,7 @@ def edit(args):
152160 client = get_client ()
153161 data = client .get_node (node_id )
154162 json_doc = json .dumps (data , indent = 2 )
155- except Exception as e :
163+ except Exception :
156164 sys .stderr .write ("Unable to retrieve node \" {}\" .\n " .format (node_id ))
157165 sys .exit (1 )
158166
@@ -169,12 +177,12 @@ def edit(args):
169177 new_node = None
170178 try :
171179 new_node = json .loads (new_data )
172- except :
180+ except Exception :
173181 print ("Aborted. Edited data resulted in invalid JSON." )
174182 sys .exit (2 )
175183
176184 exit_value = 1
177- difference = diff (data , new_node )
185+ difference = diff (data , new_node )
178186
179187 if difference :
180188 new_node = json .loads (new_data )
@@ -198,12 +206,20 @@ def info(args):
198206 This is sometimes useful as a check to see if the server is running or
199207 not.
200208 """
209+ #pylint:disable=unused-argument
210+
201211 exit_value = 1
202212
203213 try :
204214 client = get_client ()
205- info = client .get_info ()
206- print (json .dumps (info , indent = 2 , sort_keys = True ))
215+ info_data = client .get_info ()
216+ server = client .server
217+ port = client .port
218+
219+ print ("Server: {}:{}" .format (server , port ))
220+ print ("---------" )
221+ print (json .dumps (info_data , indent = 2 , sort_keys = True ))
222+
207223 exit_value = 0
208224 except Exception as e :
209225 sys .stderr .write ("Unable to retrieve information: {}\n " .format (e ))
@@ -322,11 +338,13 @@ def cat(args):
322338 Given a node ID, retrieve the data and dump it to STDOUT, much like the
323339 unix `cat` utility operates.
324340 """
341+ #pylint:disable=fixme
325342 # TODO: Honor multiple node IDs?
326343 node_id = args .node
327344 version = args .version
328345
329346 def node_getter (node_id , version ):
347+ """ Fetches a node document. """
330348 data = None
331349
332350 try :
@@ -336,7 +354,7 @@ def cat(args):
336354 data = client .get_node (node_id )
337355 else :
338356 data = client .get_node_by_version (node_id , version )
339- except Exception as e :
357+ except Exception :
340358 if version is not None :
341359 msg = "Unable to retrieve node \" {}\" version {}.\n "
342360 sys .stderr .write (msg .format (node_id , version ))
@@ -346,10 +364,10 @@ def cat(args):
346364
347365 sys .exit (1 )
348366
349- return data ;
367+ return data
350368
351369 data = node_getter (node_id , version )
352-
370+
353371 print (json .dumps (data , indent = 2 , sort_keys = True ))
354372
355373def delete (args ):
@@ -362,11 +380,14 @@ def delete(args):
362380 try :
363381 client = get_client ()
364382 client .delete_node (node_id )
365- except Exception as e :
383+ except Exception :
366384 sys .stderr .write ("Unable to delete node \" {}\" .\n " .format (node_id ))
367385 sys .exit (1 )
368386
369387def main ():
388+ """
389+ The main execution function.
390+ """
370391 # Create the top-level parser
371392 parser = argparse .ArgumentParser (prog = 'osdf' )
372393
@@ -379,13 +400,17 @@ def main():
379400 # Create the parser for the "cat" command
380401 parser_cat = subparsers .add_parser ('cat' , help = 'Dump a node to STDOUT.' )
381402 parser_cat .add_argument ('node' , type = str , help = 'A node ID.' )
382- parser_cat .add_argument ('-v' , '--version' , type = str ,
383- help = "Specify a specific version of the node to retrieve. " +
384- "Defaults to the latest version if not provided." )
403+ parser_cat .add_argument (
404+ '-v' , '--version' , type = str ,
405+ help = "Specify a specific version of the node to retrieve. " + \
406+ "Defaults to the latest version if not provided."
407+ )
385408 parser_cat .set_defaults (func = cat )
386409
387- parser_info = subparsers .add_parser ('info' ,
388- help = 'Display information about the OSDF server.' )
410+ parser_info = subparsers .add_parser (
411+ 'info' ,
412+ help = 'Display information about the OSDF server.'
413+ )
389414 parser_info .set_defaults (func = info )
390415
391416 # Create the parser for the node deletion command
@@ -397,28 +422,40 @@ def main():
397422 parser_oql = subparsers .add_parser ('oql' , help = 'Perform an OQL query.' )
398423 parser_oql .add_argument ('ns' , type = str , help = 'The OSDF namespace to search.' )
399424 parser_oql .add_argument ('query' , type = str , help = 'The OQL query statement.' )
400- parser_oql .add_argument ('-a' , '--all' , action = 'store_true' , help = 'Return all results (no pagination).' )
425+ parser_oql .add_argument (
426+ '-a' , '--all' ,
427+ action = 'store_true' ,
428+ help = 'Return all results (no pagination).'
429+ )
401430 parser_oql .set_defaults (func = oql )
402431
403432 # Create the parser for ES (ElasticSearch Query DSL) querying.
404433 parser_dsl = subparsers .add_parser ('search' ,
405434 help = 'Perform an ElasticSearch DSL query.' )
406435 parser_dsl .add_argument ('ns' , type = str , help = 'The OSDF namespace to search.' )
407436 parser_dsl .add_argument ('query' , type = str , help = 'The query statement.' )
408- parser_dsl .add_argument ('-a' , '--all' , action = 'store_true' , help = 'Return all results (no pagination).' )
437+ parser_dsl .add_argument (
438+ '-a' , '--all' ,
439+ action = 'store_true' ,
440+ help = 'Return all results (no pagination).'
441+ )
409442 parser_dsl .set_defaults (func = search )
410443
411444 # Create the parser for schema retrieval.
412- parser_schemas = subparsers .add_parser ('schemas' ,
413- help = 'Retrieve the schemas for a namespace.' )
445+ parser_schemas = subparsers .add_parser (
446+ 'schemas' ,
447+ help = 'Retrieve the schemas for a namespace.'
448+ )
414449 parser_schemas .add_argument ('ns' , type = str , help = 'The OSDF namespace.' )
415450 parser_schemas .add_argument ('schema' , nargs = '?' , type = str ,
416451 help = 'A specific schema to retrieve.' )
417452 parser_schemas .set_defaults (func = schemas )
418453
419454 # Create the parser for auxiliary schema retrieval.
420- parser_aux = subparsers .add_parser ('aux' ,
421- help = 'Retrieve the auxiliary schemas for a namespace.' )
455+ parser_aux = subparsers .add_parser (
456+ 'aux' ,
457+ help = 'Retrieve the auxiliary schemas for a namespace.'
458+ )
422459 parser_aux .add_argument ('ns' , type = str , help = 'The OSDF namespace.' )
423460 parser_aux .add_argument ('aux_schema' , nargs = '?' , type = str ,
424461 help = 'A specific auxiliary schema to retrieve.' )
0 commit comments