Example #1
0
    def handle(self, app, serve, no_index):
        app.jinja_loader  # ugly workaround
        app.blohg.init_repo(REVISION_DEFAULT)

        app.url_map = self.remap_rules(app.url_map, no_index)

        # That's a risky one, it woud be better to give a parameter to the
        # freezer
        app.root_path = app.config.get('REPO_PATH')

        freezer = Freezer(app)

        def static_generator(static_dir):
            for f in app.blohg.changectx.files:
                if f.startswith(static_dir):
                    yield dict(filename=f[len(static_dir):] \
                               .strip(posixpath.sep))

        @freezer.register_generator
        def static():
            """Walk the static dir and freeze everything"""
            return static_generator('static')

        @freezer.register_generator
        def attachments():
            """Walk the attachment dir and freeze everything"""
            return static_generator(app.config['ATTACHMENT_DIR'])

        freezer.freeze()
        if serve:
            freezer.serve()
Example #2
0
def freeze(destination):
    '''Freeze the wiki to the destination directory.'''
    # If the freezer is provided a relative URL then it behaves by trying to
    # write to package area. This won't work if the package is installed so
    # convert the relative path to an absolute one.
    if not os.path.isabs(destination):
        destination = os.path.join(os.getcwd(), destination)

    # Set MarkWiki in a freezing mode so that certain elements of the views
    # won't get rendered.
    app.config['FREEZING'] = True

    app.config['FREEZER_IGNORE_MIMETYPE_WARNINGS'] = True
    app.config['FREEZER_DESTINATION'] = destination

    # The freeze operation is destructive because it follows the delete
    # links. Copy the wiki into a temporary area and work with that instead.
    _create_wiki_copy()

    freezer = Freezer(app)

    # Add the URL generator to suppress a warning. It won't really do anything.
    @freezer.register_generator
    def delete():
        yield {'page_path': 'Home'}

    try:
        freezer.freeze()
    except OSError:
        return ('Failed to freeze the MarkWiki. Do you have permission to '
                'write to the destination directory?')

    return _prune_frozen(destination)
Example #3
0
def main():
    server.app.config['FREEZER_DESTINATION'] = config.OPTIONS['destination']
    server.app.config['FREEZER_BASE_URL'] = config.OPTIONS['baseurl']
    freezer = Freezer(server.app)
    freezer.freeze()

    return freezer
def freeze_topic_model_browser():
    global token
    topic_model_browser_app.config.update(
        FREEZER_BASE_URL='http://mediamining.univ-lyon2.fr/people/guille/cats/tom/' + token,
        FREEZER_DESTINATION='tom/' + token,
        FREEZER_IGNORE_404_NOT_FOUND=True,
        FREEZER_REMOVE_EXTRA_FILES=False,
        FREEZER_DESTINATION_IGNORE=['.tsv', '.json']
    )
    topic_model_browser_app.debug = False
    topic_model_browser_app.testing = True
    topic_model_browser_app.config['ASSETS_DEBUG'] = False
    print('Freeze topic model browser')
    topic_model_freezer = Freezer(topic_model_browser_app)
    print('Finalizing the topic model browser...')

    @topic_model_freezer.register_generator
    def topic_details():
        for _topic_id in range(topic_model.nb_topics):
            yield {'tid': _topic_id}

    @topic_model_freezer.register_generator
    def document_details():
        for _doc_id in range(topic_model.corpus.size):
            yield {'did': _doc_id}

    @topic_model_freezer.register_generator
    def word_details():
        for _word_id in range(len(topic_model.corpus.vocabulary)):
            yield {'wid': _word_id}

    topic_model_freezer.freeze()
    print('Done.')
Example #5
0
    def take_action(self, args):
        self.app.ara.config['FREEZER_DESTINATION'] = os.path.abspath(args.path)

        if args.playbook is not None:
            self.app.ara.config['ARA_PLAYBOOK_OVERRIDE'] = args.playbook

        self.log.warning('Generating static files at %s...', args.path)
        filterwarnings('ignore', '.*', NotFoundWarning)
        if self.app.ara.config['ARA_IGNORE_EMPTY_GENERATION']:
            filterwarnings('ignore', '.*', MissingURLGeneratorWarning)
        freezer = Freezer(self.app.ara)

        # Patternfly fonts are called from inside the CSS and are therefore
        # not automatically found by flask-frozen. We need to generate URLs
        # for the fonts.
        patternfly = self.app.ara.config['XSTATIC']['patternfly']

        @freezer.register_generator
        def serve_static_packaged():
            for font in walk_directory(os.path.join(patternfly, 'fonts')):
                yield dict(
                    module='patternfly',
                    filename='fonts/%s' % font
                )
        freezer.freeze()

        print('Done.')
Example #6
0
def freeze():
    """Prepare and run the freezer"""
    app = app_factory({'FREEZER_DESTINATION': join('..', 'output')})
    app.context_processor(freezer_template_context)
    freezer = Freezer(app, with_static_files=False)
    freezer.register_generator(partial(bootstrap_fonts, app))
    freezer.freeze()
    copy_extra(freezer)
