Skip to content

medcat.utils.download_scripts

This module is designed to identify and download the medcat-scripts.

It will link the current setup (i.e medcat version) into account and subsequently identify and download the medcat-scripts based on the most recent applicable tag. So if you've got medcat==2.2.0, it might grab medcat/v2.2.3 for instance.

Functions:

  • fetch_scripts

    Download the latest compatible medcat-scripts folder into.

  • main

Attributes:

DOWNLOAD_URL_TEMPLATE module-attribute

DOWNLOAD_URL_TEMPLATE = f'https://api.github.com/repos/{GITHUB_REPO}/zipball/{tag}'

EXPECTED_TAG_PREFIX module-attribute

EXPECTED_TAG_PREFIX = 'medcat/v'

GITHUB_REPO module-attribute

GITHUB_REPO = 'CogStack/cogstack-nlp'

SCRIPTS_PATH module-attribute

SCRIPTS_PATH = 'medcat-scripts/'

logger module-attribute

logger = getLogger(__name__)

fetch_scripts

fetch_scripts(destination: str | Path = '.', overwrite_url: str | None = None, overwrite_tag: str | None = None) -> Path

Download the latest compatible medcat-scripts folder into.

Parameters:

  • destination

    (str | Path, default: '.' ) –

    The destination path. Defaults to ".".

  • overwrite_url

    (str | None, default: None ) –

    The overwrite URL. Defaults to None.

  • overwrite_tag

    (str | None, default: None ) –

    The overwrite tag. Defaults to None.

Returns:

  • Path ( Path ) –

    The path of the scripts.

Source code in medcat-v2/medcat/utils/download_scripts.py
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
def fetch_scripts(destination: str | Path = ".",
                  overwrite_url: str | None = None,
                  overwrite_tag: str | None = None) -> Path:
    """Download the latest compatible medcat-scripts folder into.

    Args:
        destination (str | Path): The destination path. Defaults to ".".
        overwrite_url (str | None): The overwrite URL. Defaults to None.
        overwrite_tag (str | None): The overwrite tag. Defaults to None.

    Returns:
        Path: The path of the scripts.
    """
    dest = Path(destination).expanduser().resolve()
    dest.mkdir(parents=True, exist_ok=True)

    zip_url = _determine_url(overwrite_url, overwrite_tag)
    with tempfile.TemporaryDirectory() as tmp_dir:
        zip_path = os.path.join(tmp_dir, 'downloaded_scripts.zip')
        _download_zip(zip_url, zip_path)
        _extract_zip(dest, Path(zip_path))
    _fix_requirements(dest, _get_medcat_version())
    logger.info(
        "You also need to install the requiements by doing:\n"
        "%s -m pip install -r %s/requirements.txt",
        sys.executable, str(destination))
    return dest

main

main(*in_args: str)
Source code in medcat-v2/medcat/utils/download_scripts.py
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
def main(*in_args: str):
    parser = argparse.ArgumentParser(
        prog="python -m medcat download-scripts",
        description="Download medcat-scripts"
    )
    parser.add_argument("destination", type=str, default=".", nargs='?',
                        help="The destination folder for the scripts")
    parser.add_argument("--overwrite-url", type=str, default=None,
                        help="The URL to download and extract from. "
                             "This is expected to refer to a .zip file "
                             "that has a `medcat-scripts` folder.")
    parser.add_argument("--overwrite-tag", '-t', type=str, default=None,
                        help="The tag to use from GitHub")
    parser.add_argument("--log-level", type=str, default='INFO',
                        choices=["DEBUG", "INFO", "WARNING", "ERROR"],
                        help="The log level for fetching")
    args = parser.parse_args(in_args)
    log_level = args.log_level
    logger.setLevel(log_level)
    if not logger.handlers:
        logger.addHandler(logging.StreamHandler())
    fetch_scripts(args.destination, args.overwrite_url,
                  args.overwrite_tag)