diff hubArchiveCreator.py @ 13:25809f699cb3 draft

planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 65ab931ef2b05a5acf06cbde3a746c94a0a0a4cb
author rmarenco
date Thu, 11 Aug 2016 19:02:29 -0400
parents d05236b15f81
children 3233451a3bd6
line wrap: on
line diff
--- a/hubArchiveCreator.py	Wed Jul 27 10:43:58 2016 -0400
+++ b/hubArchiveCreator.py	Thu Aug 11 19:02:29 2016 -0400
@@ -11,6 +11,8 @@
 import argparse
 import collections
 import json
+import logging
+import os
 import sys
 
 # Internal dependencies
@@ -19,11 +21,11 @@
 from Bed import Bed
 from BigWig import BigWig
 from util.Fasta import Fasta
+from util.Filters import TraceBackFormatter
 from Gff3 import Gff3
 from Gtf import Gtf
 from TrackHub import TrackHub
 
-
 # TODO: Verify each subprocessed dependency is accessible [gff3ToGenePred, genePredToBed, twoBitInfo, faToTwoBit, bedToBigBed, sort
 
 
@@ -67,7 +69,9 @@
 
     parser.add_argument('--genome_name', help='UCSC Genome Browser assembly ID')
 
-    ucsc_tools_path = ''
+    parser.add_argument('--debug_mode', action='store_true', help='Allow more details about the errors')
+
+    # Begin init variables
 
     toolDirectory = '.'
     extra_files_path = '.'
@@ -75,6 +79,16 @@
     # Get the args passed in parameter
     args = parser.parse_args()
 
+    extra_files_path = args.extra_files_path
+    toolDirectory = args.directory
+
+    #### Logging management ####
+    # If we are in Debug mode, also print in stdout the debug dump
+
+    configure_logger(extra_files_path=extra_files_path, debug=args.debug_mode)
+
+    #### END Logging management ####
+
     array_inputs_reference_genome = json.loads(args.fasta)
 
     # TODO: Replace these with the object Fasta
@@ -100,7 +114,6 @@
     array_inputs_bigwig = args.bigwig
 
     outputFile = args.output
-    json_inputs_data = args.data_json
 
     json_inputs_data = args.data_json
 
@@ -108,10 +121,6 @@
     # We remove the spaces in ["name"] of inputs_data
     sanitize_name_inputs(inputs_data)
 
-    if args.directory:
-        toolDirectory = args.directory
-    if args.extra_files_path:
-        extra_files_path = args.extra_files_path
 
     # TODO: Check here all the binaries / tools we need. Exception if missing
 
@@ -141,9 +150,7 @@
     # We terminate le process and so create a HTML file summarizing all the files
     trackHub.terminate()
 
-    print "\t"
-    print "--------------"
-    print "Well done guys! Your data are ready to be displayed in UCSC Track Hub."
+    logging.debug('#### End of HubArchiveCreator Debug Mode: Bye! ####')
 
     sys.exit(0)
 
@@ -184,5 +191,87 @@
                 datatype_dictionary.update({data_value["order_index"]: extensionObject})
     return datatype_dictionary
 
+def configure_logger(extra_files_path=None, debug=False):
+    if not extra_files_path:
+        raise Exception("Extra files path is not set. Stopping the application")
+
+
+    # All case log: log everything in a .log file
+    logger_file_name = ''.join([__name__, '.log'])
+    logging_file_path = os.path.join(extra_files_path, logger_file_name)
+
+    logging.basicConfig(filename=logging_file_path, level=logging.DEBUG)
+
+    log_stdout = logging.StreamHandler(sys.stdout)
+    if not debug:
+        configure_logger_user(log_stdout)
+    else:
+        configure_logger_dev(log_stdout)
+
+    # stderr configuration
+    configure_logger_stderr()
+
+    logging.debug('#### Welcome in HubArchiveCreator Debug Mode ####\n')
+
+def configure_logger_user(log_stdout=None):
+    """
+    User Logger is defined as following:
+        - User needs to have WARN, ERROR and CRITICAL but well formatted / without traceback
+            in STDOUT
+        - Still access to full, brute and traceback for errors
+            in STDERR
+        - And further access to debug if needed
+            in .log
+    :return:
+    """
+    if not log_stdout:
+        raise Exception("No log_stdout given. Stopping the application")
+
+    # stdout for INFO / WARN / ERROR / CRITICAL
+    log_stdout.setLevel(logging.INFO)
+
+    formatter = TraceBackFormatter('%(message)s')
+
+    log_stdout.setFormatter(formatter)
+
+    logging.getLogger().addHandler(log_stdout)
+
+def configure_logger_dev(log_stdout=None):
+    """
+    Dev Logger is defined as following:
+        - Dev needs to have WARN, ERROR and CRITICAL but well formatted / without traceback, in stdout
+        - Still access to full, brute and traceback in stderr for errors
+        - And further access to debug if needed
+    :return:
+    """
+    if not log_stdout:
+        raise Exception("No log_stdout given. Stopping the application")
+    log_format = '%(message)s'
+
+    # stdout and stderr and both identical for INFO / WARN / ERROR / CRITICAL
+    log_stdout.setLevel(logging.DEBUG)
+
+    formatter = logging.Formatter(log_format)
+
+    log_stdout.setFormatter(formatter)
+
+    logging.getLogger().addHandler(log_stdout)
+
+def configure_logger_stderr():
+    """
+    Configure what should be logged in stderr
+    :return:
+    """
+    log_error = logging.StreamHandler(sys.stderr)
+    log_error.setLevel(logging.ERROR)
+    log_error_format = '%(message)s'
+
+    formatter_error = logging.Formatter(log_error_format)
+
+    log_error.setFormatter(formatter_error)
+
+    logging.getLogger().addHandler(log_error)
+
 if __name__ == "__main__":
+    logging.getLogger(__name__)
     main(sys.argv)