import re


def parse_version(version):
    """ Parses a string, such as `'9.0-20160731'` or `'8.0-5.5'` into a 
        dictionary with keys, `major`, `minor`, and `patch`. """
    
    version = str(version)
    pattern = '(?:MarkLogic-(?:RHEL\d-)?)?(\d{1,2}\.\d(?:-(?:(?:\d{8})|(?:\d{1,2}(?:\.\d{1,2})?)))?)(?:[\.\-](?:x86_|amd)64)?'
    match = re.findall(pattern, version)
    if 1 != len(match):
        raise Exception(version + ' doesn’t match a MarkLogic artifact')
    mm_patch = match[0].split('-')
    mm = mm_patch[0]
    patch = None
    if len(mm_patch) > 1:
        patch = mm_patch[1]
    mm_tokens = mm.split('.')
    major = int(mm_tokens[0])
    minor = None
    if len(mm_tokens) > 1:
        minor = int(mm_tokens[1])
    return {'major': str(major), 'minor': str(minor), 'patch': patch}