"""
Assorted tools for importing related data and interfacing
with phantomjs.
"""
import importlib
import os
from pydoc import locate
import subprocess
from django.conf import settings
file_dir = os.path.dirname(__file__)
[docs]def render_html(url):
"""
Process a given url through phantomjs and return a string of the page
content.
"""
phantom_script_path = os.path.join(file_dir, 'phantomjs', 'render_html.js')
proc = subprocess.Popen(["phantomjs", phantom_script_path, url],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return proc.communicate()
def get_backend(name):
"""
Allows for importing classes, which importlib does not.
"""
return locate(name)
# May be time to depricate this function, since we're relying on locate
def get_app(name):
"""
Attempts to fetch the seites module by app name.
"""
app = importlib.import_module(name + ".seites")
assert getattr(app, 'list_pages', None) is not None, \
"{} seites module is not configured properly.".format(name)
return app
def get_apps():
"""
Generate a list of all Django apps and attempt to fetch their
seites module.
"""
for app in settings.INSTALLED_APPS:
try:
yield get_app(app)
except (ImportError, AssertionError):
pass