mirror of
https://github.com/niess/python-appimage.git
synced 2026-03-14 12:20:14 +01:00
Add an app builder
This commit is contained in:
41
python_appimage/utils/template.py
Normal file
41
python_appimage/utils/template.py
Normal file
@@ -0,0 +1,41 @@
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
|
||||
from .fs import make_tree
|
||||
from .log import debug
|
||||
|
||||
|
||||
__all__ = ['copy_template', 'load_template']
|
||||
|
||||
|
||||
_template_pattern = re.compile('[{][{][ ]*([^{} ]+)[ ]*[}][}]')
|
||||
|
||||
|
||||
def load_template(path, **kwargs):
|
||||
'''Load a template file and substitue keywords
|
||||
'''
|
||||
with open(path) as f:
|
||||
template = f.read()
|
||||
|
||||
def matcher(m):
|
||||
tag = m.group(1)
|
||||
try:
|
||||
return kwargs[tag]
|
||||
except KeyError:
|
||||
return tag
|
||||
|
||||
return _template_pattern.sub(matcher, template)
|
||||
|
||||
|
||||
def copy_template(path, destination, **kwargs):
|
||||
'''Copy a template file and substitue keywords
|
||||
'''
|
||||
txt = load_template(path, **kwargs)
|
||||
|
||||
debug('COPY', '%s as %s', os.path.basename(path), destination)
|
||||
make_tree(os.path.dirname(destination))
|
||||
with open(destination, 'w') as f:
|
||||
f.write(txt)
|
||||
|
||||
shutil.copymode(path, destination)
|
||||
Reference in New Issue
Block a user