Example #7
0
def freeze():
    """
    Freeze the site into static files.
    This function has severe problem. Do not use!
    """
    from flask_frozen import Freezer
    freezer = Freezer(app)
    freezer.freeze()
Example #8
0
def build_html():
    from flask_frozen import Freezer
    from app import views
    views.app.config['FREEZER_DESTINATION'] = '../build'
    views.app.testing = True
    freezer = Freezer(views.app, with_static_files=False)
    freezer.register_generator(views.school_url_generator)
    freezer.register_generator(views.blog_url_generator)
    freezer.freeze()
Example #9
0
def freeze():
    """首页静态化"""
    from xueer.hello import hello
    from flask_frozen import Freezer

    freezer = Freezer(hello)

    if __name__ == '__main__':
        freezer.freeze()
Example #10
0
def freeze(*args):
	import shutil
	from flask import url_for

	freezer = Freezer(app)
	freezer.freeze()
	# Copy the boilerplate files (favicon, robots.txt) to the freezer
	for filename in app.config.get('BOILER_FILES'):
		shutil.copy(app.config.get('BOILER_FULL_URL') + filename, app.config.get('FREEZER_DESTINATION'))
Example #11
0
def freeze_app():
    from flask_frozen import Freezer
    debugged = app.debug
    app.debug = True
    url_rules = ('about', 'schedule', 'logistics', 'partner', 'register', 'thanks', 'downloads', 'live')
    for rule in url_rules:
        app.add_url_rule('/<lang>/{}/'.format(rule), rule, partial(page, page=rule))
    freezer = Freezer(app)
    freezer.freeze()
    app.debug = debugged
Example #12
0
def build_html():
    from flask_frozen import Freezer
    from app import views

    views.app.config["FREEZER_DESTINATION"] = "../build"
    views.app.config["FREEZER_REMOVE_EXTRA_FILES"] = False
    views.app.testing = True
    freezer = Freezer(views.app, with_static_files=False)
    freezer.register_generator(views.school_url_generator)
    freezer.register_generator(views.blog_url_generator)
    freezer.freeze()
Example #13
0
 def test_transitivity(self):
     with self.built_app() as (temp, app, freezer, urls):
         with temp_directory() as temp2:
             # Run the freezer on it's own output
             app2 = freezer.make_static_app()
             app2.config["FREEZER_DESTINATION"] = temp2
             app2.debug = True
             freezer2 = Freezer(app2)
             freezer2.register_generator(self.filenames.iterkeys)
             freezer2.freeze()
             destination = app.config["FREEZER_DESTINATION"]
             self.assertEquals(read_all(destination), read_all(temp2))
Example #14
0
File: cli.py Project: madflow/toni
def process_build(args):
    """Process the build"""
    if args.dir:
        if os.path.isdir(args.dir):
            target = args.dir
        else:
            target = os.path.join(APP_BASE_PATH, args.dir)
        app.config['FREEZER_DESTINATION'] = target
    if args.base:
        app.config['FREEZER_BASE_URL'] = args.base
    freezer = Freezer(app)
    freezer.freeze()
Example #15
0
File: run.py Project: alanhdu/blog
def build():
    """Build blog"""
    app.config["BLOG"]["posts"] = sorted(load(["posts"]),
                                         key=lambda post: post.revdate,
                                         reverse=True)
    app.config["FREEZER_BASE_URL"] = app.config["BLOG"]["base_url"]
    app.config["FREEZER_DESTINATION"] = "../build"

    app.static_url_path = app.config["BLOG"]["base_path"] + "static/"

    freezer = Freezer(app)
    freezer.freeze()
Example #16
0
def build_individual_repositories(repository_list):
    app.config['FREEZER_DESTINATION'] = os.path.abspath("static")
    app.config['FREEZER_BASE_URL'] = "https://dabo.guru/"
    app.config['FREEZER_REMOVE_EXTRA_FILES'] = False
    freezer = Freezer(app=app, with_static_files=False, with_no_argument_rules=False, log_url_for=True)
    for repository in repository_list:
        def url_generator():
            return documentation.get_possible_pages_for_frozen_flask(repository)

        freezer.register_generator(url_generator)
    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", category=MissingURLGeneratorWarning)
        freezer.freeze()
Example #17
0
def freeze():
    freezer = Freezer(app)

    @freezer.register_generator
    def page_list():
        for lang_code in ['en', 'fr']:
            yield 'pages.index', {'lang_code': lang_code}
            yield 'pages.guide', {'lang_code': lang_code}
            yield 'schedule.index', {'lang_code': lang_code}
            yield '/{}/schedule/schedule.json'.format(lang_code)
            yield '/{}/schedule/schedule.ics'.format(lang_code)

    freezer.freeze()
