__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)

[((228, 242), 'os.chdir', 'os.chdir', (['path'], {}), '(path)\n', (236, 242), False, 'import os\n'), ((323, 337), 'os.chdir', 'os.chdir', (['path'], {}), '(path)\n', (331, 337), False, 'import os\n'), ((594, 608), 'os.chdir', 'os.chdir', (['path'], {}), '(path)\n', (602, 608), False, 'import os\n'), ((954, 982), 'getfiles.getfiles', 'getfiles.getfiles', (['self.path'], {}), '(self.path)\n', (971, 982), False, 'import getfiles\n'), ((1189, 1217), 'getfiles.getfiles', 'getfiles.getfiles', (['self.path'], {}), '(self.path)\n', (1206, 1217), False, 'import getfiles\n'), ((1420, 1446), 'getdirs.getdirs', 'getdirs.getdirs', (['self.path'], {}), '(self.path)\n', (1435, 1446), False, 'import getdirs\n'), ((2159, 2199), 'os.path.join', 'os.path.join', (['self.path', '"""resources.txt"""'], {}), "(self.path, 'resources.txt')\n", (2171, 2199), False, 'import os\n'), ((2363, 2391), 'getfiles.getfiles', 'getfiles.getfiles', (['self.path'], {}), '(self.path)\n', (2380, 2391), False, 'import getfiles\n'), ((247, 269), 'jsfiddle_build.Build', 'jsfiddle_build.Build', ([], {}), '()\n', (267, 269), False, 'import jsfiddle_build\n'), ((356, 372), 'os.listdir', 'os.listdir', (['path'], {}), '(path)\n', (366, 372), False, 'import os\n'), ((613, 647), 'jsfiddle_readme_generator.Readme', 'jsfiddle_readme_generator.Readme', ([], {}), '()\n', (645, 647), False, 'import jsfiddle_readme_generator\n'), ((874, 885), 'os.getcwd', 'os.getcwd', ([], {}), '()\n', (883, 885), False, 'import os\n'), ((1592, 1618), 'getdirs.getdirs', 'getdirs.getdirs', (['self.path'], {}), '(self.path)\n', (1607, 1618), False, 'import getdirs\n'), ((2215, 2232), 'os.path.exists', 'os.path.exists', (['f'], {}), '(f)\n', (2229, 2232), False, 'import os\n'), ((2535, 2552), 'os.path.exists', 'os.path.exists', (['f'], {}), '(f)\n', (2549, 2552), False, 'import os\n'), ((515, 548), 'jsfiddle_generator.JSFiddleRepo', 'jsfiddle_generator.JSFiddleRepo', ([], {}), '()\n', (546, 548), False, 'import jsfiddle_generator\n'), ((1123, 1141), 'os.path.dirname', 'os.path.dirname', (['f'], {}), '(f)\n', (1138, 1141), False, 'import os\n'), ((1359, 1377), 'os.path.dirname', 'os.path.dirname', (['f'], {}), '(f)\n', (1374, 1377), False, 'import os\n'), ((1711, 1744), 'jsfiddle_github.sanitize', 'jsfiddle_github.sanitize', (['relpath'], {}), '(relpath)\n', (1735, 1744), False, 'import jsfiddle_github\n'), ((1064, 1083), 'os.path.basename', 'os.path.basename', (['f'], {}), '(f)\n', (1080, 1083), False, 'import os\n'), ((1299, 1318), 'os.path.basename', 'os.path.basename', (['f'], {}), '(f)\n', (1315, 1318), False, 'import os\n'), ((1668, 1679), 'os.getcwd', 'os.getcwd', ([], {}), '()\n', (1677, 1679), False, 'import os\n'), ((1785, 1796), 'os.getcwd', 'os.getcwd', ([], {}), '()\n', (1794, 1796), False, 'import os\n'), ((1970, 1995), 'os.rename', 'os.rename', (['path', 'new_path'], {}), '(path, new_path)\n', (1979, 1995), False, 'import os\n'), ((2480, 2499), 'os.path.basename', 'os.path.basename', (['f'], {}), '(f)\n', (2496, 2499), False, 'import os\n')]