thule [t-OOL-ee]
A dumb, yet useful, file walker.
Usage
- Import
from thule import Actions, Walker - Subclass Actions for your use case (e.g.
class JSONtoYAML(Actions):) - 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
- one or more functions suffixed with
- Instantiate the subclassed Actions (e.g.
converter = JSONtoYAML()) - Instantiate a Walker (e.g.
walker = Walker()) - 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
Metadata
Release files for thule 1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| thule-1.0.tar.gz | 3.4 kB | Without provenance | Details for thule-1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| thule-1.0-py3-none-any.whl | Python 3 | none | any | Details for thule-1.0-py3-none-any.whl |
Total release size: 7.1 kB
Release files / thule-1.0.tar.gz
| Download URL | thule-1.0.tar.gz |
|---|---|
| Size | 3.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9779748a9129b9cb51b3bc81b709d6d71fb5815ac98be79d1584dc1c328ee39f
|
|
BLAKE2b-256 checksum How to use checksums |
dec811e3d7b1125789cb6e9b66509f4deacde4ef54852375d59e255dabef822c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.8.8
|
Release files / thule-1.0-py3-none-any.whl
| Download URL | thule-1.0-py3-none-any.whl |
|---|---|
| Size | 3.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
413f4f2c8d74c4b07c1199fdf721498e835001904a7e6a0a97285bca0839e342
|
|
BLAKE2b-256 checksum How to use checksums |
96116aa9a6adb3ccaf0182b97e0d66db5ffa3bcdf16acb7b3d044a6b7f1b1d18
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.8.8
|
Security for thule 1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.