Skip to content

Commit 68df91c

Browse files
committed
Implement prepend_sys_path_separator
1 parent 2383617 commit 68df91c

File tree

2 files changed

+46
-37
lines changed

2 files changed

+46
-37
lines changed

alembic/config.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,27 @@ def messaging_opts(self) -> MessagingOptions:
339339
),
340340
)
341341

342+
def get_separator_char(self, name: str) -> Optional[str]:
343+
separator = self.get_main_option(name)
344+
345+
split_on_path = {
346+
None: None,
347+
"space": " ",
348+
"newline": "\n",
349+
"os": os.pathsep,
350+
":": ":",
351+
";": ";",
352+
}
353+
354+
try:
355+
return split_on_path[separator]
356+
except KeyError as ke:
357+
raise ValueError(
358+
"'%s' is not a valid value for %s; "
359+
"expected 'space', 'newline', 'os', ':', ';'"
360+
% (separator, name)
361+
) from ke
362+
342363

343364
class MessagingOptions(TypedDict, total=False):
344365
quiet: bool

alembic/script/base.py

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -180,53 +180,41 @@ def from_config(cls, config: Config) -> ScriptDirectory:
180180
version_locations_str = config.get_main_option("version_locations")
181181
version_locations: Optional[List[str]]
182182
if version_locations_str:
183-
version_path_separator = config.get_main_option(
183+
split_char = config.get_separator_char(
184184
"version_path_separator"
185185
)
186186

187-
split_on_path = {
188-
None: None,
189-
"space": " ",
190-
"newline": "\n",
191-
"os": os.pathsep,
192-
":": ":",
193-
";": ";",
194-
}
195-
196-
try:
197-
split_char: Optional[str] = split_on_path[
198-
version_path_separator
199-
]
200-
except KeyError as ke:
201-
raise ValueError(
202-
"'%s' is not a valid value for "
203-
"version_path_separator; "
204-
"expected 'space', 'newline', 'os', ':', ';'"
205-
% version_path_separator
206-
) from ke
187+
if split_char is None:
188+
# legacy behaviour for backwards compatibility
189+
version_locations = _split_on_space_comma.split(
190+
version_locations_str
191+
)
207192
else:
208-
if split_char is None:
209-
# legacy behaviour for backwards compatibility
210-
version_locations = _split_on_space_comma.split(
211-
version_locations_str
212-
)
213-
else:
214-
version_locations = [
215-
x.strip()
216-
for x in version_locations_str.split(split_char)
217-
if x
218-
]
193+
version_locations = [
194+
x.strip()
195+
for x in version_locations_str.split(split_char)
196+
if x
197+
]
219198
else:
220199
version_locations = None
221200

222201
prepend_sys_path = config.get_main_option("prepend_sys_path")
223202
if prepend_sys_path:
224-
if os.name == 'nt':
225-
prepend_paths = _split_on_space_comma.split(prepend_sys_path)
203+
split_char = config.get_separator_char(
204+
"prepend_sys_path_separator"
205+
)
206+
207+
if split_char is None:
208+
# legacy behaviour for backwards compatibility
209+
sys.path[:0] = list(
210+
_split_on_space_comma_colon.split(prepend_sys_path)
211+
)
226212
else:
227-
prepend_paths = _split_on_space_comma_colon.split(prepend_sys_path)
228-
229-
sys.path[:0] = (os.path.normpath(path.strip()) for path in prepend_paths)
213+
sys.path[:0] = (
214+
path.strip()
215+
for path in prepend_sys_path.split(split_char)
216+
if path
217+
)
230218

231219
rvl = config.get_main_option("recursive_version_locations") == "true"
232220
return ScriptDirectory(

0 commit comments

Comments
 (0)