Hug API Development Server
(
file: "A Python file that contains a Hug API" = None,
module: "A Python module that contains a Hug API" = None,
host: "Interface to bind to" = "",
port: number = 8000,
no_404_documentation: boolean = False,
manual_reload: boolean = False,
interval: number = 1,
command: "Run a command defined in the given module" = None,
silent: boolean = False,
)
| 44 | |
| 45 | @cli(version=current) |
| 46 | def hug( |
| 47 | file: "A Python file that contains a Hug API" = None, |
| 48 | module: "A Python module that contains a Hug API" = None, |
| 49 | host: "Interface to bind to" = "", |
| 50 | port: number = 8000, |
| 51 | no_404_documentation: boolean = False, |
| 52 | manual_reload: boolean = False, |
| 53 | interval: number = 1, |
| 54 | command: "Run a command defined in the given module" = None, |
| 55 | silent: boolean = False, |
| 56 | ): |
| 57 | """Hug API Development Server""" |
| 58 | api_module = None |
| 59 | if file and module: |
| 60 | print("Error: can not define both a file and module source for Hug API.") |
| 61 | sys.exit(1) |
| 62 | if file: |
| 63 | sys.path.append(os.path.dirname(os.path.abspath(file))) |
| 64 | sys.path.append(os.getcwd()) |
| 65 | api_module = importlib.machinery.SourceFileLoader(file.split(".")[0], file).load_module() |
| 66 | elif module: |
| 67 | sys.path.append(os.getcwd()) |
| 68 | api_module = importlib.import_module(module) |
| 69 | if not api_module or not hasattr(api_module, "__hug__"): |
| 70 | print("Error: must define a file name or module that contains a Hug API.") |
| 71 | sys.exit(1) |
| 72 | |
| 73 | api = API(api_module, display_intro=not silent) |
| 74 | if command: |
| 75 | if command not in api.cli.commands: |
| 76 | print(str(api.cli)) |
| 77 | sys.exit(1) |
| 78 | |
| 79 | flag_index = (sys.argv.index("-c") if "-c" in sys.argv else sys.argv.index("--command")) + 1 |
| 80 | sys.argv = sys.argv[flag_index:] |
| 81 | api.cli.commands[command]() |
| 82 | return |
| 83 | |
| 84 | ran = False |
| 85 | if not manual_reload: |
| 86 | thread.start_new_thread(reload_checker, (interval,)) |
| 87 | while True: |
| 88 | reload_checker.reloading = False |
| 89 | time.sleep(1) |
| 90 | try: |
| 91 | _start_api( |
| 92 | api_module, host, port, no_404_documentation, False if silent else not ran |
| 93 | ) |
| 94 | except KeyboardInterrupt: |
| 95 | if not reload_checker.reloading: |
| 96 | sys.exit(1) |
| 97 | reload_checker.reloading = False |
| 98 | ran = True |
| 99 | for name in list(sys.modules.keys()): |
| 100 | if name not in INIT_MODULES: |
| 101 | del sys.modules[name] |
| 102 | if file: |
| 103 | api_module = importlib.machinery.SourceFileLoader( |
nothing calls this directly
no test coverage detected