Example #18
0
    def run(self):
        app = self.app
        app.init()
        freezer = Freezer(app)

        logging.getLogger().setLevel(logging.INFO)
        logging.info('Generating static pages...')

        @freezer.register_generator
        def generate_pages():
            for p in app.pages:
                if p.path == 'index':
                    yield '/'
                # logging.info('Generating %s' % p.url)
                yield p.url

        @freezer.register_generator
        def generate_datafiles():
            yield '/api/pages/all.json'
            yield '/api/posts.json'
            for p in app.pages:
                yield '/api/pages/%s.json' % p.path

        @freezer.register_generator
        def generate_attachments():
            for p in app.pages:
                res_path = path.join(path.dirname(p._filename), p.name)
                if path.exists(res_path):
                    for root, dirs, files in walk(res_path):
                        for f in files:
                            if not f.endswith('.md'):
                                f_p = path.join(path.dirname(p._filename), p.name, f)
                                if path.exists(f_p):
                                    yield p.url + '/' + f

        @freezer.register_generator
        def generate_archives():
            for archive in app.site.archives:
                yield '/archives/%s' % archive.title

        freezer.freeze()
        logging.info('Generation completed.Doing cleanup....')

        for root, dirs, files in walk(path.join(getcwd(), 'builds')):
            for d in dirs:
                if path.basename(d) in ['.webassets-cache', '_styles', '_scripts']:
                    rmtree(path.join(root, d))

            for f in files:
                if path.splitext(f)[1] in ['.less', '.scss', '.coffee']:
                    remove(path.join(root, f))
Example #19
0
def generate():
	app.config['FREEZER_DESTINATION'] = settings.web_app_location
	app.config['FREEZER_BASE_URL'] = settings.external_url

	freezer = Freezer(app)

	copy = copytext.Copy(settings.copy_sheet_location)
	@freezer.register_generator
	def candidate_page():
		for sheetName in copy.sheetNames():
			if sheetName == 'metadata' or sheetName == 'Attribution': continue
			for row in copy[sheetName]:
				yield {"candidate_id": row['Candidate Name'].unescape().replace(" ", "_").replace("/", "_")}
				
	freezer.freeze()
def freeze_event_browser():
    global topic_associations, author_list, token
    os.makedirs('mabed/'+token+'/static/data')
    print('Freeze event browser')
    event_browser_app.config.update(
        FREEZER_BASE_URL='http://mediamining.univ-lyon2.fr/people/guille/cats/mabed/'+token,
        FREEZER_DESTINATION='mabed/'+token,
        FREEZER_IGNORE_404_NOT_FOUND=True,
        FREEZER_REMOVE_EXTRA_FILES=False,
    )
    event_browser_freezer = Freezer(event_browser_app)
    event_browser_app.debug = False
    event_browser_app.testing = True
    event_browser_app.config['ASSETS_DEBUG'] = False
    event_browser_freezer.freeze()
    print('Done.')
Example #21
0
def build_pages():
    ignored = [bundle.output for bundle in assets] + ["rust"]
    app.config['FREEZER_DESTINATION'] = os.path.abspath("static")
    app.config['FREEZER_BASE_URL'] = "https://dabo.guru/"
    app.config['FREEZER_DESTINATION_IGNORE'] = ignored
    app.config['FREEZER_IGNORE_ENDPOINTS'] = ['oauth_respond', 'mc_api_name', 'mc_api_uuid', 'serve_markdown',
                                              'uuid_api']
    freezer = Freezer(app=app, with_static_files=False, with_no_argument_rules=False, log_url_for=True)
    freezer.register_generator(no_argument_rules_urls_with_ignore)
    freezer.register_generator(markdown_url_generator)
    freezer.register_generator(lambda: completely_static_files)
    print("Updating documentation")
    documentation.update_all_repositories(config)
    print("Freezing")
    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", category=MissingURLGeneratorWarning)
        freezer.freeze()
def _generate(app, destination):
    app.config.update(
        FREEZER_RELATIVE_URLS=True
      , FREEZER_DESTINATION=destination
    )

    freezer_config = app.config['generator_config'].get('freezer_config', None)
    if freezer_config:
        app.config.update(freezer_config)

    freezer = Freezer( app
                       # We'll have to request all pages manually. This
                       # way we can create some endpoints pro forma
                       # without yielding 404 errors.
                     , with_no_argument_rules=False)
    freezer.register_generator(app.config['menu'].allLinks)
    freezer.freeze()
Example #23
0
def build(config_overrides):
    app = create_app(config_overrides)
    app.config['FREEZER_REMOVE_EXTRA_FILES'] = True
    freezer = Freezer(app)

    @freezer.register_generator
    def page_url_generator():
        cms = get_or_create_cms()
        contents = cms.structure()
        folder_names = contents['folders'].keys()
        for folder in folder_names:
            yield 'page_router.folder_index', {'folder_name': folder}
            page_names = contents['folders'][folder]['pages'].keys()
            for page in page_names:
                yield 'page_router.render_page', {'folder_name': folder,
                                                  'page_name': page}

    freezer.freeze()
