Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
82 lines (69 sloc) 2.68 KB
# Copyright 2019 Red Hat
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
This module interfaces with dhall-lang
import json
from pathlib import Path
from subprocess import Popen, PIPE
from typing import Any, Dict, Optional, Union
DEFAULT_PATH = Path("~/.local/bin/dhall-to-json").expanduser()
Input = Union[Path, str]
Env = Optional[Dict[str, str]]
def _install() -> None:
# TODO: implement opt-out
import urllib.request
from hashlib import sha256
req = urllib.request.urlopen(DEFAULT_URL)
data =
digest = sha256(data).hexdigest()
if digest != DEFAULT_HASH:
raise RuntimeError(
f"{DEFAULT_URL}: expected '{DEFAULT_HASH}' got '{digest}")
DEFAULT_PATH.parent.mkdir(parents=True, exist_ok=True)
p = Popen(["tar", "-xjf", "-", "--strip-components=2",
"-C", str(DEFAULT_PATH.parent)],
if p.wait():
raise RuntimeError(f"{DEFAULT_URL}: couldn't extract")
def _load(input: Input, env: Env = None, debug: bool = False) -> Any:
cmd = [str(DEFAULT_PATH if DEFAULT_PATH.exists() else "dhall-to-json")]
if debug:
if env and not env.get('PATH'):
env['PATH'] = ':'.join(['/bin', '/usr/local/bin'])
if isinstance(input, str):
proc = Popen(
cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, env=env)
proc = Popen(
cmd + ["--file", str(input)], stdout=PIPE, stderr=PIPE, env=env)
stdout, stderr = proc.communicate()
if stderr:
raise RuntimeError(f"Dhall error:" + stderr.decode('utf-8'))
return json.loads(stdout.decode('utf-8'))
def load(input: Input, env: Env = None, debug: bool = False) -> Any:
return _load(input, env, debug)
except FileNotFoundError:
return _load(input, env, debug)
if __name__ == "__main__":
print(load('let x = "Hello dhall" in x'))
You can’t perform that action at this time.