File: cli.py

package info (click to toggle)
sqlglot 27.6.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 96,288 kB
  • sloc: python: 73,347; sql: 20,230; javascript: 40; makefile: 39
file content (38 lines) | stat: -rwxr-xr-x 1,328 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python3

from importlib import import_module
from pathlib import Path
from unittest import mock

from pdoc.__main__ import cli, parser

# Need this import or else import_module doesn't work
import sqlglot  # noqa
from sqlglot.dialects import *

# Load all dialects up front because lazy loading breaks pdoc's dynamic importing
sqlglot.dialects.__all__ = [globals()[attr_name] for attr_name in sqlglot.dialects.__all__]


def mocked_import(*args, **kwargs):
    """Return a MagicMock if import fails for any reason"""
    try:
        return import_module(*args, **kwargs)
    except Exception:
        mocked_module = mock.MagicMock()
        mocked_module.__name__ = args[0]
        return mocked_module


if __name__ == "__main__":
    # Mock uninstalled dependencies so pdoc can still work
    with mock.patch("importlib.import_module", side_effect=mocked_import):
        opts = parser.parse_args()
        opts.docformat = "google"
        opts.modules = ["sqlglot"]
        opts.footer_text = "Copyright (c) 2023 Toby Mao"
        opts.template_directory = Path(__file__).parent.joinpath("templates").absolute()
        opts.edit_url = ["sqlglot=https://github.com/tobymao/sqlglot/tree/main/sqlglot/"]

        with mock.patch("pdoc.__main__.parser", **{"parse_args.return_value": opts}):
            cli()