diff --git a/scrapy/commands/startproject.py b/scrapy/commands/startproject.py index 4323cdb53..9e1e95ab1 100644 --- a/scrapy/commands/startproject.py +++ b/scrapy/commands/startproject.py @@ -109,7 +109,7 @@ class Command(ScrapyCommand): move(project_dir / 'module', project_dir / project_name) for paths in TEMPLATES_TO_RENDER: tplfile = Path(project_dir, *(string.Template(s).substitute(project_name=project_name) for s in paths)) - render_templatefile(str(tplfile), project_name=project_name, ProjectName=string_camelcase(project_name)) + render_templatefile(tplfile, project_name=project_name, ProjectName=string_camelcase(project_name)) print(f"New Scrapy project '{project_name}', using template directory " f"'{self.templates_dir}', created in:") print(f" {project_dir.resolve()}\n") diff --git a/scrapy/utils/template.py b/scrapy/utils/template.py index 8075902b3..2177817d9 100644 --- a/scrapy/utils/template.py +++ b/scrapy/utils/template.py @@ -1,21 +1,24 @@ """Helper functions for working with templates""" +from os import PathLike import re import string from pathlib import Path +from typing import Union -def render_templatefile(path: str, **kwargs): - raw = Path(path).read_text('utf8') +def render_templatefile(path: Union[str, PathLike], **kwargs): + path_obj = Path(path) + raw = path_obj.read_text('utf8') content = string.Template(raw).substitute(**kwargs) - render_path = path[:-len('.tmpl')] if path.endswith('.tmpl') else path + render_path = path_obj.with_suffix('') if path_obj.suffix == '.tmpl' else path_obj - if path.endswith('.tmpl'): - Path(path).rename(render_path) + if path_obj.suffix == '.tmpl': + path_obj.rename(render_path) - Path(render_path).write_text(content, 'utf8') + render_path.write_text(content, 'utf8') CAMELCASE_INVALID_CHARS = re.compile(r'[^a-zA-Z\d]') diff --git a/tests/test_utils_template.py b/tests/test_utils_template.py index b1aca5ed3..4f1e8772b 100644 --- a/tests/test_utils_template.py +++ b/tests/test_utils_template.py @@ -28,7 +28,7 @@ class UtilsRenderTemplateFileTestCase(unittest.TestCase): template_path.write_text(template, encoding='utf8') assert template_path.is_file() # Failure of test itself - render_templatefile(str(template_path), **context) + render_templatefile(template_path, **context) self.assertFalse(template_path.exists()) self.assertEqual(render_path.read_text(encoding='utf8'), rendered)