Finish template engine

This commit is contained in:
Adam Gausmann 2019-01-29 19:45:45 -06:00
parent 258dd8ae75
commit 8c71086242
4 changed files with 50 additions and 4 deletions

View file

@ -8,6 +8,7 @@ name = "pypi"
[packages]
mako = "*"
toml = "*"
[dev-packages]

10
Pipfile.lock generated
View file

@ -1,7 +1,7 @@
{
"_meta": {
"hash": {
"sha256": "5f2a777077fa545ffa7e39a4b62c35aae42497d7e4786940f3e4b79fac2c5737"
"sha256": "aa3152fd503bd5d4cad9dccb32c96b17ad9ba583118340e3a8c4d7ef86a5acf0"
},
"host-environment-markers": {
"implementation_name": "cpython",
@ -67,6 +67,14 @@
"sha256:4e97332c9ce444b0c2c38dd22ddc61c743eb208d916e4265a2a3b575bdccb1d3"
],
"version": "==1.1.0"
},
"toml": {
"hashes": [
"sha256:f1db651f9657708513243e61e6cc67d101a39bad662eaa9b5546f789338e07a3",
"sha256:235682dd292d5899d361a811df37e04a8828a5b1da3115886b73cf81ebc9100e",
"sha256:229f81c57791a41d65e399fc06bf0848bab550a9dfd5ed66df18ce5f05e73d5c"
],
"version": "==0.10.0"
}
},
"develop": {}

4
hosts.toml Normal file
View file

@ -0,0 +1,4 @@
[ayypad.i3status]
wireless = ["wlp4s0"]
ethernet = ["enp2s0"]
disks = ["/", "/home"]

View file

@ -2,10 +2,14 @@
import argparse
import os
import socket
import sys
from pathlib import Path
from mako.lookup import TemplateLookup
import mako.lookup
import mako.template
import toml
def main():
parser = argparse.ArgumentParser(
@ -20,16 +24,45 @@ def main():
)
parser.add_argument(
'-n', '--hostname',
help='The hostname or other identifying name of this system.',
default=os.environ.get('HOSTNAME'),
help='The hostname or other identifying name of this system that will'
' be used to retrieve the host-specific configuration.',
default=os.environ.get('HOSTNAME') or socket.gethostname(),
)
parser.add_argument(
'-o', '--home',
help='The home directory where generated dotfiles will be installed.',
type=Path,
default=os.environ.get('HOME') or Path.home(),
)
args = parser.parse_args()
templates_dir = args.dotfiles / 'templates'
include_dir = args.dotfiles / 'include'
with open(args.dotfiles / 'hosts.toml') as hosts_file:
hosts_config = toml.load(hosts_file)
lookup = mako.lookup.TemplateLookup(
directories=[
str(templates_dir),
str(include_dir),
],
)
for template_path in templates_dir.glob('**/*'):
if not template_path.is_file():
continue
template = mako.template.Template(
filename=str(template_path),
strict_undefined=True,
lookup=lookup,
)
output = template.render(
host=hosts_config[args.hostname]
)
output_path = args.home / template_path.relative_to(templates_dir)
with open(output_path, 'w+') as output_file:
output_file.write(output)
main()