def write_modified_raws(graphics_to_apply, raws_sourcedir, outputdir): """Write the full modified raws to the raw output directory. graphics_to_apply is a dict of type string:dict{string:BoundNode}. The top- level string keys are filenames. Each filename's value is a dict representing the top-level, relevant tags in the target raw file of that name. The inner key is the full tag (without brackets), and the BoundNode is the top node of the tree that that tag begins. Unless you have way too much time on your hands, I recommend you generate graphics_to_apply using the bind_graphics_to_targets function. The actual writing is done in this way: first, the function walks the target raw source directory structure, and duplicates it in the raw output directory. Then it walks the target raw source files, looking for each filename in graphics_to_apply's keyset, or one of the properties graphics_overwrite or graphics_ignore. * If it finds the filename in graphics_overwrite, it copies the file directly from the corresponding place in the graphics source directory. * If it finds the filename in graphics_ignore, or doesn't find the filename at all, it copies the file directly from the corresponding place in the target raw source directory. * If it finds the filename in graphics_to_apply's keyset, it opens the file in the target raw source directory, creates and opens a corresponding file in the raw output directory, and walks through the target raw source file a line at a time, constructing the modified file. """ properties = config.properties userlog.info("Writing modified raws...") os.makedirs(outputdir, exist_ok=True) for root, dirs, files in os.walk(raws_sourcedir): # Create directories so we don't have any issues later on for _dir in dirs: targetdir = os.path.join(root, _dir) targetdir = outputdir + targetdir[len(raws_sourcedir):] if not os.path.exists(targetdir): userlog.info("Creating output directory %s", _dir) os.makedirs(targetdir, exist_ok=True) for file in files: targetpath = os.path.join(root, file) targetpath = outputdir + targetpath[len(raws_sourcedir):] if ((parsing.path_compatible( targetpath, properties[config.GRAPHICS_IGNORE_LIST][1:]) or file not in graphics_to_apply.keys())): userlog.info("Copying %s from target source...", file) targetpath = shutil.copyfile(os.path.join(root, file), targetpath) userlog.info("%s copied.", file) # TODO Need to implement graphics overwrites # elif parsing.path_compatible(targetpath, # properties[config.GRAPHICS_OVERWRITE_LIST][1:] # ): # pass else: _apply_graphics_to_file(graphics_to_apply, file, root, targetpath) userlog.info("All files written.")
def write_modified_raws(graphics_to_apply, raws_sourcedir, outputdir): """Write the full modified raws to the raw output directory. graphics_to_apply is a dict of type string:dict{string:BoundNode}. The top- level string keys are filenames. Each filename's value is a dict representing the top-level, relevant tags in the target raw file of that name. The inner key is the full tag (without brackets), and the BoundNode is the top node of the tree that that tag begins. Unless you have way too much time on your hands, I recommend you generate graphics_to_apply using the bind_graphics_to_targets function. The actual writing is done in this way: first, the function walks the target raw source directory structure, and duplicates it in the raw output directory. Then it walks the target raw source files, looking for each filename in graphics_to_apply's keyset, or one of the properties graphics_overwrite or graphics_ignore. * If it finds the filename in graphics_overwrite, it copies the file directly from the corresponding place in the graphics source directory. * If it finds the filename in graphics_ignore, or doesn't find the filename at all, it copies the file directly from the corresponding place in the target raw source directory. * If it finds the filename in graphics_to_apply's keyset, it opens the file in the target raw source directory, creates and opens a corresponding file in the raw output directory, and walks through the target raw source file a line at a time, constructing the modified file. """ properties = config.properties userlog.info("Writing modified raws...") os.makedirs(outputdir, exist_ok=True) for root, dirs, files in os.walk(raws_sourcedir): # Create directories so we don't have any issues later on for _dir in dirs: targetdir = os.path.join(root, _dir) targetdir = outputdir + targetdir[len(raws_sourcedir):] if not os.path.exists(targetdir): userlog.info("Creating output directory %s", _dir) os.makedirs(targetdir, exist_ok=True) for file in files: targetpath = os.path.join(root, file) targetpath = outputdir + targetpath[len(raws_sourcedir):] if ((parsing.path_compatible( targetpath, properties[config.GRAPHICS_IGNORE_LIST][1:]) or file not in graphics_to_apply.keys())): userlog.info("Copying %s from target source...", file) targetpath = shutil.copyfile(os.path.join(root, file), targetpath) userlog.info("%s copied.", file) # TODO Need to implement graphics overwrites # elif parsing.path_compatible(targetpath, # properties[config.GRAPHICS_OVERWRITE_LIST][1:] # ): # pass else: _apply_graphics_to_file(graphics_to_apply, file, root, targetpath) userlog.info("All files written.")
def find_graphics_overrides(graphics_directory, graphics_overwrites): to_return = [] userlog.info("Locating graphics override files") for root, dirs, files in os.walk(graphics_directory): for file in files: filepath = os.path.join(root, file) if parsing.path_compatible(filepath, graphics_overwrites): to_return.append(filepath) return to_return
def find_graphics_overrides(graphics_directory, graphics_overwrites): to_return = [] userlog.info("Locating graphics override files") for root, dirs, files in os.walk(graphics_directory): for file in files: filepath = os.path.join(root, file) if parsing.path_compatible(filepath, graphics_overwrites): to_return.append(filepath) return to_return