| 67 | self.app_name = self.app_name[:-2] |
| 68 | |
| 69 | def load(self) -> SanicApp: |
| 70 | module_path = os.path.abspath(self.cwd) |
| 71 | if module_path not in sys.path: |
| 72 | sys.path.append(module_path) |
| 73 | |
| 74 | if self.factory: |
| 75 | return self.factory() |
| 76 | else: |
| 77 | from sanic.app import Sanic |
| 78 | from sanic.simple import create_simple_server |
| 79 | |
| 80 | maybe_path = Path(self.module_input) |
| 81 | if self.as_simple or ( |
| 82 | maybe_path.is_dir() |
| 83 | and ("\\" in self.module_input or "/" in self.module_input) |
| 84 | ): |
| 85 | app = create_simple_server(maybe_path) |
| 86 | else: |
| 87 | implied_app_name = False |
| 88 | if not self.module_name and not self.app_name: |
| 89 | self.module_name = self.module_input |
| 90 | self.app_name = DEFAULT_APP_NAME |
| 91 | implied_app_name = True |
| 92 | module = import_module(self.module_name) |
| 93 | app = getattr(module, self.app_name, None) |
| 94 | if not app and implied_app_name: |
| 95 | raise ValueError( |
| 96 | "Looks like you only supplied a module name. Sanic " |
| 97 | "tried to locate an application instance named " |
| 98 | f"{self.module_name}:app, but was unable to locate " |
| 99 | "an application instance. Please provide a path " |
| 100 | "to a global instance of Sanic(), or a callable that " |
| 101 | "will return a Sanic() application instance." |
| 102 | ) |
| 103 | if self.as_factory or isfunction(app): |
| 104 | if not callable(app): |
| 105 | type_name = type(app).__name__ |
| 106 | raise ValueError( |
| 107 | f"Expected a callable, but got {type_name}." |
| 108 | ) |
| 109 | try: |
| 110 | app = app(self.args) |
| 111 | except TypeError: |
| 112 | app = app() |
| 113 | |
| 114 | app_type_name = type(app).__name__ |
| 115 | |
| 116 | if ( |
| 117 | not isinstance(app, Sanic) |
| 118 | and self.args |
| 119 | and hasattr(self.args, "target") |
| 120 | ): |
| 121 | with suppress(ModuleNotFoundError): |
| 122 | maybe_module = import_module(self.module_input) |
| 123 | app = getattr(maybe_module, "app", None) |
| 124 | if not app: |
| 125 | message = ( |
| 126 | "Module is not a Sanic app, " |