def freeze_the_app():
    app.config['JSMAIN'] = 'main-built'
    freezer = Freezer(app)
    subprocess.call(['compass compile -e production --force'], shell=True)
    subprocess.call(['r.js -o build.js'], shell=True)
    freezer.freeze()
Example #25
0
def build():
    freezer = Freezer(app)
    freezer.freeze()
Example #26
0
 def build(self, output_path=os.path.join(os.getcwd(), "build")):
     self.app.config["FREEZER_DESTINATION"] = output_path
     freezer = Freezer(self.app)
     freezer.freeze()
Example #27
0
def export_static():
    freezer = Freezer(app)
    freezer.freeze()
"""
@cube.register_generator
def page():
    for page in pages:
      yield {'path': page['permalink']}

"""
Check to ensure that the application is loaded through
a virtual enviornment. If it is not, prompt the user
to either install a virtual enviornment or activate
the one with the applciation.
"""
if __name__ == "__main__":
    if "VIRTUAL_ENV" not in os.environ:
        print("""
        Your Virtual Environment or virtualenv has not been activated.

        To use this application, please activate it by executing:
        
          source venv/bin/activate
          
        If the problem persists, ensure that virtualenv is installed:
        
          pip install virtualenv
          
        and that all other requirements have been satisfied.
        """)
    elif len(sys.argv) > 1 and sys.argv[1] == "build":
        cube.freeze()
    else:
        commonscloudstatic.run(port=commonscloudstatic.config['PORT'])
Example #29
0
def main():
    app.config['FREEZER_DESTINATION'] = '../build'
    freezer = Freezer(app)
    freezer.freeze()
    with open(Path(freezer.root) / 'CNAME', 'w') as f:
        f.write('transparenz.flipdot.org')
Example #30
0
def create_jamstack():
    app.config["FREEZER_IGNORE_MIMETYPE_WARNINGS"] = True
    app.config["FREEZER_DESTINATION"] = get_path("output") / "static_html"
    freezer = Freezer(app)
    freezer.freeze()
Example #31
0

@app.route("/resume/")
def resume():
    return render_template('resume.html')


@app.route("/projects/")
def projects():
    return render_template('projects.html')


# Removing Resume Download for static version
# @app.route("/show/")
# def show_static_pdf():
#     file_url = os.path.join(current_app.root_path, 'static/DavidKucherResume.pdf')
#     return send_file(file_url, mimetype='pdf',
#                      download_name='DavidKucherResume.pdf',
#                      as_attachment=True)

freezer = Freezer(app)

if __name__ == '__main__':
    if len(sys.argv) > 1:
        if sys.argv[1] == 'freeze':
            res = freezer.freeze()
        elif sys.argv[1] == 'serve':
            res = freezer.run()
    else:
        app.run(port=8888)
    with 'index.html' at the end.

    :param page_path: str
    :return: str
    """
    if not page_path.endswith('/'):
        page_path += '/'
    return process_page(page_path + 'index')


@app.after_request
def add_header(request):
    request.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
    request.headers["Pragma"] = "no-cache"
    request.headers["Expires"] = "0"
    request.headers['Cache-Control'] = 'public, max-age=0'
    return request


if __name__ == '__main__':
    if len(sys.argv) > 1 and sys.argv[1] == "build":
        build_mode = True
        urls = freezer.freeze()
        generate_sitemap(urls)
        if len(build_errors) > 0:
            for error in build_errors:
                sys.stderr.write(error + '\n')
            sys.exit(-1)
    else:
        app.run(host="0.0.0.0", debug=True, threaded=True)
