Generate structure

Create a Python command-line tool that reads a JSON description of a directory and file hierarchy and creates that structure on disk. Each key in the JSON corresponds to a directory or file name: if the value is another dictionary, it’s treated as a subdirectory; if it’s a string, it becomes the file’s content. The tool supports validation, dry-run previews, overwrite policies, and atomic file creation.

Phase 1 — CLI & JSON loading

  • Command: create_structure.py <root_folder_path> <structure_json_file_path>
  • Read JSON (UTF-8) into an in-memory dict
  • Create <root_folder_path> if missing

Functional result: Script loads the dictionary and ensures the root folder exists.


Phase 2 — Structure validation

  • Accept only dict→directory, string→file-content
  • Disallow path separators and empty names in keys
  • Reject unsupported types with a concise error

Functional result: Valid structures pass; invalid ones stop with a short, clear message.


Phase 3 — Recursive creation

  • Depth-first create directories
  • For file entries, write content (UTF-8)
  • Overwrite only files that correspond to file keys

Functional result: The filesystem mirrors the JSON: directories and files are created with expected contents.


Phase 4 — Dry-run, conflict policy

  • --dry-run previews planned actions without touching disk
  • --mode {fail,skip,overwrite} defines behavior for existing paths
    • fail – stops immediately if a file or folder already exists, ensuring nothing is overwritten.
    • skip – leaves existing files or folders untouched and continues creating the rest of the structure.
    • overwrite – replaces existing files with new content (eventually using atomic writes for safety).

Functional result: Users can preview changes; re-runs behave predictably per mode; file updates are atomic, avoiding partial writes.