mirror of https://github.com/scrapy/scrapy.git
improve render_templatefile
This commit is contained in:
parent
24d6ac1f52
commit
12a26755ae
|
|
@ -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")
|
||||
|
|
|
|||
|
|
@ -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]')
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in New Issue