__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, 'os.chdir', 'os.chdir', False, 'import os\n'), (23, 'os.chdir', 'os.chdir', False, 'import os\n'), (33, 'os.chdir', 'os.chdir', False, 'import os\n'), (47, 'getfiles.getfiles', 'getfiles.getfiles', False, 'import getfiles\n'), (53, 'getfiles.getfiles', 'getfiles.getfiles', False, 'import getfiles\n'), (59, 'getdirs.getdirs', 'getdirs.getdirs', False, 'import getdirs\n'), (78, 'os.path.join', 'os.path.join', False, 'import os\n'), (82, 'getfiles.getfiles', 'getfiles.getfiles', False, 'import getfiles\n'), (18, 'jsfiddle_build.Build', 'jsfiddle_build.Build', False, 'import jsfiddle_build\n'), (24, 'os.listdir', 'os.listdir', False, 'import os\n'), (34, 'jsfiddle_readme_generator.Readme', 'jsfiddle_readme_generator.Readme', False, 'import jsfiddle_readme_generator\n'), (43, 'os.getcwd', 'os.getcwd', False, 'import os\n'), (66, 'getdirs.getdirs', 'getdirs.getdirs', False, 'import getdirs\n'), (79, 'os.path.exists', 'os.path.exists', False, 'import os\n'), (85, 'os.path.exists', 'os.path.exists', False, 'import os\n'), (28, 'jsfiddle_generator.JSFiddleRepo', 'jsfiddle_generator.JSFiddleRepo', False, 'import jsfiddle_generator\n'), (50, 'os.path.dirname', 'os.path.dirname', False, 'import os\n'), (56, 'os.path.dirname', 'os.path.dirname', False, 'import os\n'), (68, 'jsfiddle_github.sanitize', 'jsfiddle_github.sanitize', False, 'import jsfiddle_github\n'), (49, 'os.path.basename', 'os.path.basename', False, 'import os\n'), (55, 'os.path.basename', 'os.path.basename', False, 'import os\n'), (67, 'os.getcwd', 'os.getcwd', False, 'import os\n'), (69, 'os.getcwd', 'os.getcwd', False, 'import os\n'), (72, 'os.rename', 'os.rename', False, 'import os\n'), (84, 'os.path.basename', 'os.path.basename', False, 'import os\n')]