Use package_data and an initial config file.

This commit is contained in:
Richard Kiss 2020-03-28 21:34:16 -07:00
parent 05906e1a13
commit 6868839a74
6 changed files with 24 additions and 9 deletions

View File

@ -14,7 +14,7 @@ dependencies = [
"chiabip158", # bip158-style wallet filters
"chiapos", # proof of space
"sortedcontainers",
"websockets", #websockets
"websockets",
]
dev_dependencies = [
"pytest",
@ -62,6 +62,7 @@ setup(
"generate-chia-keys = src.cmds.generate_keys:main",
]
},
package_data={"src.util": ["initial-*.yaml"], "src.server": ["dummy.crt", "dummy.key"],},
use_scm_version={"fallback_version": "unknown-no-.git-directory"},
long_description=open("README.md").read(),
zip_safe=False,

View File

@ -1,6 +1,7 @@
import asyncio
import concurrent
import logging
import pkg_resources
import random
import ssl
from secrets import token_bytes
@ -8,9 +9,7 @@ from typing import Any, AsyncGenerator, List, Optional, Tuple, Dict
from aiter import aiter_forker, iter_to_aiter, join_aiters, map_aiter, push_aiter
from aiter.server import start_server_aiter
from yaml import safe_load
from src.definitions import ROOT_DIR
from src.protocols.shared_protocol import (
Handshake,
HandshakeAck,
@ -23,6 +22,7 @@ from src.server.outbound_message import Delivery, Message, NodeType, OutboundMes
from src.types.peer_info import PeerInfo
from src.types.sized_bytes import bytes32
from src.util import partial_func
from src.util.config import load_config
from src.util.errors import (
IncompatibleProtocolVersion,
InvalidAck,
@ -33,8 +33,7 @@ from src.util.ints import uint16
from src.util.network import create_node_id
import traceback
config_filename = ROOT_DIR / "config" / "config.yaml"
config = safe_load(open(config_filename, "r"))
config = load_config("config.yaml")
class ChiaServer:
@ -86,7 +85,9 @@ class ChiaServer:
)
except Exception:
pass
return "ssl/dummy.crt", "ssl/dummy.key", "1234", None
dummy_crt = pkg_resources.resource_filename(__name__, "dummy.crt")
dummy_key = pkg_resources.resource_filename(__name__, "dummy.key")
return dummy_crt, dummy_key, "1234", None
async def start_server(
self, host: str, on_connect: OnConnectFunc = None, config: Dict = None,

View File

@ -1,15 +1,28 @@
import yaml
import argparse
import pathlib
import pkg_resources
from typing import Dict, Any, Callable, Optional
from src.definitions import ROOT_DIR
def migrate_config_file(filename: str, path: pathlib.Path) -> None:
default_config_file = pkg_resources.resource_string(
__name__, f"initial-{filename}"
).decode()
path.parent.mkdir(parents=True, exist_ok=True)
with open(path, "w") as f:
f.write(default_config_file)
def load_config(filename: str, sub_config: Optional[str] = None) -> Dict:
config_filename = ROOT_DIR / "config" / filename
path = ROOT_DIR / "config" / filename
if not path.is_file():
migrate_config_file(filename, path)
if sub_config is not None:
return yaml.safe_load(open(config_filename, "r"))[sub_config]
return yaml.safe_load(open(path, "r"))[sub_config]
else:
return yaml.safe_load(open(config_filename, "r"))
return yaml.safe_load(open(path, "r"))
def load_config_cli(filename: str, sub_config: Optional[str] = None) -> Dict: