Skip to content

Commit

Permalink
yamldirs command to create yaml file from directory and directory fro…
Browse files Browse the repository at this point in the history
…m yaml.
  • Loading branch information
thebjorn committed Aug 6, 2019
1 parent c1a4656 commit 720ca28
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 5 deletions.
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@
# built documents.
#
# The short X.Y version.
version = '1.1.6'
version = '1.1.7'
# The full version, including alpha/beta/rc tags.
release = '1.1.6'
release = '1.1.7'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
7 changes: 6 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Topic :: Software Development :: Libraries
"""

version = '1.1.6'
version = '1.1.7'


class PyTest(TestCommand):
Expand Down Expand Up @@ -55,6 +55,11 @@ def run_tests(self):
classifiers=[line for line in classifiers.split('\n') if line],
long_description=open('README.rst').read(),
cmdclass={'test': PyTest},
entry_points={
'console_scripts': [
'yamldirs = yamldirs.yamldirs_cmd:main',
]
},
packages=find_packages(),
zip_safe=False,
)
2 changes: 1 addition & 1 deletion yamldirs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-

__version__ = '1.1.6'
__version__ = '1.1.7'

from .filemaker import create_files

2 changes: 1 addition & 1 deletion yamldirs/filemaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def make_file(self, filename, content):
@contextmanager
def create_files(filedef, cleanup=True):
"""Contextmanager that creates a directory structure from a yaml
descripttion.
description.
"""
cwd = os.getcwd()
tmpdir = tempfile.mkdtemp()
Expand Down
40 changes: 40 additions & 0 deletions yamldirs/yamldirs_cmd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# -*- coding: utf-8 -*-
import os
import sys

import yaml
from .filemaker import Filemaker


def tree(root):
def _tree():
for rt, dirs, files in os.walk(root):
for name in files:
if name.endswith('~'): continue
if name.startswith('.'): continue
yield os.path.join(rt, name)
return list(sorted(_tree()))


def directory2yaml(dirname):
res = {}
for filename in tree(dirname):
parts = filename.replace('\\', '/').split('/')
cur = res
for part in parts[:-1]:
cur.setdefault(part, {})
cur = cur[part]
cur[parts[-1]] = open(filename).read().replace('\r\n', '\n').strip()
print yaml.dump(res)


def reconstitute_directory(yamlfile):
Filemaker(os.getcwd(), open(yamlfile).read())


def main():
arg = sys.argv[1]
if arg.endswith('.yaml'):
reconstitute_directory(arg)
else:
directory2yaml(arg)

0 comments on commit 720ca28

Please sign in to comment.