コード例 #1
0
ファイル: core.py プロジェクト: malkarouri/Clay
    def __init__(self, base_dir, settings=None, source_dir=config.SOURCE_DIR):
        if os.path.isfile(base_dir):
            base_dir = os.path.abspath(os.path.dirname(base_dir))
        self.base_dir = base_dir
        self.source_dir = utils.make_dirs(base_dir, source_dir)
        self.build_dir = os.path.join(base_dir, config.BUILD_DIR)

        settings = settings or {}
        self.settings = Settings(config.default_settings, settings,
            case_insensitive=True)

        theme_prefix = self.settings.get('theme_prefix', '').rstrip('/')
        if theme_prefix:
            theme_prefix += '/'
        self.settings['theme_prefix'] = theme_prefix

        views_ignore = self.settings.get('views_ignore', [])
        self.settings['views_ignore'] = tuple(views_ignore)

        self.app = Shake(config.app_settings)
        self._make_render()
        self._enable_pre_processors()
        self._add_urls()
コード例 #2
0
ファイル: core.py プロジェクト: plaes/Clay
    def __init__(self, base_dir, settings=None, source_dir=c.SOURCE_DIR):
        base_dir = normpath(abspath(realpath(base_dir)))
        if not isdir(base_dir):
            base_dir = dirname(base_dir)
        self.base_dir = u.to_unicode(base_dir)
        self.source_dir = u.make_dirs(base_dir, source_dir)
        self.build_dir = join(base_dir, c.BUILD_DIR)
 
        settings = settings or {}
        self.settings = Settings(settings, c.default_settings)

        theme_prefix = self.settings.get('theme_prefix', '').strip('/')
        if theme_prefix:
            theme_prefix += '/'
        self.settings['theme_prefix'] = theme_prefix

        views_ignore = self.settings.get('views_ignore', [])
        self.settings['views_ignore'] = tuple(views_ignore)

        self.app = Shake(__file__, c.app_settings)
        self._make_render()
        self._enable_pre_processors()
        self._add_urls()
コード例 #3
0
ファイル: core.py プロジェクト: malkarouri/Clay
class Clay(object):

    def __init__(self, base_dir, settings=None, source_dir=config.SOURCE_DIR):
        if os.path.isfile(base_dir):
            base_dir = os.path.abspath(os.path.dirname(base_dir))
        self.base_dir = base_dir
        self.source_dir = utils.make_dirs(base_dir, source_dir)
        self.build_dir = os.path.join(base_dir, config.BUILD_DIR)

        settings = settings or {}
        self.settings = Settings(config.default_settings, settings,
            case_insensitive=True)

        theme_prefix = self.settings.get('theme_prefix', '').rstrip('/')
        if theme_prefix:
            theme_prefix += '/'
        self.settings['theme_prefix'] = theme_prefix

        views_ignore = self.settings.get('views_ignore', [])
        self.settings['views_ignore'] = tuple(views_ignore)

        self.app = Shake(config.app_settings)
        self._make_render()
        self._enable_pre_processors()
        self._add_urls()

    def _make_render(self):
        loader = ChoiceLoader([
            FileSystemLoader(self.source_dir),
            PackageLoader('clay', config.SOURCE_DIR),
        ])
        self.render = Render(loader=loader)
        self.render.set_filter('json', utils.filter_to_json)

    def _enable_pre_processors(self):
        ext_trans = {}
        processors = self.settings.pre_processors

        for name in processors:
            pr = globals().get('p_' + name)
            if pr and pr.enabled:
                pr.add_extensions(self)
                for ext in pr.extensions_in:
                    ext_trans[ext] = pr.extension_out

        self.ext_trans = ext_trans

    def _add_urls(self):
        self.app.add_urls([
            Rule('/', self.render_view),
            Rule('/<path:path>', self.render_view),
        ])

    def _normalize_path(self, path):
        if '..' in path:
            return self.not_found()
        path = path.strip('/')
        is_dir = os.path.isdir(os.path.join(self.source_dir, path))
        if is_dir:
            path += '/'
        if not path or is_dir:
            path += 'index.html'
        return path

    def _translate_ext(self, old_ext):
        return self.ext_trans.get(old_ext, old_ext)

    def _get_alternative(self, path):
        path = path.strip('/')
        fname, ext = os.path.splitext(path)
        fullpath = os.path.join(config.DEFAULT_TEMPLATES, path)
        if os.path.exists(fullpath):
            return ext, path, fullpath
        
        if path != 'index.html':
            return None, None, None

        pdir = os.path.join(self.source_dir, fname + '.*')
        files = glob.glob(pdir)
        if files:
            fullpath = files[0]
            path = fullpath.replace(self.source_dir, '')
            _, ext = os.path.splitext(path)
            return ext, path, fullpath

        return None, None, None

    def _post_process(self, html):
        html = utils.to_unicode(html)
        processors = self.settings.post_processors

        for name in processors:
            pp = globals().get('pp_' + name)
            if pp and pp.enabled:
                html = pp.process(html)

        return html

    def run(self, host=None, port=None):
        host = host if host is not None else self.settings.HOST
        port = port if port is not None else self.settings.PORT
        try:
            port = int(port)
        except Exception:
            port = self.settings.PORT
        ips = [ip for ip in socket.gethostbyname_ex(socket.gethostname())[2]
            if not ip.startswith("127.")][:1]
        if ips:
            print ' * Your local IP is:', ips[0]
        try:
            self.app.run(host=host, port=port)
        except socket.error, e:
            print e