Switch to unified view

a b/medacy/tools/get_metamap.py
1
import json
2
import os
3
4
_this_dir = os.path.dirname(__file__)
5
_config_path = os.path.join(_this_dir, "../../config.json")
6
7
8
def _validate_path(path):
9
    """Raises an error if the provided path is not valid"""
10
    if not os.path.isfile(path):
11
        raise FileNotFoundError("The MetaMap path provided is not a file")
12
    if not path.endswith("metamap"):
13
        raise ValueError("The name of this file is not 'metamap', therefore it cannot be the MetaMap binary")
14
15
16
def get_metamap():
17
    """
18
    Gets the path to MetaMap from the config json and returns it, or prompts
19
    the user to specify the MetaMap path if it has not been set for this installation
20
    """
21
    with open(_config_path, 'rb') as f:
22
        config_data = json.load(f)
23
24
    mm_path = config_data['metamap_path']
25
26
    # 0 is the default value, indicating that the path has never been specified
27
    if mm_path == 0:
28
        print("The path to MetaMap has not been specified for this installation")
29
        new_path = input("Please specify path to the MetaMap binary: ")
30
31
        _validate_path(new_path)
32
33
        config_data['metamap_path'] = new_path
34
35
        with open(_config_path, 'w') as f:
36
            json.dump(config_data, f)
37
38
        mm_path = new_path
39
40
    elif not os.path.isfile(mm_path):
41
        new_path = input("MetaMap was not found at the given location; please specify a new path: ")
42
        _validate_path(new_path)
43
        mm_path = new_path
44
45
    return mm_path
46
47
48
def get_metamap_path():
49
    """Returns the path to the MetaMap binary, or 0 if it has not been set."""
50
    with open(_config_path, 'rb') as f:
51
        config_data = json.load(f)
52
53
    return config_data['metamap_path']