def run():
    """
    Main process that\:
     * Parse command-line arguments,
     * Parse configuration file,
     * Initiates logger,
     * Check GitHub permissions,
     * Check Handle Service connection,
     * Run the issue action.

    """
    # Get command-line arguments
    args = get_args()
    # init logging
    if args.v and args.log is not None:
        init_logging(args.log, level='DEBUG')
    elif args.log is not None:
        init_logging(args.log)
    else:
        init_logging(None)

    # Run command
    # Retrieve command has a slightly different behavior from the rest so it's singled out
    if args.command != 'retrieve':
        issue_file = _get_issue(args.issue)
        dataset_file = _get_datasets(args.dsets)
        print(args.issue, issue_file)
        process_command(args.command, issue_file, dataset_file, args.issue, args.dsets)

    elif args.command == 'retrieve':
        if args.id is not None:
            list_of_ids = args.id
            # In the case the user is requesting more than one issue
            for directory in [args.issues, args.dsets]:
                # Added the '.' test to avoid creating directories that are intended to be files.
                if not os.path.exists(directory) and '.' not in directory:
                    os.makedirs(directory)
                # This tests whether a list of ids is provided with a directory where to dump the retrieved
                # issues and related datasets.
                if len(list_of_ids) > 1 and not os.path.isdir(directory):
                    print('You have provided multiple ids but a single file as destination, aborting.')
                    sys.exit(1)
            # Looping over list of ids provided
            for n in list_of_ids:
                local_issue = LocalIssue(None, None, None, None, args.command)
                local_issue.retrieve(n, args.issues, args.dsets)
        else:
            # TODO provide possibility to flush database contents from a dedicated webservice?
            pass
def process_command(command, issue_file, dataset_file, issue_path, dataset_path):
    payload = issue_file
    if dataset_file is not None:
        dsets = _get_datasets(dataset_file)
    else:
        dsets = None
    if command == 'create':
        payload['uid'] = str(uuid4())
        payload['workflow'] = unicode('new')
        payload['dateCreated'] = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')
    local_issue = LocalIssue(payload, dsets, issue_path, dataset_path, command)
    local_issue.validate(command)
    if command == 'create':
        local_issue.create()
    elif command == 'update':
        local_issue.update()
    elif command == 'close':
        local_issue.close()
def process_command(command,
                    issue_file=None,
                    dataset_file=None,
                    issue_path=None,
                    dataset_path=None,
                    status=None,
                    list_of_ids=None,
                    **kwargs):
    payload = issue_file

    # Fill in mandatory fields
    if command in [CREATE, UPDATE, CLOSE]:
        if 'passphrase' in kwargs:
            credentials = _authenticate(passphrase=kwargs['passphrase'])
        else:
            credentials = _authenticate()
        # Initializing non-mandatory fields to pass validation process.
        if URL not in payload.keys():
            payload[URL] = ''
        if MATERIALS not in payload.keys():
            payload[MATERIALS] = []
    if command == CREATE:
        payload[UID] = str(uuid4())
        payload[STATUS] = unicode(STATUS_NEW)
        payload[DATE_CREATED] = datetime.utcnow().strftime(TIME_FORMAT)
        payload[DATE_UPDATED] = payload[DATE_CREATED]

    local_issue = LocalIssue(action=command,
                             issue_file=payload,
                             dataset_file=dataset_file,
                             issue_path=issue_path,
                             dataset_path=dataset_path)
    if command not in [RETRIEVE, RETRIEVE_ALL]:
        local_issue.validate(command)
    # WS Call
    if command == CREATE:
        local_issue.create(credentials)
    elif command == UPDATE:
        local_issue.update(credentials)
    elif command == CLOSE:
        local_issue.close(credentials, status)
    elif command == RETRIEVE:
        local_issue.retrieve(list_of_ids, issue_path, dataset_path)
    elif command == RETRIEVE_ALL:
        local_issue.retrieve_all(issue_path, dataset_path)
def process_command(command, issue_file=None, dataset_file=None, issue_path=None, dataset_path=None, status=None,
                    list_of_ids=None, **kwargs):
    payload = issue_file

    if command in [CREATE, UPDATE, CLOSE]:
        credentials = _get_credentials(kwargs)
        # Initializing non-mandatory fields to pass validation process.
        if URL not in payload.keys():
            payload[URL] = []
        if MATERIALS not in payload.keys():
            payload[MATERIALS] = []
    # intializing mandatory new issue fields
    if command == CREATE:
        payload[UID] = str(uuid4())
        payload[STATUS] = unicode(STATUS_NEW)

    # instatiating a localissue object
    local_issue = LocalIssue(action=command, issue_file=payload, dataset_file=dataset_file, issue_path=issue_path,
                             dataset_path=dataset_path)

    # issue file validation
    if command not in [RETRIEVE, RETRIEVE_ALL]:
        local_issue.validate(command)
    # WS Call
    if command == CREATE:
        local_issue.create(credentials)
    elif command == UPDATE:
        local_issue.update(credentials)
    elif command == CLOSE:
        local_issue.close(credentials, status)
    elif command == RETRIEVE:
        local_issue.retrieve(list_of_ids, issue_path, dataset_path)
    elif command == RETRIEVE_ALL:
        local_issue.retrieve_all(issue_path, dataset_path)