import os
import subprocess
import pydantic
from typing import Optional
from datetime import date, datetime

run = lambda cmd: subprocess.run(
    cmd.split(' '),
    stdout = subprocess.PIPE,
    stderr = subprocess.PIPE
)

def filepath_or_string(s: str) -> str:
    '''Loads the contents of a string if it is a filepath, otherwise returns the string.'''
    if os.path.isfile(s):
        with open(s, 'r') as f:
            return f.read()
    else:
        return s
    

class GlobalVars(pydantic.BaseModel):
    '''Static-valued global variables to be interpolated into any HTML templates.'''
    today: date = datetime.today()


class SiteConfig(pydantic.BaseModel):
    base_url: str
    web_root: str
    build_cache: str 
    git_repo: Optional[str] = None
    assets: Optional[list] = None
    articles: Optional[list] = None