Produce a new :class:`.ScriptDirectory` given a :class:`.Config` instance. The :class:`.Config` need only have the ``script_location`` key present.
(cls, config: Config)
| 159 | |
| 160 | @classmethod |
| 161 | def from_config(cls, config: Config) -> ScriptDirectory: |
| 162 | """Produce a new :class:`.ScriptDirectory` given a :class:`.Config` |
| 163 | instance. |
| 164 | |
| 165 | The :class:`.Config` need only have the ``script_location`` key |
| 166 | present. |
| 167 | |
| 168 | """ |
| 169 | script_location = config.get_alembic_option("script_location") |
| 170 | if script_location is None: |
| 171 | raise util.CommandError( |
| 172 | "No 'script_location' key found in configuration." |
| 173 | ) |
| 174 | truncate_slug_length: Optional[int] |
| 175 | tsl = config.get_alembic_option("truncate_slug_length") |
| 176 | if tsl is not None: |
| 177 | truncate_slug_length = int(tsl) |
| 178 | else: |
| 179 | truncate_slug_length = None |
| 180 | |
| 181 | prepend_sys_path = config.get_prepend_sys_paths_list() |
| 182 | if prepend_sys_path: |
| 183 | sys.path[:0] = prepend_sys_path |
| 184 | |
| 185 | rvl = config.get_alembic_boolean_option("recursive_version_locations") |
| 186 | return ScriptDirectory( |
| 187 | util.coerce_resource_to_filename(script_location), |
| 188 | file_template=config.get_alembic_option( |
| 189 | "file_template", _default_file_template |
| 190 | ), |
| 191 | truncate_slug_length=truncate_slug_length, |
| 192 | sourceless=config.get_alembic_boolean_option("sourceless"), |
| 193 | output_encoding=config.get_alembic_option( |
| 194 | "output_encoding", "utf-8" |
| 195 | ), |
| 196 | version_locations=config.get_version_locations_list(), |
| 197 | timezone=config.get_alembic_option("timezone"), |
| 198 | hooks=config.get_hooks_list(), |
| 199 | recursive_version_locations=rvl, |
| 200 | messaging_opts=config.messaging_opts, |
| 201 | ) |
| 202 | |
| 203 | @contextmanager |
| 204 | def _catch_revision_errors( |