Example #33
0
class TarbellSite:
    def __init__(self, path, client_secrets_path=None, quiet=False):
        self.app = Flask(__name__)

        self.quiet = quiet

        self.app.jinja_env.finalize = silent_none  # Don't print "None"
        self.app.debug = True  # Always debug

        self.path = path
        self.project, self.base = self.load_project(path)

        self.data = {}
        self.hooks = self.process_hooks(hooks)
        self.expires = 0

        # add routes
        self.app.add_url_rule('/', view_func=self.preview)
        self.app.add_url_rule('/<path:path>', view_func=self.preview)
        self.app.add_url_rule('/data.json', view_func=self.data_json)

        self.app.register_blueprint(filters)

        self.app.before_request(self.add_site_to_context)
        self.app.after_request(self.never_cache_preview)

        # centralized freezer setup
        self.app.config.setdefault('FREEZER_RELATIVE_URLS', True)
        self.app.config.setdefault('FREEZER_REMOVE_EXTRA_FILES', False)
        self.app.config.setdefault('FREEZER_DESTINATION', 
            os.path.join(os.path.realpath(self.path), '_site'))

        self.freezer = Freezer(self.app) 
        self.freezer.register_generator(self.find_files)

    def add_site_to_context(self):
        """
        Add current Tarbell object to Flask's `g`
        """
        g.current_site = self

    def never_cache_preview(self, response):
        """
        Ensure preview is never cached
        """
        response.cache_control.max_age = 0
        response.cache_control.no_cache = True
        response.cache_control.must_revalidate = True
        response.cache_control.no_store = True
        return response


    def process_hooks(self, hooks):
        """
        Process all project hooks
        """
        try:
            enabled_hooks = self.project.HOOKS
        except AttributeError:
            return hooks

    def call_hook(self, hook, *args, **kwargs):
        """
        Calls each registered hook
        """
        for function in self.hooks[hook]:
            function.__call__(*args, **kwargs)

    def _get_base(self, path):
        """
        Get project blueprint
        """
        base = None

        # Slightly ugly DRY violation for backwards compatibility with old
        # "_base" convention
        if os.path.isdir(os.path.join(path, "_blueprint")):
            base_dir = os.path.join(path, "_blueprint/")
            # Get the blueprint template and register it as a blueprint
            if os.path.exists(os.path.join(base_dir, "blueprint.py")):
                filename, pathname, description = imp.find_module('blueprint', [base_dir])
                base = imp.load_module('blueprint', filename, pathname, description)
                self.blueprint_name = "_blueprint"
            else:
                puts("No _blueprint/blueprint.py file found")
        elif os.path.isdir(os.path.join(path, "_base")):
            puts("Using old '_base' convention")
            base_dir = os.path.join(path, "_base/")
            if os.path.exists(os.path.join(base_dir, "base.py")):
                filename, pathname, description = imp.find_module('base', [base_dir])
                base = imp.load_module('base', filename, pathname, description)
                self.blueprint_name = "_base"
            else:
                puts("No _base/base.py file found")

        if base:
            base.base_dir = base_dir

        if hasattr(base, 'blueprint') and isinstance(base.blueprint, Blueprint):
            self.app.register_blueprint(base.blueprint, site=self)

        return base

    def load_project(self, path):
        """
        Load a Tarbell project
        """
        base = self._get_base(path)

        filename, pathname, description = imp.find_module('tarbell_config', [path])
        project = imp.load_module('project', filename, pathname, description)

        try:
            self.key = project.SPREADSHEET_KEY
            self.client = get_drive_api()
        except AttributeError:
            self.key = None
            self.client = None

        try:
            project.CREATE_JSON
        except AttributeError:
            project.CREATE_JSON = False

        try:
            project.S3_BUCKETS
        except AttributeError:
            project.S3_BUCKETS = {}

        project.EXCLUDES = list(set(EXCLUDES + getattr(project, 'EXCLUDES', []) + getattr(base, 'EXCLUDES', [])))

        # merge project template types with defaults
        project.TEMPLATE_TYPES = set(getattr(project, 'TEMPLATE_TYPES', [])) | set(TEMPLATE_TYPES)

        try:
            project.DEFAULT_CONTEXT
        except AttributeError:
            project.DEFAULT_CONTEXT = {}

        project.DEFAULT_CONTEXT.update({
            "PROJECT_PATH": self.path,
            "ROOT_URL": "127.0.0.1:5000",
            "SPREADSHEET_KEY": self.key,
            "BUCKETS": project.S3_BUCKETS,
            "SITE": self,
        })

        # Set up template loaders
        template_dirs = [path]
        if base:
            template_dirs.append(base.base_dir)
        error_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'error_templates')
        template_dirs.append(error_path)

        self.app.jinja_loader = TarbellFileSystemLoader(template_dirs)

        # load the project blueprint, if it exists
        if hasattr(project, 'blueprint') and isinstance(project.blueprint, Blueprint):
            self.app.register_blueprint(project.blueprint, site=self)

        return project, base

    def _resolve_path(self, path):
        """
        Resolve static file paths
        """
        filepath = None
        mimetype = None

        for root, dirs, files in self.filter_files(self.path):
            # Does it exist in error path?
            error_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'error_templates', path)
            try:
                with open(error_path):
                    mimetype, encoding = mimetypes.guess_type(error_path)
                    filepath = error_path
            except IOError:
                pass

            # Does it exist in Tarbell blueprint?
            if self.base:
                basepath = os.path.join(root, self.blueprint_name, path)
                try:
                    with open(basepath):
                        mimetype, encoding = mimetypes.guess_type(basepath)
                        filepath = basepath
                except IOError:
                    pass

            # Does it exist under regular path?
            fullpath = os.path.join(root, path)
            try:
                with open(fullpath):
                    mimetype, encoding = mimetypes.guess_type(fullpath)
                    filepath = fullpath
            except IOError:
                pass

        return filepath, mimetype

    def data_json(self, extra_context=None, publish=False):
        """
        Serve site context as JSON. Useful for debugging.
        """
        if not self.project.CREATE_JSON:
            # nothing to see here, but the right mimetype
            return jsonify()

        if not self.data:
            # this sets site.data by spreadsheet or gdoc
            self.get_context(publish)

        return jsonify(self.data)

    def preview(self, path=None, extra_context=None, publish=False):
        """
        Serve up a project path
        """
        try:
            self.call_hook("preview", self)

            if path is None:
                path = 'index.html'

            # Detect files
            filepath, mimetype = self._resolve_path(path)

            # Serve dynamic
            if filepath and mimetype and mimetype in self.project.TEMPLATE_TYPES:
                context = self.get_context(publish)
                context.update({
                    "PATH": path,
                    "PREVIEW_SERVER": not publish,
                    "TIMESTAMP": int(time.time()),
                })
                if extra_context:
                    context.update(extra_context)

                rendered = render_template(path, **context)
                return Response(rendered, mimetype=mimetype)

            # Serve static
            if filepath:
                dir, filename = os.path.split(filepath)
                return send_from_directory(dir, filename)

        except Exception as e:
            ex_type, ex, tb = sys.exc_info()
            try:
                # Find template with name of error
                cls = e.__class__
                ex_type, ex, tb = sys.exc_info()

                context = self.project.DEFAULT_CONTEXT
                context.update({
                    'PATH': path,
                    'traceback': traceback.format_exception(ex_type, ex, tb),
                    'e': e,
                })
                if extra_context:
                    context.update(extra_context)

                try:
                    error_path = '_{0}.{1}.html'.format(cls.__module__, cls.__name__)
                    rendered = render_template(error_path, **context)
                except TemplateNotFound:
                    # Find template without underscore prefix, @TODO remove in v1.1
                    error_path = '{0}.{1}.html'.format(cls.__module__, cls.__name__)
                    rendered = render_template(error_path, **context)

                return Response(rendered, mimetype="text/html")
            except TemplateNotFound:
                # Otherwise raise old error
                reraise(ex_type, ex, tb)

        # Last ditch effort -- see if path has "index.html" underneath it
        if not path.endswith("index.html"):
            if not path.endswith("/"):
                path = "{0}/".format(path)
            path = "{0}{1}".format(path, "index.html")
            return self.preview(path)

        # It's a 404
        if path.endswith('/index.html'):
            path = path[:-11]
        rendered = render_template("404.html", PATH=path)
        return Response(rendered, status=404)

    def get_context(self, publish=False):
        """
        Use optional CONTEXT_SOURCE_FILE setting to determine data source.
        Return the parsed data.

        Can be an http|https url or local file. Supports csv and excel files.
        """
        context = self.project.DEFAULT_CONTEXT
        try:
            file = self.project.CONTEXT_SOURCE_FILE
            # CSV
            if re.search(r'(csv|CSV)$', file):
                context.update(self.get_context_from_csv())
            # Excel
            if re.search(r'(xlsx|XLSX|xls|XLS)$', file):
                context.update(self.get_context_from_xlsx())
        except AttributeError:
            context.update(self.get_context_from_gdoc())

        return context

    def get_context_from_xlsx(self):
        """
        Get context from an Excel file
        """
        if re.search('^(http|https)://', self.project.CONTEXT_SOURCE_FILE):
            resp = requests.get(self.project.CONTEXT_SOURCE_FILE)
            content = resp.content
        else:
            try:
                with open(self.project.CONTEXT_SOURCE_FILE) as xlsxfile:
                    content = xlsxfile.read()
            except IOError:
                filepath = "%s/%s" % (
                    os.path.abspath(self.path),
                    self.project.CONTEXT_SOURCE_FILE)
                with open(filepath) as xlsxfile:
                    content = xlsxfile.read()

        data = process_xlsx(content)
        if 'values' in data:
            data = copy_global_values(data)

        return data

    def get_context_from_csv(self):
        """
        Open CONTEXT_SOURCE_FILE, parse and return a context
        """
        if re.search('^(http|https)://', self.project.CONTEXT_SOURCE_FILE):
            data = requests.get(self.project.CONTEXT_SOURCE_FILE)
            reader = csv.reader(
                data.iter_lines(), delimiter=',', quotechar='"')
            ret = {rows[0]: rows[1] for rows in reader}
        else:
            try:
                with open(self.project.CONTEXT_SOURCE_FILE) as csvfile:
                    reader = csv.reader(csvfile, delimiter=',', quotechar='"')
                    ret = {rows[0]: rows[1] for rows in reader}
            except IOError:
                file = "%s/%s" % (
                    os.path.abspath(self.path),
                    self.project.CONTEXT_SOURCE_FILE)
                with open(file) as csvfile:
                    reader = csv.reader(csvfile, delimiter=',', quotechar='"')
                    ret = {rows[0]: rows[1] for rows in reader}
        ret.update({
            "CONTEXT_SOURCE_FILE": self.project.CONTEXT_SOURCE_FILE,
        })
        return ret

    def get_context_from_gdoc(self):
        """
        Wrap getting context from Google sheets in a simple caching mechanism.
        """
        try:
            start = int(time.time())
            if not self.data or start > self.expires:
                self.data = self._get_context_from_gdoc(self.project.SPREADSHEET_KEY)
                end = int(time.time())
                ttl = getattr(self.project, 'SPREADSHEET_CACHE_TTL',
                              SPREADSHEET_CACHE_TTL)
                self.expires = end + ttl
            return self.data
        except AttributeError:
            return {}

    def _get_context_from_gdoc(self, key):
        """
        Create a Jinja2 context from a Google spreadsheet.
        """
        try:
            content = self.export_xlsx(key)
            data = process_xlsx(content)
            if 'values' in data:
                data = copy_global_values(data)
            return data
        except BadStatusLine:
            # Stale connection, reset API and data
            puts("Connection reset, reloading drive API")
            self.client = get_drive_api()
            return self._get_context_from_gdoc(key)

    def export_xlsx(self, key):
        """
        Download xlsx version of spreadsheet.
        """
        spreadsheet_file = self.client.files().get(fileId=key).execute()
        links = spreadsheet_file.get('exportLinks')
        downloadurl = links.get('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
        resp, content = self.client._http.request(downloadurl)
        return content

    def generate_static_site(self, output_root=None, extra_context=None):
        """
        Bake out static site
        """
        self.app.config['BUILD_PATH'] = output_root

        # use this hook for registering URLs to freeze
        self.call_hook("generate", self, output_root, extra_context)

        if output_root is not None:
            # realpath or this gets generated relative to the tarbell package
            self.app.config['FREEZER_DESTINATION'] = os.path.realpath(output_root)

        self.freezer.freeze()

    def filter_files(self, path):
        """
        Exclude files based on blueprint and project configuration as well as hidden files.
        """
        excludes = r'|'.join([fnmatch.translate(x) for x in self.project.EXCLUDES]) or r'$.'
        for root, dirs, files in os.walk(path, topdown=True):
            dirs[:] = [d for d in dirs if not re.match(excludes, d)]
            dirs[:] = [os.path.join(root, d) for d in dirs]
            rel_path = os.path.relpath(root, path)

            paths = []
            for f in files:
                if rel_path == '.':
                    file_path = f
                else:
                    file_path = os.path.join(rel_path, f)
                if not re.match(excludes, file_path):
                    paths.append(f)

            files[:] = paths
            yield root, dirs, files

    def find_files(self):
        """
        Find all file paths for publishing, yield (urlname, kwargs)
        """
        # yield blueprint paths first
        if getattr(self, 'blueprint_name', None):
            for path in walk_directory(os.path.join(self.path, self.blueprint_name), ignore=self.project.EXCLUDES):
                yield 'preview', {'path': path}

        # then yield project paths
        for path in walk_directory(self.path, ignore=self.project.EXCLUDES):
            yield 'preview', {'path': path}
Example #34
0
File: bm.py Project: ianw3214/Oasis
def freeze(args):
    "https://pythonhosted.org/Frozen-Flask/"
    from flask_frozen import Freezer
    _setup_app(args)
    freezer = Freezer(app)
    freezer.freeze(debug=args.debug)
Example #35
0
from countpool import app
from flask_frozen import Freezer

static_app = Freezer(app)

if __name__ == "__main__":
    static_app.freeze()
Example #36
0
from flask_frozen import Freezer
from showcase import app

freezer = Freezer(app)

if __name__ == '__main__':
	freezer.freeze();

Example #37
0
@freezer.register_generator
def asset():
    for filename in walk_directory(path.join(root_folder, "assets")):
        yield {'path': filename}


@app.route('/<path:page_path>')
def get_index_page(page_path):
    """
    Handle requests which urls don't end with '.html' (for example, '/doc/')

    We don't need any generator here, because such urls are equivalent to the same urls
    with 'index.html' at the end.

    :param page_path: str
    :return: str
    """
    if not page_path.endswith('/'):
        page_path += '/'
    return process_page(page_path + 'index')


if __name__ == '__main__':
    if len(sys.argv) > 1 and sys.argv[1] == "build":
        urls = freezer.freeze()
        if not ignore_stdlib:
            generate_sitemap(urls)
    else:
        app.run(host="0.0.0.0", debug=True, threaded=True)
Example #38
0
from flask_frozen import Freezer
from app import app

app_freezer = Freezer(app)

if __name__ == '__main__':
    app_freezer.freeze()
Example #39
0
def freeze(config, urls_file=None):
    """Freeze the application by creating a static version of it."""
    if urls_file:
        app = make_app(config=config, URLS_FILE=urls_file)
    else:
        app = make_app(config=config)
    app.logger.info('freezing app to directory: %s' % app.config['FREEZER_DESTINATION'])
    freezer = Freezer(app)


    #@freezer.register_generator
    #def index_page():
    #    for (dirpath, dirnames, filenames) in os.walk(app.config['DATA_PATH'], topdown=True):
    #        start = len(os.path.commonprefix((app.config['DATA_PATH'], dirpath)))
    #        relative_path = dirpath[start+1:]
    #        for dirname in dirnames:
    #            yield ('page.index_page', {'uri': os.path.join(relative_path, dirname)})

    #@freezer.register_generator
    #def page_uri():
    #    # uri_index will be used so just avoid showing a warning
    #    return [
    #            ('public.page_uri', {'uri': ''}),
    #            ]
    @freezer.register_generator
    def uri_index():
        def cleanup_url(url):
            url = url.strip()
            if url.startswith('/'):
                if url.endswith('/index.html'):
                    return url
                elif url.endswith('/'):
                    url = url.strip('/')
                    if len(url) == 0:
                        return ('public.index', {})
                    return ('public.uri_index', {'uri': url})

        try:
            result = db.execute(text(fetch_query_string('select_paths_to_freeze.sql'))).fetchall()
        except (DatabaseError, StatementError) as err:
            app.logger.error("DatabaseError: %s", err)
            return []
        urls = filter(None, map(lambda x:cleanup_url(x[0]), result))

        urls_file = app.config.get('URLS_FILE', None)
        if urls_file:
            urls_file = urls_file if urls_file[0] == os.sep else os.path.join(os.getcwd(), urls_file)
            f = open(urls_file, 'r')
            urls.extend(filter(None, map(cleanup_url, f.readlines())))
            f.close()

        return urls


    @freezer.register_generator
    def send_root_file():
        root_folder = app.config.get('ROOT_FOLDER', None)
        if root_folder and os.path.isdir( root_folder ):
            for (dirpath, dirnames, filenames) in os.walk(root_folder, topdown=True):
                start = len(os.path.commonprefix((root_folder, dirpath)))
                relative_path = dirpath[start+1:]
                for filename in filenames:
                    yield ('send_root_file', {
                            'filename': os.path.join(relative_path, filename)
                            })

    @freezer.register_generator
    def send_media_file():
        media_folder = app.config.get('MEDIA_FOLDER', None)
        media_path = app.config.get('MEDIA_PATH', '/media/')
        freeze_all_files = app.config.get('MEDIA_FREEZE_ALL', False)
        if media_folder and freeze_all_files and os.path.isdir( media_folder ) and media_path[0] == '/':
            for (dirpath, dirnames, filenames) in os.walk(media_folder, topdown=True):
                start = len(os.path.commonprefix((media_folder, dirpath)))
                relative_path = dirpath[start+1:]
                for filename in filenames:
                    yield ('send_media_file', {
                            'filename': os.path.join(relative_path, filename)
                            })

    @freezer.register_generator
    def send_theme_file():
        theme_static_folder = app.config.get('THEME_STATIC_FOLDER', None)
        theme_static_path = app.config.get('THEME_STATIC_PATH', '/theme/')
        if theme_static_folder and os.path.isdir( theme_static_folder ) and theme_static_path[0] == '/':
            for (dirpath, dirnames, filenames) in os.walk(theme_static_folder, topdown=True):
                start = len(os.path.commonprefix((theme_static_folder, dirpath)))
                relative_path = dirpath[start+1:]
                for filename in filenames:
                    yield ('send_theme_file', {
                            'filename': os.path.join(relative_path, filename)
                            })


    freezer.freeze()
Example #40
0
File: script.py Project: pib/piblog
 def handle(self, app):
     freezer = Freezer(app)
     freezer.register_generator(generators.theme_static)
     freezer.freeze()
Example #41
0
File: fisle.py Project: fisle/fisle
            updated = page.meta['edited']
        except:
            updated = page.meta['date']
        updated = datetime.strptime(updated, '%Y-%m-%d %H:%M +0200')
        date = datetime.strptime(page.meta['date'], '%Y-%m-%d %H:%M +0200')
        feed.add(page.title, page.html,
                 content_type='html',
                 url=make_external(url_for('page', path=page.path)),
                 updated=updated,
                 published=date)
    return feed.get_response()


@app.route('/atom.latest')
def atom_latest():
    return get_feed('atom.latest', 5)


@app.route('/atom.all')
def atom_all():
    return get_feed('atom.all')


if __name__ == '__main__':
    """if run with build argument, generate the frozen pages
    else, run in debug mode"""
    if len(sys.argv) > 1 and sys.argv[1] == 'build':
        freezer.freeze()
    else:
        app.run(port=8080)
Example #42
0
#     # URLs as strings
#     yield '/publicar/'

# @freezer.register_generator
# def publicacion():
#     # URLs as strings
#     yield '/publicacion/'

# @freezer.register_generator
# def publicacion2():
#     # URLs as strings
#     yield '/publicacion2/'

# @freezer.register_generator
# def imagenes():
#     # URLs as strings
#     yield '/imagenes/'

# @freezer.register_generator
# def casiila():
#     # URLs as strings
#     yield '/', {'ID': '1'}

# @freezer.register_generator
# def salir():
#     # URLs as strings
#     yield '/salir/'

if __name__ == '__main__':
	freezer.freeze()