def __init__(self): search_path = [settings.TEMPLATE_DIR] for module in settings.MODULES: path = os.path.join( os.path.abspath(os.path.dirname(import_path(module).__file__)), 'templates' ) if os.path.isdir(path): search_path.append(path) loader = jinja2.FileSystemLoader(search_path) self.env = jinja2.Environment( trim_blocks=True, undefined=jinja2.StrictUndefined, autoescape=True, loader=loader ) self.env.globals.update({ 'AUTHOR': settings.AUTHOR, 'AUTHOR_EMAIL': settings.AUTHOR_EMAIL, 'SITE_ROOT': settings.SITE_ROOT, 'url': routes.mapper.url, 'absurl': routes.mapper.absurl, }) self.env.filters.update({ 'paragraphs': paragraphs, }) for module in settings.MODULES: module = import_path(module, 'templating', always_fail=False) if module: self.env.globals.update(module.context_processor())
def main(): logging.basicConfig(level=logging.DEBUG) parser = argparse.ArgumentParser() subparsers = parser.add_subparsers() commands_dir = os.path.join(os.path.dirname(commands.__file__)) for filename in glob.iglob(os.path.join(commands_dir, '*.py')): filename = os.path.basename(filename) module_name = os.path.splitext(filename)[0] module = import_path('staticsauce', 'commands', module_name) for name, obj in inspect.getmembers(module, inspect.isclass): if issubclass(obj, commands.Command): command = obj() subparser = subparsers.add_parser(command.command) command.init_parser(subparser) subparser.set_defaults(_command=command) args = parser.parse_args() kwargs = { name: obj for name, obj in inspect.getmembers(args) if not name.startswith('_') } command = args._command command(**kwargs)
def extend(self, path, prefix=None): module = import_path(path) mapper = module.mapper() for name, route in mapper: filename = route.filename if prefix is not None: filename = path_append(prefix, filename) self.add( name, filename, route.controller, route.permutations, route.kwargs )
def __call__(self): self.copy_public_dir() for name, route in routes.mapper: filename = path_append(settings.BUILD_DIR, route.filename) module, controller = route.controller.rsplit('.', 1) module = import_path(module) controller = getattr(module, controller) permutations = route.permutations \ if route.permutations is not None else [{}] for permutation in permutations: fmt_filename = filename.format(**permutation) try: os.makedirs(os.path.dirname(fmt_filename)) except OSError as err: if err.errno != errno.EEXIST: raise uri = 'http://{domain}{path}'.format( domain=settings.SITE_DOMAIN, path=route.filename ) static_file = StaticFile(fmt_filename, uri) kwargs = {} if route.kwargs: kwargs.update(route.kwargs) kwargs.update(permutation) try: controller(static_file, **kwargs) except AlreadyUpdatedError: pass else: self.logger.info("[%(controller)s] %(filename)s", { 'controller': route.controller, 'filename': fmt_filename, }) static_file.save(fmt_filename)
def menus(): module = import_path('data.nav') return module.menus
def albums(): module = import_path('data.gallery') return sorted(module.albums, key=lambda album: album.date, reverse=True)
def articles(): module = import_path('data.blog') return sorted( module.articles, key=lambda article: article.date, reverse=True )
for name, route in mapper: filename = route.filename if prefix is not None: filename = path_append(prefix, filename) self.add( name, filename, route.controller, route.permutations, route.kwargs ) def __iter__(self): return iter(self._routes.items()) def url(self, name, **kwargs): return path_append( settings.SITE_ROOT, self._routes[name].filename.format(**kwargs) ) def absurl(self, name, **kwargs): return 'http://{domain}{path}'.format( domain=settings.SITE_DOMAIN, path=self.url(name, **kwargs) ) if settings is not None: mapper = import_path(settings.ROUTES).mapper()
# This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import sys from staticsauce.utils import import_path try: import settings except ImportError: traceback = sys.exc_info()[2] if traceback.tb_next: raise settings = None if settings is not None: for module in settings.MODULES: module_parts = module.rsplit('.', 1) module_name = module_parts[-1] if not hasattr(settings, module_name): module = import_path(module, 'settings', always_fail=False) if module: setattr(settings, module_name, module)