Skip to main content

Module to facilitate actions on a file tree.

Project description

thule [t-OOL-ee]

A dumb, yet useful, file walker.

Cat

Usage

  1. Import from thule import Actions, Walker
  2. Subclass Actions for your use case (e.g. class JSONtoYAML(Actions):)
  3. Define in your class any or all of the following:
    • one or more functions suffixed with _directory, will run on any directory (or subdirectory) under the path passed to the walker's accept method
    • one or more functions suffixed with _file, will run on every file contained in any directory (or any subdirectory) under the path passed to the walker's accept method
    • one or more functions suffixed with _final, will run after all directories, subdirectories, and files have been visited under the path passed to the walker's accept method
  4. Instantiate the subclassed Actions (e.g. converter = JSONtoYAML())
  5. Instantiate a Walker (e.g. walker = Walker())
  6. Call accept on the walker (e.g. walker.accept(<path>, converter))

Example

JSON to YAML Converter

Starting data

├── dir1
│   ├── file2.json
│   ├── sub1
│      ├── file3.json
│      └── file4.json
│   └── sub2
│       └── file5.json
├── dir2
│   └── sub3
│       └── file6.json
├── dir3
│   ├── sub4
│      └── file7.json
│   └── sub5
│       ├── file8.json
│       └── file9.json
└── file1.json

Thule Script to convert JSON to YAML

"""
json_to_yaml.py
converts all json files
in nested directories to yaml files
removes old json files
"""
import os
import sys
import json
import yaml
from thule import Actions, Walker

class JSONtoYAML(Actions):

  def __init__(self):
    self.num_processed = 0

  def print_directory(self, dir_path):
    print(f"processing files in {dir_path}")

  def convert_file(self, dir_path, file_name):
    if file_name.endswith(".json"):
      try:
        print(f"  converting {file_name}....", end="", flush=True)
        file_path = os.path.join(dir_path, file_name)
        with open(file_path, 'r') as jf:
            data = json.load(jf)
        with open(file_path.replace(".json", ".yaml"), 'w') as yf:
            yaml.dump(data, yf)
        os.remove(file_path)
        self.num_processed += 1
      except Exception:
        print("[failed]")
      else:
        print("[done]")

  def total_converted_final(self):
    print(f"{self.num_processed} files in total were converted from JSON to YAML")


if __name__ == "__main__":

  cwd = os.getcwd()
  if len(sys.argv) > 2:
    print("usage: python example.py <relative path to root dir>")
    exit(1)

  rel_path = sys.argv[1]
  abs_path = os.path.join(cwd, rel_path)

  converter = JSONtoYAML()
  walker = Walker()
  walker.accept(abs_path, converter)

Run the script as follows: python json_to_yaml.py relative/path/test_json

Script Output

processing files in cwd/relative/path/test_json
  converting file1.json....[done]
processing files in cwd/relative/path/test_json/dir2
processing files in cwd/relative/path/test_json/dir2/sub3
  converting file6.json....[done]
processing files in cwd/relative/path/test_json/dir3
processing files in cwd/relative/path/test_json/dir3/sub5
  converting file9.json....[done]
  converting file8.json....[done]
processing files in cwd/relative/path/test_json/dir3/sub4
  converting file7.json....[done]
processing files in cwd/relative/path/test_json/dir1
  converting file2.json....[done]
processing files in cwd/relative/path/test_json/dir1/sub1
  converting file4.json....[done]
  converting file3.json....[done]
processing files in cwd/relative/path/test_json/dir1/sub2
  converting file5.json....[done]
9 files in total were converted from JSON to YAML

Resulting Data

├── dir1
│   ├── file2.yaml
│   ├── sub1
│      ├── file3.yaml
│      └── file4.yaml
│   └── sub2
│       └── file5.yaml
├── dir2
│   └── sub3
│       └── file6.yaml
├── dir3
│   ├── sub4
│      └── file7.yaml
│   └── sub5
│       ├── file8.yaml
│       └── file9.yaml
└── file1.yaml

Project details


Release history Release notifications | RSS feed

This version

1.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

thule-1.0.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

thule-1.0-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file thule-1.0.tar.gz.

File metadata

  • Download URL: thule-1.0.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.8

File hashes

Hashes for thule-1.0.tar.gz
Algorithm Hash digest
SHA256 9779748a9129b9cb51b3bc81b709d6d71fb5815ac98be79d1584dc1c328ee39f
MD5 4e5af417ccd423c9b14a59554c85e20a
BLAKE2b-256 dec811e3d7b1125789cb6e9b66509f4deacde4ef54852375d59e255dabef822c

See more details on using hashes here.

File details

Details for the file thule-1.0-py3-none-any.whl.

File metadata

  • Download URL: thule-1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.8

File hashes

Hashes for thule-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 413f4f2c8d74c4b07c1199fdf721498e835001904a7e6a0a97285bca0839e342
MD5 ef3a4e37820bef204b843eb79c654dfd
BLAKE2b-256 96116aa9a6adb3ccaf0182b97e0d66db5ffa3bcdf16acb7b3d044a6b7f1b1d18

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page