add more logging
This commit is contained in:
parent
941b993287
commit
2b5eef9839
4 changed files with 34 additions and 3 deletions
|
@ -19,6 +19,7 @@ class ZonaBuilder:
|
|||
cli_root: Path | None = None,
|
||||
cli_output: Path | None = None,
|
||||
):
|
||||
logger.debug("Initializing ZonaBuilder.")
|
||||
self.layout: Layout = discover_layout(cli_root, cli_output)
|
||||
self.config: ZonaConfig = ZonaConfig.from_file(
|
||||
self.layout.root / "config.yml"
|
||||
|
@ -31,6 +32,7 @@ class ZonaBuilder:
|
|||
items: list[Item] = []
|
||||
|
||||
base = layout.root / layout.content
|
||||
logger.debug(f"Discovering content in {base}.")
|
||||
for path in base.rglob("*"):
|
||||
if path.is_file() and not util.should_ignore(
|
||||
path, patterns=self.config.ignore, base=base
|
||||
|
@ -45,6 +47,9 @@ class ZonaBuilder:
|
|||
if path.name.endswith(".md") and not path.is_relative_to(
|
||||
layout.root / "content" / "static"
|
||||
):
|
||||
logger.debug(
|
||||
f"Parsing {path.name} as Markdown document."
|
||||
)
|
||||
item.metadata, item.content = parse_metadata(path)
|
||||
if item.metadata.post == True:
|
||||
item.post = True
|
||||
|
|
|
@ -4,9 +4,10 @@ from pathlib import Path
|
|||
from zona import server
|
||||
from zona.builder import ZonaBuilder
|
||||
from zona.layout import initialize_site
|
||||
from zona.log import setup_logging
|
||||
from zona.log import setup_logging, get_logger
|
||||
|
||||
app = typer.Typer()
|
||||
logger = get_logger()
|
||||
|
||||
|
||||
@app.command()
|
||||
|
@ -26,6 +27,7 @@ def init(
|
|||
|
||||
Optionally specify the ROOT directory.
|
||||
"""
|
||||
logger.info("Initializing site...")
|
||||
initialize_site(root)
|
||||
|
||||
|
||||
|
|
|
@ -2,15 +2,21 @@ from dataclasses import dataclass, field
|
|||
from dacite import from_dict
|
||||
import yaml
|
||||
from pathlib import Path
|
||||
from zona.log import get_logger
|
||||
|
||||
logger = get_logger()
|
||||
|
||||
|
||||
def find_config(start: Path | None = None) -> Path | None:
|
||||
logger.debug("Searching for config file...")
|
||||
current = (start or Path.cwd()).resolve()
|
||||
|
||||
for parent in [current, *current.parents]:
|
||||
candidate = parent / "config.yml"
|
||||
if candidate.is_file():
|
||||
logger.debug(f"Config file {candidate} found.")
|
||||
return candidate
|
||||
logger.debug("Couldn't find config file.")
|
||||
return None
|
||||
|
||||
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
from pathlib import Path
|
||||
from rich import print
|
||||
import typer
|
||||
from dataclasses import dataclass, asdict
|
||||
from zona.config import ZonaConfig, find_config
|
||||
from zona import util
|
||||
from zona import util, log
|
||||
import yaml
|
||||
|
||||
logger = log.get_logger()
|
||||
|
||||
|
||||
@dataclass
|
||||
class Layout:
|
||||
|
@ -25,11 +26,14 @@ class Layout:
|
|||
output=(root / "public").resolve() if not output else output,
|
||||
)
|
||||
if validate:
|
||||
logger.debug("Validating site layout...")
|
||||
if not layout.content.is_dir():
|
||||
logger.error("Missing required content directory!")
|
||||
raise FileNotFoundError(
|
||||
"Missing required content directory!"
|
||||
)
|
||||
if not layout.templates.is_dir():
|
||||
logger.debug("Using default template directory.")
|
||||
# use the included defaults
|
||||
layout.templates = util.get_resource_dir("templates")
|
||||
|
||||
|
@ -40,19 +44,24 @@ def discover_layout(
|
|||
cli_root: Path | None = None, cli_output: Path | None = None
|
||||
) -> Layout:
|
||||
if cli_root:
|
||||
logger.debug("Using user provided site root.")
|
||||
root = cli_root
|
||||
else:
|
||||
logger.debug("Discovering site layout...")
|
||||
config = find_config(cli_root)
|
||||
if config:
|
||||
root = config.parent
|
||||
else:
|
||||
logger.debug("Using CWD as root.")
|
||||
root = Path.cwd()
|
||||
return Layout.from_input(root, cli_output)
|
||||
|
||||
|
||||
def initialize_site(root: Path | None = None):
|
||||
logger.info("Initializing site.")
|
||||
# initialize a new project
|
||||
if not root:
|
||||
logger.debug("No root provided; using CWD.")
|
||||
root = Path.cwd()
|
||||
root = root.absolute().resolve()
|
||||
config = find_config(root)
|
||||
|
@ -64,11 +73,15 @@ def initialize_site(root: Path | None = None):
|
|||
)
|
||||
)
|
||||
if ans:
|
||||
logger.debug("Unlinking config file.")
|
||||
config.unlink()
|
||||
# create requires layout
|
||||
logger.debug("Generating layout.")
|
||||
layout = Layout.from_input(root=root, validate=False)
|
||||
# load template resources
|
||||
logger.debug("Loading internal templates.")
|
||||
templates = util.get_resources("templates")
|
||||
logger.debug("Loading internal static content.")
|
||||
static = util.get_resources("content")
|
||||
for dir, resources in [
|
||||
(layout.root, None),
|
||||
|
@ -76,13 +89,18 @@ def initialize_site(root: Path | None = None):
|
|||
(layout.templates, templates),
|
||||
]:
|
||||
if not dir.is_dir():
|
||||
logger.debug(f"Creating {dir}.")
|
||||
dir.mkdir()
|
||||
if resources is not None:
|
||||
logger.debug("Writing resources.")
|
||||
for r in resources:
|
||||
logger.debug(f"Writing {(root / Path(r.name))}.")
|
||||
(root / Path(r.name)).write_text(r.contents)
|
||||
|
||||
config_path = layout.root / "config.yml"
|
||||
logger.debug("Loading default configuation.")
|
||||
config = ZonaConfig()
|
||||
logger.debug(f"Writing default configuration to {config_path}.")
|
||||
with open(config_path, "w") as f:
|
||||
yaml.dump(
|
||||
asdict(config),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue