first commit
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,46 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from typing import Any, Dict, List
|
||||
|
||||
try:
|
||||
import yaml
|
||||
|
||||
HAS_YAML = True
|
||||
except ImportError:
|
||||
HAS_YAML = False
|
||||
|
||||
__virtualname__ = "cli"
|
||||
|
||||
|
||||
def __virtual__(hub):
|
||||
if HAS_YAML:
|
||||
return True
|
||||
return (False, "PyYaml could not be loaded")
|
||||
|
||||
|
||||
def load(hub, path):
|
||||
try:
|
||||
with open(path, "rb") as fp_:
|
||||
ret = {}
|
||||
for line in fp_:
|
||||
ret.update(hub.config.render.cli.render(line))
|
||||
return ret
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
return {}
|
||||
|
||||
|
||||
def render(hub, val: List[str] or str) -> List[str]:
|
||||
"""
|
||||
Take the string and render it in json
|
||||
"""
|
||||
ret = []
|
||||
if isinstance(val, str):
|
||||
val = [val]
|
||||
for v in val:
|
||||
if "=" in v:
|
||||
key, v = v.split("=", maxsplit=1)
|
||||
ret.append({key: yaml.safe_load(v)})
|
||||
else:
|
||||
ret.append(yaml.safe_load(v))
|
||||
|
||||
return ret
|
||||
@@ -0,0 +1,42 @@
|
||||
# The render process for the cli does NOT use the rend project for a couple
|
||||
# of reasons
|
||||
# 1. Config needs to be very early in the startup, therefore it cannot use
|
||||
# asyncio, all rend funcs use asyncio
|
||||
# 2. Config will not allow for template wrapping as the render is just a
|
||||
# single command line render
|
||||
|
||||
|
||||
def process(hub, renderer, value):
|
||||
"""
|
||||
Take a renderer and a value, process it, and return the processed value
|
||||
|
||||
This is intended to load a string through the config render system
|
||||
"""
|
||||
return getattr(hub, f"config.render.{renderer}.render")(value)
|
||||
|
||||
|
||||
def pipe(hub, dpipe, data):
|
||||
"""
|
||||
Given a render pipe, render the given data
|
||||
"""
|
||||
for render in dpipe:
|
||||
if isinstance(render, bytes):
|
||||
render = render.decode()
|
||||
data = hub.config.render.init.process(render, data)
|
||||
return data
|
||||
|
||||
|
||||
def load_file(hub, renderer, fn):
|
||||
"""
|
||||
Load up a file with the passed renderer unless the file contains a
|
||||
renderer she-bang line
|
||||
"""
|
||||
with open(fn, "rb") as rfh:
|
||||
data = rfh.read()
|
||||
if not data:
|
||||
return {}
|
||||
if data.startswith(b"#!"):
|
||||
dpipe = data[2 : data.index(b"\n")].split(b"|")
|
||||
return hub.config.render.init.pipe(dpipe, data)
|
||||
else:
|
||||
return hub.config.render.init.process(renderer, data)
|
||||
@@ -0,0 +1,32 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Define the JSON loader interface
|
||||
"""
|
||||
|
||||
# Import python libs
|
||||
import json
|
||||
|
||||
__virtualname__ = "json"
|
||||
|
||||
|
||||
def __virtual__(hub):
|
||||
return True
|
||||
|
||||
|
||||
def load(hub, path):
|
||||
"""
|
||||
Use json to read in a file
|
||||
"""
|
||||
try:
|
||||
with open(path, "r") as fp_:
|
||||
ret = json.loads(fp_.read())
|
||||
return ret
|
||||
except FileNotFoundError:
|
||||
return {}
|
||||
|
||||
|
||||
def render(hub, val):
|
||||
"""
|
||||
Take the string and render it in json
|
||||
"""
|
||||
return json.loads(val)
|
||||
@@ -0,0 +1,40 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Define the yaml loader interface
|
||||
"""
|
||||
|
||||
# Import third party libs
|
||||
try:
|
||||
import toml
|
||||
|
||||
HAS_TOML = True
|
||||
except ImportError:
|
||||
HAS_TOML = False
|
||||
|
||||
__virtualname__ = "toml"
|
||||
# __contracts__ = [__virtualname__]
|
||||
|
||||
|
||||
def __virtual__(hub):
|
||||
if HAS_TOML:
|
||||
return True
|
||||
return (False, "TOML could not be loaded")
|
||||
|
||||
|
||||
def load(hub, path):
|
||||
"""
|
||||
use toml to read in a file
|
||||
"""
|
||||
try:
|
||||
with open(path, "rb") as fp_:
|
||||
return toml.load(fp_.read())
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
return {}
|
||||
|
||||
|
||||
def render(hub, val):
|
||||
"""
|
||||
Take the string and render it in json
|
||||
"""
|
||||
return toml.loads(val)
|
||||
@@ -0,0 +1,39 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Define the yaml loader interface
|
||||
"""
|
||||
|
||||
# Import third party libs
|
||||
try:
|
||||
import yaml
|
||||
|
||||
HAS_YAML = True
|
||||
except ImportError:
|
||||
HAS_YAML = False
|
||||
|
||||
__virtualname__ = "yaml"
|
||||
|
||||
|
||||
def __virtual__(hub):
|
||||
if HAS_YAML:
|
||||
return True
|
||||
return (False, "PyYaml could not be loaded")
|
||||
|
||||
|
||||
def load(hub, path):
|
||||
"""
|
||||
use yaml to read in a file
|
||||
"""
|
||||
try:
|
||||
with open(path, "rb") as fp_:
|
||||
return yaml.safe_load(fp_.read())
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
return {}
|
||||
|
||||
|
||||
def render(hub, val):
|
||||
"""
|
||||
Take the string and render it in json
|
||||
"""
|
||||
return yaml.safe_load(val)
|
||||
Reference in New Issue
Block a user