Last active
November 25, 2015 00:07
-
-
Save WindfallLabs/43e55ae30a467cac7ca7 to your computer and use it in GitHub Desktop.
Creates a .yaml file 'psuedo-schema' from a folder directory (including ESRI gdb's)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
"""distribution_yaml.py: Converts Paths and GBDs to dicts, then to a yaml file. | |
This script 'walks' through a folder structure (including ESRI Geodatabases) | |
and outputs a .yaml pseudo-schema of the directory-tree and geodatabase(s). | |
MIT License (MIT) | |
Copyright (C) 2015 Garin Wally / Windfall Spatial | |
This code was modified from code by Blake Miller, found here: | |
http://btmiller.com/2015/03/17/represent-file-structure-as-yaml-with-python.html | |
""" | |
import os | |
import sys | |
import arcpy | |
import yaml | |
def gdb2dict(path): | |
"""Creates a dictionary from a Geodatabase schema. | |
""" | |
gdb_dir = {} | |
for gdbname, dsnames, fcs in arcpy.da.Walk(path): | |
gdb = os.path.basename(gdbname) | |
gdb_dir[gdb] = [] | |
if dsnames: | |
for ds in dsnames: | |
gdb_dir[gdb].append(gdb2dict(os.path.join(path, ds))) | |
for fc in fcs: | |
gdb_dir[gdb].append(fc) | |
else: | |
gdb_dir[gdb] = fcs | |
return gdb_dir | |
def dir2dict(path): | |
"""Creates a dictionary from a directory tree. | |
Credit: Blake Miller | |
""" | |
directory = {} | |
for dirname, dirnames, filenames in os.walk(path): | |
dn = os.path.basename(dirname) | |
directory[dn] = [] | |
if dirnames: | |
for d in dirnames: | |
if not d.endswith(".gdb"): | |
directory[dn].append(dir2dict(path=os.path.join(path, d))) | |
elif d.endswith(".gdb"): | |
gdb = os.path.join(path, d) | |
directory[dn].append(gdb2dict(gdb)) | |
for f in filenames: | |
directory[dn].append(f) | |
else: | |
directory[dn] = filenames | |
return directory | |
def path2yaml(input_path, output_yaml=None): | |
"""Converts an input_dict to an output_yaml file.""" | |
if output_yaml is None or output_yaml == "": | |
output_yaml = "{}.yaml".format(os.path.basename(input_path)) | |
assert output_yaml.endswith(".yaml"), \ | |
"Output file must have extension '.yaml'" | |
if input_path.endswith(".gdb"): | |
out_file = os.path.join(os.path.dirname(input_path), output_yaml) | |
else: | |
out_file = os.path.join(input_path, output_yaml) | |
with open(out_file, 'w') as f: | |
if not input_path.endswith(".gdb"): | |
yaml.safe_dump(dir2dict(input_path), f, | |
default_flow_style=False, allow_unicode=True) | |
else: | |
yaml.safe_dump(gdb2dict(input_path), f, | |
default_flow_style=False, allow_unicode=True) | |
print("Dict written to {}".format(out_file)) | |
return | |
# Allow script to be run from either commandline or with raw_input | |
if __name__ == '__main__': | |
try: | |
if len(sys.argv) == 1: | |
path2yaml(os.path.abspath(sys.argv[1])) | |
elif len(sys.argv) == 2: | |
path2yaml(os.path.abspath(sys.argv[1]), sys.argv[2]) | |
except IndexError: | |
path2yaml(raw_input("Enter directory or path: "), | |
raw_input("Enter an output .yaml name (Optional) ")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment