Example #1
0
    def __init__(self, site, path, source=None):
        '''Initialize an entry

        This involves change of base class by running static method Class.check
        of every member of models.TYPE_LIST.

        Arguments:

         - `site`: site this entry belongs to
         - `path`: relative path to source template and to result
         - `source`: optional source template path. Can be used to trick
            system to have virtual entries (with no real equivalent in
            source directory)
        '''
        self.site = site
        self.path = path
        self.source = source
        self.mtime = self.get_mtime()

        self.settings = Settings(parent=self.site.settings)

        base = '_%s.html' % self.__class__.__name__.lower()
        if op.exists(op.join(self.site.root, base)):
            self.settings.parent_tmpl = base

        self.template = self.get_template()
        self.settings.base, self.settings.slug = op.split(self.path)
        self.collect()
        if hasattr(self, 'init'):
            self.init()
Example #2
0
    def __init__(self, root, dest):
        self.root = root
        if op.exists(dest):
            shutil.rmtree(dest)

        self.settings = Settings(parent_tmpl='_base.html')

        conf = op.join(self.root, '.cyrax.cfg')
        if not op.exists(conf):
            conf = op.join(self.root, 'settings.cfg')
            if op.exists(conf):
                logger.warn('settings.cfg is deprecated, please rename to .cyrax.cfg')
        if op.exists(conf):
            self.settings.read(open(conf, 'rt').read())

        site_base_path = base_path(self.url)
        self.dest = op.join(dest, url2path(site_base_path[1:]))

        self.env = initialize_env(root)
        self.env.globals['site'] = self
        self.entries = []

        if self.settings.get('sitecallback'):
            callback = impcallback(self.settings.sitecallback, self.root)
            callback(self)

        self._traverse()
Example #3
0
    def __init__(self, site, path, source=None):
        '''Initialize an entry

        This involves change of base class by running static method Class.check
        of every member of models.TYPE_LIST.

        Arguments:

         - `site`: site this entry belongs to
         - `path`: relative path to source template and to result
         - `source`: optional source template path. Can be used to trick
            system to have virtual entries (with no real equivalent in
            source directory)
        '''
        self.site = site
        self.path = path
        self.source = source
        self.mtime = self.get_mtime()

        self.settings = Settings(parent=self.site.settings)

        base = '_%s.html' % self.__class__.__name__.lower()
        if op.exists(op.join(self.site.root, base)):
            self.settings.parent_tmpl = base

        self.template = self.get_template()
        self.settings.base, self.settings.slug = op.split(self.path)
        self.collect()
        if hasattr(self, 'init'):
            self.init()
Example #4
0
    def __init__(self, root, dest):
        self.root = root
        if op.exists(dest):
            shutil.rmtree(dest)

        self.settings = Settings(parent_tmpl='_base.html')
        conf = op.join(self.root, 'settings.cfg')
        if op.exists(conf):
            self.settings.read(file(conf).read().decode('utf-8'))

        site_base_path = base_path(self.url)
        self.dest = op.join(dest, url2path(site_base_path[1:]))

        self.env = initialize_env(root)
        self.env.globals['site'] = self
        self.entries = []

        if self.settings.get('sitecallback'):
            callback = impcallback(self.settings.sitecallback, self.root)
            callback(self)

        self._traverse()
Example #5
0
class Site(object):
    def __init__(self, root, dest):
        self.root = root
        if op.exists(dest):
            shutil.rmtree(dest)

        self.settings = Settings(parent_tmpl='_base.html')
        conf = op.join(self.root, 'settings.cfg')
        if op.exists(conf):
            self.settings.read(file(conf).read().decode('utf-8'))

        site_base_path = base_path(self.url)
        self.dest = op.join(dest, url2path(site_base_path[1:]))

        self.env = initialize_env(root)
        self.env.globals['site'] = self
        self.entries = []

        if self.settings.get('sitecallback'):
            callback = impcallback(self.settings.sitecallback, self.root)
            callback(self)

        self._traverse()

    @property
    def url(self):
        return getattr(self.settings, 'url', '/')

    def __repr__(self):
        return '<Site: %r>' % self.root

    def __getitem__(self, name):
        return self.settings[name]

    def __getattr__(self, name):
        try:
            return self.settings[name]
        except KeyError, e:
            raise AttributeError(str(e))
Example #6
0
    def __init__(self, site, path, source=None):
        self.site = site
        self.path = path
        self.source = source
        self.mtime = self.get_mtime()

        self.settings = Settings(parent=self.site.settings)

        base = '_%s.html' % self.__class__.__name__.lower()
        if op.exists(op.join(self.site.root, base)):
            self.settings.parent_tmpl = base

        self.settings.base, self.settings.slug = op.split(self.path)
        self.collect()
        if hasattr(self, 'init'):
            self.init()
Example #7
0
class Untouchable(Entry):
    @staticmethod

    def check(site, path):
        extension = path.split('.')[-1]
        touchables = ('html', 'css', 'js')
        return extension not in touchables

    def __init__(self, site, path, source=None):
        self.site = site
        self.path = path
        self.source = source
        self.mtime = self.get_mtime()

        self.settings = Settings(parent=self.site.settings)

        base = '_%s.html' % self.__class__.__name__.lower()
        if op.exists(op.join(self.site.root, base)):
            self.settings.parent_tmpl = base

        self.settings.base, self.settings.slug = op.split(self.path)
        self.collect()
        if hasattr(self, 'init'):
            self.init()

    def isdir(self):
        return self.settings.get('isdir', False)

    def render(self):
        logger.info('Copying untouchable file %r' % self)
        destpath = self.get_dest()
        makedirs(op.dirname(destpath))
        shutil.copyfile(self.path, destpath)

    def collect(self):
        pass
Example #8
0
class Entry(object):
    def __init__(self, site, path, source=None):
        '''Initialize an entry

        This involves change of base class by running static method Class.check
        of every member of models.TYPE_LIST.

        Arguments:

         - `site`: site this entry belongs to
         - `path`: relative path to source template and to result
         - `source`: optional source template path. Can be used to trick
            system to have virtual entries (with no real equivalent in
            source directory)
        '''
        self.site = site
        self.path = path
        self.source = source
        self.mtime = self.get_mtime()

        self.settings = Settings(parent=self.site.settings)

        base = '_%s.html' % self.__class__.__name__.lower()
        if op.exists(op.join(self.site.root, base)):
            self.settings.parent_tmpl = base

        self.template = self.get_template()
        self.settings.base, self.settings.slug = op.split(self.path)
        self.collect()
        if hasattr(self, 'init'):
            self.init()

    def __repr__(self):
        return '<%s: %r>' % (self.__class__.__name__, self.path)

    def __getitem__(self, name):
        return self.settings[name]

    def __getattr__(self, name):
        try:
            return self.settings[name]
        except KeyError:
            raise AttributeError(name)

    def get_mtime(self):
        '''Determine modification time (with date)

        As in modification time vs creation time.

        If source is not null and path does not exist, then entry is virtual and
        returns current datetime.
        '''
        path = op.join(self.site.root, self.path)
        if self.source and not op.exists(path):
            return datetime.datetime.now()
        mtime = op.getmtime(path)
        return datetime.datetime(*time.gmtime(mtime)[:6])

    def get_template(self):
        '''Get Jinja2 template of entry to render
        '''
        return self.site.env.get_template(self.source or self.path,
                                          globals={'entry': self})

    def collect(self):
        '''Collect settings information from entry

        Renders Jinja2 template to get this information from {% meta %}
        '''
        self.template.render()

    def isdir(self):
        '''Determines if entry should be rendered as directory
        '''
        return self.settings.get('isdir', True)

    def get_dest(self):
        path = op.join(self.site.dest, url2path(self.get_relative_url()))
        if self.isdir():
            path = op.join(path, 'index.html')
        return path

    def get_relative_url(self):
        return self.path

    def get_url(self):
        return safe_url_join(base_path(self.site.url), self.get_relative_url())

    def get_absolute_url(self):
        return safe_url_join(self.site.url, self.get_relative_url())

    def render(self):
        logger.info('Rendering %r' % self)
        # workaround for a dumb bug
        # no ideas why but all tag templates contain same self inside
        self.template.globals['entry'] = self
        path = self.get_dest()
        makedirs(op.dirname(path))
        open(path, 'wt', encoding='utf-8').write(self.template.render())
