@ -22,6 +22,7 @@ from .template_tools import (
file_content ,
file_list ,
file_list_hier ,
file_json ,
file_metadata ,
file_name ,
file_raw ,
@ -35,6 +36,12 @@ def setup_logging(verbose: bool = False) -> None:
pass
def parse_var ( varspec : str ) - > List :
if ( not ( ' = ' in varspec ) ) :
return [ varspec , True ]
return list ( varspec . split ( ' = ' , 2 ) )
def get_args ( args : List [ str ] ) - > argparse . Namespace :
parser = argparse . ArgumentParser ( " Compile a Pixywerk directory into an output directory. " )
@ -50,8 +57,9 @@ def get_args(args: List[str]) -> argparse.Namespace:
parser . add_argument ( " -d " , " --dry-run " , help = " Perform a dry-run. " , action = " store_true " )
parser . add_argument ( " -v " , " --verbose " , help = " Output verbosely. " , action = " store_true " )
parser . add_argument ( " --processors " , help = " Specify a path to a processor configuration file. " , default = None )
parser . add_argument (
" -D " , " --define " , help = " Add a variable to the metadata. " , nargs = " + " , action = " extend " , type = parse_var )
result = parser . parse_args ( args )
# validate arguments
if not os . path . isdir ( result . root ) :
raise FileNotFoundError ( " can ' t find root folder {} " . format ( result . root ) )
@ -89,17 +97,21 @@ def main() -> int:
" author " : " " ,
" author_email " : " " ,
}
for var in args . define :
default_metadata [ var [ 0 ] ] = var [ 1 ]
meta_tree = MetaTree ( args . root , default_metadata )
file_list_cache = cast ( Dict , { } )
file_cont_cache = cast ( Dict , { } )
file_name_cache = cast ( Dict , { } )
file_raw_cache = cast ( Dict , { } )
file_json_cache = cast ( Dict , { } )
flist = file_list ( args . root , file_list_cache )
default_metadata [ " globals " ] = {
" get_file_list " : flist ,
" get_hier " : file_list_hier ( args . root , flist ) ,
" get_file_name " : file_name ( args . root , meta_tree , process_chains , file_name_cache ) ,
" get_file_content " : file_content ( args . root , meta_tree , process_chains , file_cont_cache ) ,
" get_json " : file_json ( args . root , file_json_cache ) ,
" get_raw " : file_raw ( args . root , file_raw_cache ) ,
" get_file_metadata " : file_metadata ( meta_tree ) ,
" get_time_iso8601 " : time_iso8601 ( " UTC " ) ,
@ -127,7 +139,7 @@ def main() -> int:
continue
metadata = meta_tree . get_metadata ( os . path . join ( workroot , f ) )
chain = process_chains . get_chain_for_filename ( os . path . join ( root , f ) , ctx = metadata )
print ( " process {} -> {} " . format ( os . path . join ( root , f ) , os . path . join ( target_dir , chain . output_filename ) ) )
print ( " process {} -> {} -> {} ". format ( os . path . join ( root , f ) , repr ( chain ) , os . path . join ( target_dir , chain . output_filename ) ) )
if not args . dry_run :
try :
with open ( os . path . join ( target_dir , chain . output_filename ) , " w " ) as outfile :