1. Alexander Dudko
  2. jira-bulk-loader

Commits

Alexander Dudko  committed bd94409

argsparse moved to separate module & switched to io.open

  • Participants
  • Parent commits 48b1801
  • Branches master
  • Tags 0.1.7

Comments (0)

Files changed (3)

File bin/jira-bulk-loader.py

View file
 #!/usr/bin/python
 #-*- coding: UTF-8 -*-
 
-import argparse
 from jirabulkloader.task_extractor import TaskExtractor
-from jirabulkloader.task_extractor_exceptions import TaskExtractorTemplateErrorProject, TaskExtractorJiraValidationError, TaskExtractorTemplateErrorJson, TaskExtractorJiraCreationError, TaskExtractorJiraHostProblem
-from jirabulkloader.jiraConnect import JiraConnectConnectionError
-
-prg_description="Uses template file to create many tasks in Jira at once"
-
-prg_epilog="""Report bugs to: <https://bitbucket.org/oktopuz/jira-bulk-loader/issues>
-Project home page: <https://bitbucket.org/oktopuz/jira-bulk-loader>"""
-
-parser = argparse.ArgumentParser(description=prg_description, formatter_class=argparse.RawDescriptionHelpFormatter, epilog=prg_epilog)
-
-parser.add_argument('template_file', help='file containing tasks definition')
-parser.add_argument('-W', dest='project', help='Project key')
-parser.add_argument('-R', dest='priority', help='Task priority. "Medium" by default', default="Medium")
-parser.add_argument('-D', dest='dueDate', help='dueDate  (YYYY-mm-DD). For example: 2012-05-31')
-parser.add_argument('--dry', dest='dry_run', action='store_true', help='Make a dry run. It checks everything but does not create tasks', default=False)
+import jirabulkloader.interface as iface
 
-mandatory = parser.add_argument_group('mandatory arguments')
-mandatory.add_argument('-H', dest='hostname', required=True, help='Jira hostname with http:// or https://')
-mandatory.add_argument('-U', dest='username', required=True, help='your Jira username')
-mandatory.add_argument('-P', dest='password', required=True, help='your Jira password')
+args = iface.get_options()
 
-args = parser.parse_args()
-
-
-##############################################################
-# open input file, parse and create tasks
-
-import codecs
 try:
-    f = codecs.open(args.template_file, encoding='utf-8')
-    input_text = f.read()
+    input_text = iface.get_template( args.template_file )
 except IOError as e:
     print "Template file error: %s" % e
     exit(1)
-else:
-    f.close()
 
 options = {}
 if args.dueDate: options['duedate'] = args.dueDate
 
 task_ext = TaskExtractor(args.hostname, args.username, args.password, options, dry_run = args.dry_run)
 
+from jirabulkloader.task_extractor_exceptions import TaskExtractorTemplateErrorProject, TaskExtractorJiraValidationError, TaskExtractorTemplateErrorJson, TaskExtractorJiraCreationError, TaskExtractorJiraHostProblem
+from jirabulkloader.jiraConnect import JiraConnectConnectionError
+
 try:
     print "Parsing task list.."
     tasks =  task_ext.load(input_text)

File jirabulkloader/interface.py

View file
+
+
+def get_options( args_list = None ):
+    """Create argparse structure and parse arguments"""
+
+    import argparse
+
+    parser = argparse.ArgumentParser( \
+        description="A command line tool to create issues in Atlassian JIRA via REST API.",
+        formatter_class=argparse.RawDescriptionHelpFormatter, \
+        epilog="Project home page: <https://bitbucket.org/oktopuz/jira-bulk-loader>",
+        )
+
+    parser.add_argument('template_file', help='a file containing issues definition')
+    parser.add_argument('--dry', dest='dry_run', action='store_true', \
+        help='make a dry run. It checks everything but does not create issues', default=False)
+
+    required = parser.add_argument_group('required arguments')
+    required.add_argument('-H', '--host',  required=True, help='JIRA hostname with http:// or https://')
+    required.add_argument('-U', '--user', required=True, help='your username')
+    required.add_argument('-P', dest='password', required=True, help='your password. You\'ll be prompted for it if not specified')
+
+    issue_attr = parser.add_argument_group('JIRA attributes')
+    issue_attr.add_argument('-W', '--project', help='project key')
+    issue_attr.add_argument('-R', '--priority', help="default issue priority. 'Medium' if not specified", default="Medium")
+    issue_attr.add_argument('-D', '--duedate', help='default issue dueDate (YYYY-mm-DD)')
+
+    return parser.parse_args( args_list )
+
+
+def get_template( template_filename ):
+    """Read template file using uft-8 encoding.
+    If there is no such file, IOError exception will be raised."""
+
+    import io
+
+    with io.open( template_filename, 'rt', encoding='utf-8' ) as f:
+        return f.read()
+
+

File jirabulkloader/test/test_interface.py

View file
+
+import pytest
+import jirabulkloader.interface as ji
+
+def test_args_parse_without_config_file():
+    assert vars(ji.get_options( ['-H', 'h1', '-U', 'u1', '-P', 'p1', 'f1'] )) == \
+        { 
+        'host' : 'h1', 
+        'user' : 'u1', 
+        'dry_run' : False, 
+        'priority' : 'Medium', 
+        'template_file' : 'f1',
+        'duedate' : None,
+        'password' : 'p1',
+        'project' : None,
+        }
+
+