__all__ = ['Factory']


import jsfiddle_build
import jsfiddle_github
import jsfiddle_generator
import jsfiddle_readme_generator
import getdirs
import getfiles
import os
import popd
import yaml


@popd.popd
def _build(path):
    os.chdir(path)
    jsfiddle_build.Build().save("build.html")


@popd.popd
def _init(path):
    os.chdir(path)
    isempty = len(os.listdir(path)) == 0
    isfiddle = len(
        list(filter(os.path.exists, ["demo.css", "demo.js", "demo.html"]))) > 0
    if isempty or isfiddle:
        jsfiddle_generator.JSFiddleRepo().create()


@popd.popd
def _readme(path):
    os.chdir(path)
    jsfiddle_readme_generator.Readme().save("README.md")


class Factory:
    """attrs: `path`. methods: `detox()`, `init()`, `build()`, `readme()`, `update_resources()`"""
    path = None

    def __init__(self, path=None):
        if not path:
            path = os.getcwd()
        self.path = path

    def build_html(self):
        files = getfiles.getfiles(self.path)
        matches = ["demo.html", "fiddle.html"]
        for f in filter(lambda f: os.path.basename(f) in matches, files):
            _build(os.path.dirname(f))

    def create_readme(self):
        files = getfiles.getfiles(self.path)
        matches = ["demo.html", "fiddle.html"]
        for f in filter(lambda f: os.path.basename(f) in matches, files):
            _readme(os.path.dirname(f))

    def init(self):
        for path in getdirs.getdirs(self.path):
            _init(path)

    def detox(self):
        renamed = True
        while renamed:
            renamed = False
            for path in getdirs.getdirs(self.path):
                relpath = os.path.relpath(path, os.getcwd())
                new_relpath = jsfiddle_github.sanitize(relpath)
                new_path = os.path.join(os.getcwd(), new_relpath)
                ishidden = relpath[0] == "." and "%s." % os.sep not in relpath
                if not ishidden and new_relpath != relpath:
                    os.rename(path, new_path)
                    print("%s -> %s" % (path, new_path))
                    renamed = True
                    break

    def update_resources(self):
        f = os.path.join(self.path, "resources.txt")
        if not os.path.exists(f):
            print("SKIP: %s NOT EXISTS" % f)
        resources = list(filter(None, open(f).read().splitlines()))
        files = getfiles.getfiles(self.path)
        matches = ["demo.details", "fiddle.manifest"]
        for f in filter(lambda f: os.path.basename(f) in matches, files):
            if os.path.exists(f):
                data = yaml.load(open(f, 'r'))
                if data.get("resources", []) != resources:
                    data["resources"] = resources
                    yaml.dump(data, open(f, 'w'), default_flow_style=False)

[((17, 4, 17, 18), 'os.chdir', 'os.chdir', ({(17, 13, 17, 17): 'path'}, {}), '(path)', False, 'import os\n'), ((23, 4, 23, 18), 'os.chdir', 'os.chdir', ({(23, 13, 23, 17): 'path'}, {}), '(path)', False, 'import os\n'), ((33, 4, 33, 18), 'os.chdir', 'os.chdir', ({(33, 13, 33, 17): 'path'}, {}), '(path)', False, 'import os\n'), ((47, 16, 47, 44), 'getfiles.getfiles', 'getfiles.getfiles', ({(47, 34, 47, 43): 'self.path'}, {}), '(self.path)', False, 'import getfiles\n'), ((53, 16, 53, 44), 'getfiles.getfiles', 'getfiles.getfiles', ({(53, 34, 53, 43): 'self.path'}, {}), '(self.path)', False, 'import getfiles\n'), ((59, 20, 59, 46), 'getdirs.getdirs', 'getdirs.getdirs', ({(59, 36, 59, 45): 'self.path'}, {}), '(self.path)', False, 'import getdirs\n'), ((78, 12, 78, 52), 'os.path.join', 'os.path.join', ({(78, 25, 78, 34): 'self.path', (78, 36, 78, 51): '"""resources.txt"""'}, {}), "(self.path, 'resources.txt')", False, 'import os\n'), ((82, 16, 82, 44), 'getfiles.getfiles', 'getfiles.getfiles', ({(82, 34, 82, 43): 'self.path'}, {}), '(self.path)', False, 'import getfiles\n'), ((18, 4, 18, 26), 'jsfiddle_build.Build', 'jsfiddle_build.Build', ({}, {}), '()', False, 'import jsfiddle_build\n'), ((24, 18, 24, 34), 'os.listdir', 'os.listdir', ({(24, 29, 24, 33): 'path'}, {}), '(path)', False, 'import os\n'), ((34, 4, 34, 38), 'jsfiddle_readme_generator.Readme', 'jsfiddle_readme_generator.Readme', ({}, {}), '()', False, 'import jsfiddle_readme_generator\n'), ((43, 19, 43, 30), 'os.getcwd', 'os.getcwd', ({}, {}), '()', False, 'import os\n'), ((66, 24, 66, 50), 'getdirs.getdirs', 'getdirs.getdirs', ({(66, 40, 66, 49): 'self.path'}, {}), '(self.path)', False, 'import getdirs\n'), ((79, 15, 79, 32), 'os.path.exists', 'os.path.exists', ({(79, 30, 79, 31): 'f'}, {}), '(f)', False, 'import os\n'), ((85, 15, 85, 32), 'os.path.exists', 'os.path.exists', ({(85, 30, 85, 31): 'f'}, {}), '(f)', False, 'import os\n'), ((28, 8, 28, 41), 'jsfiddle_generator.JSFiddleRepo', 'jsfiddle_generator.JSFiddleRepo', ({}, {}), '()', False, 'import jsfiddle_generator\n'), ((50, 19, 50, 37), 'os.path.dirname', 'os.path.dirname', ({(50, 35, 50, 36): 'f'}, {}), '(f)', False, 'import os\n'), ((56, 20, 56, 38), 'os.path.dirname', 'os.path.dirname', ({(56, 36, 56, 37): 'f'}, {}), '(f)', False, 'import os\n'), ((68, 30, 68, 63), 'jsfiddle_github.sanitize', 'jsfiddle_github.sanitize', ({(68, 55, 68, 62): 'relpath'}, {}), '(relpath)', False, 'import jsfiddle_github\n'), ((49, 34, 49, 53), 'os.path.basename', 'os.path.basename', ({(49, 51, 49, 52): 'f'}, {}), '(f)', False, 'import os\n'), ((55, 34, 55, 53), 'os.path.basename', 'os.path.basename', ({(55, 51, 55, 52): 'f'}, {}), '(f)', False, 'import os\n'), ((67, 48, 67, 59), 'os.getcwd', 'os.getcwd', ({}, {}), '()', False, 'import os\n'), ((69, 40, 69, 51), 'os.getcwd', 'os.getcwd', ({}, {}), '()', False, 'import os\n'), ((72, 20, 72, 45), 'os.rename', 'os.rename', ({(72, 30, 72, 34): 'path', (72, 36, 72, 44): 'new_path'}, {}), '(path, new_path)', False, 'import os\n'), ((84, 34, 84, 53), 'os.path.basename', 'os.path.basename', ({(84, 51, 84, 52): 'f'}, {}), '(f)', False, 'import os\n')]