diff query.py @ 14:e780b47013df draft

Uploaded 20180201
author fabio
date Thu, 01 Feb 2018 17:13:30 -0500
parents 039e8e1e8b1f
children dd3c4fd64402
line wrap: on
line diff
--- a/query.py	Thu Feb 01 16:23:26 2018 -0500
+++ b/query.py	Thu Feb 01 17:13:30 2018 -0500
@@ -18,9 +18,12 @@
 
 __version__ = "1.0.0";
 VALID_CHARS = '.-()[]0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ '
+# in the case of collections, exitcodes equal to 0 and 1 are not considered errors
+ERR_EXIT_CODE = 2;
+OK_EXIT_CODE = 0;
 
-# in the case of collections, exitcodes equal to 0 and 1 are not considered errors
-def raiseException( exitcode, message, errorfilepath ):
+def raiseException( exitcode, message, output_dir_path, errorfilename ):
+    errorfilepath = os.path.join(output_dir_path, errorfilename+"_txt");
     with open(errorfilepath, 'w') as out:
         out.write(message);
     sys.exit(exitcode);
@@ -66,7 +69,7 @@
                 task_processed = True;
                 break;
             elif json_status_content['state'] in ['FAILURE', 'REVOKED']:
-                return raiseException( 1, "Task ID: "+str(task_id)+"\nTask status: "+str(json_status_content['state']), str(options.errorfile) );
+                return raiseException( ERR_EXIT_CODE, "Task ID: "+str(task_id)+"\nTask status: "+str(json_status_content['state']), output_dir_path, str(options.errorfile) );
             else:
                 time.sleep(QUERY_DELAY); # in seconds
         
@@ -81,9 +84,9 @@
                 accessions_list = accessions_list + accession_number + "\n";
             with open(output_file_path, 'w') as out:
                 out.write(accessions_list.strip());
-        return sys.exit(0);
+        return sys.exit(OK_EXIT_CODE);
     else:
-        return raiseException( 1, "Unable to query the remote server. Please try again in a while.", str(options.errorfile) );
+        return raiseException( ERR_EXIT_CODE, "Unable to query the remote server. Please try again in a while.", output_dir_path, str(options.errorfile) );
 
 def query( options, args ):
     output_dir_path = options.outputdir;
@@ -110,13 +113,13 @@
                             seq_id = ''.join(e for e in seq_id if e in VALID_CHARS)
                             seq_text = line_split[1];
                             if seq_id in multiple_data:
-                                return raiseException( 1, "Error: the id '"+seq_id+"' is duplicated", str(options.errorfile) );
+                                return raiseException( ERR_EXIT_CODE, "Error: the id '"+seq_id+"' is duplicated", output_dir_path, str(options.errorfile) );
                             multiple_data[seq_id] = seq_text;
         if len(multiple_data) > 0:
             return query_request( options, args,  multiple_data );
             #return echo( options, args );
         else:
-            return raiseException( 1, "An error has occurred. Please be sure that your input files are valid.", str(options.errorfile) );
+            return raiseException( ERR_EXIT_CODE, "An error has occurred. Please be sure that your input files are valid.", output_dir_path, str(options.errorfile) );
     else:
         # try with the sequence in --sequence
         text_content = options.sequences;
@@ -136,16 +139,16 @@
                             seq_id = ''.join(e for e in seq_id if e in VALID_CHARS)
                             seq_text = line_split[1];
                             if seq_id in multiple_data:
-                                return raiseException( 1, "Error: the id '"+seq_id+"' is duplicated", str(options.errorfile) );
+                                return raiseException( ERR_EXIT_CODE, "Error: the id '"+seq_id+"' is duplicated", output_dir_path, str(options.errorfile) );
                             multiple_data[seq_id] = seq_text;
                 if len(multiple_data) > 0:
                     return query_request( options, args, multiple_data );
                     #return echo( options, args );
                 else:
-                    return raiseException( 1, "An error has occurred. Please be sure that your input files are valid.", str(options.errorfile) );
+                    return raiseException( ERR_EXIT_CODE, "An error has occurred. Please be sure that your input files are valid.", output_dir_path, str(options.errorfile) );
             else:
-                return raiseException( 1, "You have to insert at least one row formatted as a tab delimited (ID, SEQUENCE) couple", str(options.errorfile) );
-    return 1;
+                return raiseException( ERR_EXIT_CODE, "You have to insert at least one row formatted as a tab delimited (ID, SEQUENCE) couple", output_dir_path, str(options.errorfile) );
+    return ERR_EXIT_CODE;
 
 def __main__():
     # Parse the command line options