예제 #1
0
파일: data.py 프로젝트: svenhertle/stawebg
class Project:
    def __init__(self, project_dir="", test=False, output=None):
        self._sites = []
        self._layouts = {}
        self._root_dir = project_dir
        self._test = test
        self._other_output = output

        self._config = Config(os.path.join(self._root_dir, "stawebg.json"), Config.global_struct)

        # Make directories absolute
        dirs = self._config.get(["dirs"])
        for k in dirs:
            dirs[k] = os.path.join(self._root_dir, dirs[k])

        # Set locale
        try:
            locale.setlocale(locale.LC_ALL, self.getConfig(["locale"], False, ""))
        except locale.Error as e:
            fail('Failed to set the locale "' + self.getConfig(["locale"], False, "") + '": ' + str(e))

        # Add all layouts to list
        for name in listFolders(self.getConfig(["dirs", "layouts"])):
            self._layouts[name] = Layout(self, name)

        # Add all site directories to list
        for s in listFolders(self.getConfig(["dirs", "sites"])):
            site = Site(s, self)
            self._sites.append(site)
            site.read()

        # copy files to out dir
        for s in self._sites:
            s.copy()

    def getConfig(self, key, fail=True, default=None):
        return self._config.get(key, fail, default)

    def getLayout(self, name=None):
        if not name:
            name = "default"

        layout = self._layouts.get(name)

        if not layout:
            fail("Can't find layout: " + name)

        return layout

    def getOutputDir(self):
        if self._other_output:
            return self._other_output
        elif self._test:
            return self.getConfig(["dirs", "test"])
        else:
            return self.getConfig(["dirs", "out"])
예제 #2
0
파일: data.py 프로젝트: svenhertle/stawebg
    def __init__(self, project_dir="", test=False, output=None):
        self._sites = []
        self._layouts = {}
        self._root_dir = project_dir
        self._test = test
        self._other_output = output

        self._config = Config(os.path.join(self._root_dir, "stawebg.json"), Config.global_struct)

        # Make directories absolute
        dirs = self._config.get(["dirs"])
        for k in dirs:
            dirs[k] = os.path.join(self._root_dir, dirs[k])

        # Set locale
        try:
            locale.setlocale(locale.LC_ALL, self.getConfig(["locale"], False, ""))
        except locale.Error as e:
            fail('Failed to set the locale "' + self.getConfig(["locale"], False, "") + '": ' + str(e))

        # Add all layouts to list
        for name in listFolders(self.getConfig(["dirs", "layouts"])):
            self._layouts[name] = Layout(self, name)

        # Add all site directories to list
        for s in listFolders(self.getConfig(["dirs", "sites"])):
            site = Site(s, self)
            self._sites.append(site)
            site.read()

        # copy files to out dir
        for s in self._sites:
            s.copy()
예제 #3
0
파일: data.py 프로젝트: svenhertle/stawebg
    def read(self):
        # read site specific config
        filename = os.path.join(self.getConfig(["dirs", "sites"]), self._name + ".json")
        if not os.path.isfile(filename):
            fail("Can't find config file: " + filename)
        site_config = Config(filename, Config.site_struct)
        self._config = Config.merge(self._config, site_config, True)

        # create file index
        path = self.getAbsDestPath()
        if os.path.isdir(path):
            self._file_index = findFiles(path)

        # read all pages
        self._readHelper(self.getAbsSrcPath(), self._root)
예제 #4
0
파일: data.py 프로젝트: svenhertle/stawebg
    def _readHelper(self, dir_path, parent, dir_hidden=False, blog_data_dir=False, page_config=None):
        index_rename = None
        if page_config:
            index_rename = page_config.get(["files", "rename", os.path.basename(dir_path)], False)
            page_config.delete(["files", "sort"], False)
            page_config.delete(["files", "rename"], False)
            page_config.delete(["blog"], False)
        else:
            page_config = self._config

        entries = sorted(os.listdir(dir_path))
        blog = None

        idx = None
        if not blog_data_dir:
            if "stawebg.json" in entries:
                tmp_config = Config(os.path.join(dir_path, "stawebg.json"), Config.directory_struct)
                page_config = Config.merge(page_config, tmp_config, False)

            # Add layout to list -> copy later
            layout = self._project.getLayout(page_config.get(["layout"], False))
            if layout not in self._layouts:
                self._layouts.append(layout)

            # Create blog, if there is config for it
            if page_config.get(["blog"], False):
                blog = Blog(dir_path, page_config, self)

            # First we have to find the index file in this directory…
            idx = None
            for f in entries:
                absf = os.path.join(dir_path, f)
                if isFile(absf) and isCont(absf, self) and isIndex(absf, self):
                    if index_rename:
                        page_config.add(["files", "rename", f], index_rename)
                    idx = Page(
                        os.path.split(dir_path)[1],
                        absf,
                        self,
                        parent,
                        dir_hidden or isHidden(absf, self, page_config),
                        blog,
                        page_config,
                    )
                    entries.remove(f)
                    break
            # …or create an empty page as index
            if not idx:
                dirname = os.path.split(dir_path)[1]
                if index_rename:
                    page_config.add(["files", "rename", None], index_rename)
                idx = Page(
                    dirname, None, self, parent, dir_hidden or isHidden(dirname, self, page_config), blog, page_config
                )

            if parent:
                parent.appendPage(idx)
            else:
                self._root = idx

            # Sort entries as specified in configuration
            sorted_entries = page_config.get(["files", "sort"], False, [])
            for s in reversed(sorted_entries):
                absf = os.path.join(dir_path, s)
                if not s in entries:
                    print("\tFile not found (specified in sort): " + absf)
                else:
                    entries.remove(s)
                    entries.insert(0, s)

        # Make absolute paths and check if it's a page
        for f in entries:
            absf = os.path.join(dir_path, f)
            if isExcluded(absf, self, page_config):
                continue
            hidden = dir_hidden or isHidden(absf, self, page_config)
            new_blog_data_dir = blog_data_dir or (blog and os.path.samefile(absf, blog.getAbsDir()))

            # Content file -> Page
            if isFile(absf) and isCont(absf, self):
                if new_blog_data_dir:
                    continue
                print("\tFound page: " + absf)
                idx.appendPage(Page(os.path.splitext(f)[0], absf, self, idx, hidden, blog, page_config))
            # Directory -> Go inside
            elif os.path.isdir(absf):
                print("\tFound dir:  " + absf)
                self._readHelper(absf, idx, hidden, new_blog_data_dir, page_config.copy())
            # Unknown object
            else:
                tmp = OtherFile(
                    self.getAbsSrcPath(), os.path.relpath(absf, self.getAbsSrcPath()), self.getAbsDestPath()
                )
                self._other_files.append(tmp)
                print("\tFound unkown object: " + absf)