Example #9
0
class Entry(object):
    def __init__(self, site, path, source=None):
        '''Initialize an entry

        This involves change of base class by running static method Class.check
        of every member of models.TYPE_LIST.

        Arguments:

         - `site`: site this entry belongs to
         - `path`: relative path to source template and to result
         - `source`: optional source template path. Can be used to trick
            system to have virtual entries (with no real equivalent in
            source directory)
        '''
        self.site = site
        self.path = path
        self.source = source
        self.mtime = self.get_mtime()

        self.settings = Settings(parent=self.site.settings)

        base = '_%s.html' % self.__class__.__name__.lower()
        if op.exists(op.join(self.site.root, base)):
            self.settings.parent_tmpl = base

        self.template = self.get_template()
        self.settings.base, self.settings.slug = op.split(self.path)
        self.collect()
        if hasattr(self, 'init'):
            self.init()

    def __repr__(self):
        return '<%s: %r>' % (self.__class__.__name__, self.path)

    def __getitem__(self, name):
        return self.settings[name]

    def __getattr__(self, name):
        try:
            return self.settings[name]
        except KeyError:
            raise AttributeError(name)

    def get_mtime(self):
        '''Determine modification time (with date)

        As in modification time vs creation time.

        If source is not null and path does not exist, then entry is virtual and
        returns current datetime.
        '''
        path = op.join(self.site.root, self.path)
        if self.source and not op.exists(path):
            return datetime.datetime.now()
        mtime = op.getmtime(path)
        return datetime.datetime(*time.gmtime(mtime)[:6])

    def get_template(self):
        '''Get Jinja2 template of entry to render
        '''
        return self.site.env.get_template(self.source or self.path,
                                          globals={'entry': self})

    def collect(self):
        '''Collect settings information from entry

        Renders Jinja2 template to get this information from {% meta %}
        '''
        self.template.render()

    def isdir(self):
        '''Determines if entry should be rendered as directory
        '''
        return self.settings.get('isdir', True)

    def get_dest(self):
        path = op.join(self.site.dest, url2path(self.get_relative_url()))
        if self.isdir():
            path = op.join(path, 'index.html')
        return path

    def get_relative_url(self):
        return self.path

    def get_url(self):
        return safe_url_join(base_path(self.site.url), self.get_relative_url())

    def get_absolute_url(self):
        return safe_url_join(self.site.url, self.get_relative_url())

    def render(self):
        logger.info('Rendering %r' % self)
        # workaround for a dumb bug
        # no ideas why but all tag templates contain same self inside
        self.template.globals['entry'] = self
        path = self.get_dest()
        makedirs(op.dirname(path))
        file(path, 'w').write(self.template.render().encode('utf-8'))
Example #10
0
class Site(object):
    def __init__(self, root, dest):
        self.root = root
        if op.exists(dest):
            shutil.rmtree(dest)

        self.settings = Settings(parent_tmpl='_base.html')

        conf = op.join(self.root, '.cyrax.cfg')
        if not op.exists(conf):
            conf = op.join(self.root, 'settings.cfg')
            if op.exists(conf):
                logger.warn('settings.cfg is deprecated, please rename to .cyrax.cfg')
        if op.exists(conf):
            self.settings.read(open(conf, 'rt').read())

        site_base_path = base_path(self.url)
        self.dest = op.join(dest, url2path(site_base_path[1:]))

        self.env = initialize_env(root)
        self.env.globals['site'] = self
        self.entries = []

        if self.settings.get('sitecallback'):
            callback = impcallback(self.settings.sitecallback, self.root)
            callback(self)

        self._traverse()

    @property
    def url(self):
        return getattr(self.settings, 'url', '/')

    def __repr__(self):
        return '<Site: %r>' % self.root

    def __getitem__(self, name):
        return self.settings[name]

    def __getattr__(self, name):
        try:
            return self.settings[name]
        except KeyError as e:
            raise AttributeError(str(e))

    def _traverse(self):
        events.emit('traverse-started', site=self)
        exclude = self.settings.get('exclude', [])

        for path, _, files in os.walk(self.root):
            relative = path[len(self.root):].lstrip(os.sep)
            if (not relative.startswith('static') and
                not relative.startswith('utils') and
                not any(map(ishidden, relative.split(op.sep)))):
                for f in files:
                    full = op.join(relative, f)
                    if (f != 'settings.cfg' and
                        not ishidden(f) and
                        not any(map(lambda x: fnmatch(full, x), exclude))):
                        self.add_page(op.join(relative, f).replace('\\', '/'))

        events.emit('site-traversed', site=self)

    def add_page(self, path):
        self.entries.append(get_entry(self, path))

    def render(self):
        for entry in self.entries:
            entry.render()
        events.emit('site-rendered', site=self)
        self._copy_static()

    def _copy_static(self):
        if op.exists(op.join(self.root, 'static')):
            logger.info('Copying static files')
            shutil.copytree(op.join(self.root, 'static'),
                            op.join(self.dest, 'static'))