Files
cibuildwheel/test/template_projects/base.py
T

35 lines
935 B
Python
Raw Normal View History

import os
import io
2020-05-02 09:59:55 +01:00
import jinja2
2020-05-02 22:50:52 +01:00
from typing import Union, Dict, Any
2020-05-02 09:59:55 +01:00
FilesDict = Dict[str, Union[str, jinja2.Template]]
2020-05-02 16:54:19 +01:00
TemplateContext = Dict[str, Any]
class TemplateProject:
2020-05-02 09:59:55 +01:00
files: FilesDict
2020-05-02 16:54:19 +01:00
template_context: TemplateContext
2020-05-02 16:54:19 +01:00
def __init__(self):
self.files = {}
self.template_context = {}
2020-05-02 16:54:19 +01:00
def generate(self, path: str):
2020-05-02 09:59:55 +01:00
for filename, content in self.files.items():
file_path = os.path.join(path, filename)
os.makedirs(os.path.dirname(file_path), exist_ok=True)
with io.open(file_path, 'w', encoding='utf8') as f:
2020-05-02 09:59:55 +01:00
if isinstance(content, jinja2.Template):
2020-05-02 16:54:19 +01:00
content = content.render(self.template_context)
2020-05-02 09:59:55 +01:00
f.write(content)
2020-05-03 10:30:44 +01:00
def copy(self):
other = TemplateProject()
other.files = self.files.copy()
other.template_context = self.template_context.copy()
return other