Exemplo n.º 1
0
 def test_safe_mode(self):
     test_path = os.path.dirname(os.path.realpath(__file__))
     project = StatikProject(os.path.join(test_path, 'data-simple'), safe_mode=True)
     with self.assertRaises(SafetyViolationError):
         project.generate(
             in_memory=True
         )
Exemplo n.º 2
0
def watch(project_path,
          output_path,
          host='0.0.0.0',
          port=8000,
          min_reload_time=2.0,
          open_browser=True,
          safe_mode=False):
    """Watches the given project path for filesystem changes, and automatically rebuilds the project when
    changes are detected. Also serves an HTTP server on the given host/port.

    Args:
        project_path: The path to the Statik project to be watched.
        output_path: The path into which to write the output files.
        host: The host IP/hostname to which to bind when serving output files.
        port: The port to which to bind when serving output files.
        min_reload_time: The minimum time (in seconds) between reloads when files change.
        open_browser: Whether or not to automatically open the web browser at the served URL.
        safe_mode: Whether or not to run Statik in safe mode.
    """
    project = StatikProject(project_path, safe_mode=safe_mode)
    project.generate(output_path=output_path, in_memory=False)

    watch_folders = [
        StatikProject.MODELS_DIR, StatikProject.DATA_DIR,
        StatikProject.VIEWS_DIR, StatikProject.TEMPLATES_DIR,
        project.config.assets_src_path
    ]

    # let the template tags folder be optional
    template_tags_folder = os.path.join(project.path,
                                        StatikProject.TEMPLATETAGS_DIR)
    if os.path.exists(template_tags_folder) and os.path.isdir(
            template_tags_folder):
        watch_folders.append(StatikProject.TEMPLATETAGS_DIR)

    # if theming is enabled, watch the specific theme's folder for changes
    if project.config.theme is not None:
        watch_folders.append(
            os.path.join(StatikProject.THEMES_DIR, project.config.theme))

    watch_folders = [
        f if os.path.isabs(f) else os.path.join(project.path, f)
        for f in watch_folders
    ]
    for folder in watch_folders:
        if not os.path.exists(folder) or not os.path.isdir(folder):
            logger.error("Cannot find required project folder: %s" % folder)
            return

    httpwatcher.watch(
        output_path,
        watch_paths=watch_folders,
        on_reload=lambda: safe_wrap_project_generate(project, output_path),
        host=host,
        port=port,
        server_base_path=project.config.base_path,
        watcher_interval=min_reload_time,
        recursive=True,
        open_browser=open_browser)
Exemplo n.º 3
0
def watch(project_path, output_path, host='0.0.0.0', port=8000, min_reload_time=2.0,
          open_browser=True, safe_mode=False, error_context=None):
    """Watches the given project path for filesystem changes, and automatically rebuilds the project when
    changes are detected. Also serves an HTTP server on the given host/port.

    Args:
        project_path: The path to the Statik project to be watched.
        output_path: The path into which to write the output files.
        host: The host IP/hostname to which to bind when serving output files.
        port: The port to which to bind when serving output files.
        min_reload_time: The minimum time (in seconds) between reloads when files change.
        open_browser: Whether or not to automatically open the web browser at the served URL.
        safe_mode: Whether or not to run Statik in safe mode.
        error_context: An optional StatikErrorContext instance for detailed error reporting.
    """
    error_context = error_context or StatikErrorContext()
    project = StatikProject(project_path, safe_mode=safe_mode, error_context=error_context)
    project.generate(output_path=output_path, in_memory=False)

    watch_folders = [
        StatikProject.MODELS_DIR,
        StatikProject.DATA_DIR,
        StatikProject.VIEWS_DIR,
        StatikProject.TEMPLATES_DIR,
        project.config.assets_src_path
    ]

    # let the template tags folder be optional
    template_tags_folder = os.path.join(project.path, StatikProject.TEMPLATETAGS_DIR)
    if os.path.exists(template_tags_folder) and os.path.isdir(template_tags_folder):
        watch_folders.append(StatikProject.TEMPLATETAGS_DIR)

    # if theming is enabled, watch the specific theme's folder for changes
    if project.config.theme is not None:
        watch_folders.append(os.path.join(StatikProject.THEMES_DIR, project.config.theme))

    watch_folders = [f if os.path.isabs(f) else os.path.join(project.path, f) for f in watch_folders]
    for folder in watch_folders:
        if not os.path.exists(folder) or not os.path.isdir(folder):
            raise MissingProjectFolderError(folder)

    httpwatcher.watch(
        output_path,
        watch_paths=watch_folders,
        on_reload=lambda: safe_wrap_project_generate(
            project,
            output_path
        ),
        host=host,
        port=port,
        server_base_path=project.config.base_path,
        watcher_interval=min_reload_time,
        recursive=True,
        open_browser=open_browser
    )
Exemplo n.º 4
0
 def test_safe_mode(self):
     test_path = os.path.dirname(os.path.realpath(__file__))
     project = StatikProject(os.path.join(test_path, 'data-simple'), safe_mode=True)
     caught = False
     try:
         project.generate(
             in_memory=True
         )
     except MissingTemplateError:
         caught = True
     except SafetyViolationError:
         caught = True
     self.assertTrue(caught, "Safe mode generation must raise an appropriate exception when attempting to "
                             "generate content for an unsafe project.")
Exemplo n.º 5
0
 def test_safe_mode(self):
     test_path = os.path.dirname(os.path.realpath(__file__))
     project = StatikProject(os.path.join(test_path, 'data-simple'),
                             safe_mode=True)
     caught = False
     try:
         project.generate(in_memory=True)
     except MissingTemplateError:
         caught = True
     except SafetyViolationError:
         caught = True
     self.assertTrue(
         caught,
         "Safe mode generation must raise an appropriate exception when attempting to "
         "generate content for an unsafe project.")
Exemplo n.º 6
0
def generate(input_path, output_path=None, in_memory=False):
    """Executes the Statik site generator using the given parameters.
    """
    project = StatikProject(input_path)
    return project.generate(output_path=output_path, in_memory=in_memory)
Exemplo n.º 7
0
def generate(input_path, output_path=None, in_memory=False, safe_mode=False):
    """Executes the Statik site generator using the given parameters.
    """
    project = StatikProject(input_path, safe_mode=safe_mode)
    return project.generate(output_path=output_path, in_memory=in_memory)
Exemplo n.º 8
0
 def test_safe_mode(self):
     test_path = os.path.dirname(os.path.realpath(__file__))
     project = StatikProject(os.path.join(test_path, 'data-simple'),
                             safe_mode=True)
     with self.assertRaises(SafetyViolationError):
         project.generate(in_memory=True)