Module presalytics.story.server
Expand source code Browse git
import os
import logging
import time
import posixpath
import urllib.parse
import flask
import threading
import webbrowser
import requests
import presalytics.lib.util
import presalytics.lib.exceptions
logger = logging.getLogger(__name__)
app = flask.Flask(__name__)
@app.route('/story/<id>')
def story(id):
template_name = str(id) + '.html'
return flask.render_template(template_name)
@app.route('/img/<path:filename>/')
def static_img(filename=None):
directory = os.path.join(app.static_folder, "img")
return flask.send_from_directory(directory, filename)
@app.route('/js/<path:filename>/')
def static_js(filename=None):
directory = os.path.join(app.static_folder, "js")
return flask.send_from_directory(directory, filename)
@app.route('/css/<path:filename>/')
def static_css(filename=None):
directory = os.path.join(app.static_folder, "css")
return flask.send_from_directory(directory, filename)
@app.route('/favicon.ico')
def favicon():
return flask.send_from_directory(app.static_folder, 'favicon.ico')
@app.route('/shutdown', methods=['POST'])
def shutdown():
shutdown_func = flask.request.environ.get('werkzeug.server.shutdown')
shutdown_func()
return 'Server shutting down...'
class LocalServer(object):
def __init__(self,
host='127.0.0.1',
debug=True,
port=8082,
root_path=None,
use_reloader=False,
**kwargs):
self.host = host
self.debug = debug
self.port = port
self.root_path = self.make_local_folders(files_path=root_path)
self.static_dir = os.path.join(self.root_path, "static")
self.use_reloader = use_reloader
self.get_preloaders()
def run(self):
app.root_path = self.root_path
app.static_folder = self.static_dir
app.run(host=self.host,
debug=self.debug,
port=self.port,
use_reloader=self.use_reloader)
def make_local_folders(self, files_path=None):
if files_path is None:
files_path = os.getcwd()
if not os.path.exists(files_path):
os.mkdir(files_path)
root = os.path.join(files_path, "presalytics")
if not os.path.exists(root):
os.mkdir(root)
templates_path = os.path.join(root, "templates")
if not os.path.exists(templates_path):
os.mkdir(templates_path)
static_files_path = os.path.join(root, "static")
if not os.path.exists(static_files_path):
os.mkdir(static_files_path)
css_path = os.path.join(static_files_path, "css")
if not os.path.exists(css_path):
os.mkdir(css_path)
js_path = os.path.join(static_files_path, "js")
if not os.path.exists(js_path):
os.mkdir(js_path)
theme_path = os.path.join(css_path, "theme")
if not os.path.exists(theme_path):
os.mkdir(theme_path)
img_path = os.path.join(static_files_path, "img")
if not os.path.exists(img_path):
os.mkdir(img_path)
local_preloader_path = os.path.join(static_files_path, "preloaders")
if not os.path.exists(local_preloader_path):
os.mkdir(local_preloader_path)
return root
def get_preloaders(self):
preloaders = [
'bars.svg'
]
host = presalytics.lib.util.get_site_host()
local_preloader_path = os.path.join(self.static_dir, "preloaders")
for img in preloaders:
try:
preloader_url = urllib.parse.urljoin(host, "/static/preloaders/" + img)
response = requests.get(preloader_url)
if response.status_code == 200:
filename = os.path.join(local_preloader_path, img)
open(filename, 'wb').write(response.content)
else:
message = "Error downloading preloader " + img
raise presalytics.lib.exceptions.ApiError(message=message, status_code=response.status_code)
except Exception as ex:
message = "Could not download preloader {0} from {1}".format(img, host)
logger.error(message)
# logger.exception(ex) # non-critical error
def get_static_files_dict(self):
static_files_dict = {}
root = self.root_path
static = os.path.join(root, "static")
for item in os.listdir(static):
if os.path.isdir(item):
key = os.path.basename(item)
value = os.path.abspath(item)
static_files_dict[key] = value
static_files_dict['favicon.ico'] = os.path.join(static, "favicon.ico")
return static_files_dict
class Browser(threading.Thread):
def __init__(self, address, delay_time=2, *args, **kwargs):
super(Browser, self).__init__(*args, **kwargs)
self.address = address
self.delay_time = delay_time
def run(self):
time.sleep(self.delay_time)
webbrowser.open_new_tab(self.address)
if __name__ == '__main__':
app.run(debug=True)
Functions
def story(id)
-
Expand source code Browse git
@app.route('/story/<id>') def story(id): template_name = str(id) + '.html' return flask.render_template(template_name)
def static_img(filename=None)
-
Expand source code Browse git
@app.route('/img/<path:filename>/') def static_img(filename=None): directory = os.path.join(app.static_folder, "img") return flask.send_from_directory(directory, filename)
def static_js(filename=None)
-
Expand source code Browse git
@app.route('/js/<path:filename>/') def static_js(filename=None): directory = os.path.join(app.static_folder, "js") return flask.send_from_directory(directory, filename)
def static_css(filename=None)
-
Expand source code Browse git
@app.route('/css/<path:filename>/') def static_css(filename=None): directory = os.path.join(app.static_folder, "css") return flask.send_from_directory(directory, filename)
def favicon()
-
Expand source code Browse git
@app.route('/favicon.ico') def favicon(): return flask.send_from_directory(app.static_folder, 'favicon.ico')
def shutdown()
-
Expand source code Browse git
@app.route('/shutdown', methods=['POST']) def shutdown(): shutdown_func = flask.request.environ.get('werkzeug.server.shutdown') shutdown_func() return 'Server shutting down...'
Classes
class LocalServer (host='127.0.0.1', debug=True, port=8082, root_path=None, use_reloader=False, **kwargs)
-
Expand source code Browse git
class LocalServer(object): def __init__(self, host='127.0.0.1', debug=True, port=8082, root_path=None, use_reloader=False, **kwargs): self.host = host self.debug = debug self.port = port self.root_path = self.make_local_folders(files_path=root_path) self.static_dir = os.path.join(self.root_path, "static") self.use_reloader = use_reloader self.get_preloaders() def run(self): app.root_path = self.root_path app.static_folder = self.static_dir app.run(host=self.host, debug=self.debug, port=self.port, use_reloader=self.use_reloader) def make_local_folders(self, files_path=None): if files_path is None: files_path = os.getcwd() if not os.path.exists(files_path): os.mkdir(files_path) root = os.path.join(files_path, "presalytics") if not os.path.exists(root): os.mkdir(root) templates_path = os.path.join(root, "templates") if not os.path.exists(templates_path): os.mkdir(templates_path) static_files_path = os.path.join(root, "static") if not os.path.exists(static_files_path): os.mkdir(static_files_path) css_path = os.path.join(static_files_path, "css") if not os.path.exists(css_path): os.mkdir(css_path) js_path = os.path.join(static_files_path, "js") if not os.path.exists(js_path): os.mkdir(js_path) theme_path = os.path.join(css_path, "theme") if not os.path.exists(theme_path): os.mkdir(theme_path) img_path = os.path.join(static_files_path, "img") if not os.path.exists(img_path): os.mkdir(img_path) local_preloader_path = os.path.join(static_files_path, "preloaders") if not os.path.exists(local_preloader_path): os.mkdir(local_preloader_path) return root def get_preloaders(self): preloaders = [ 'bars.svg' ] host = presalytics.lib.util.get_site_host() local_preloader_path = os.path.join(self.static_dir, "preloaders") for img in preloaders: try: preloader_url = urllib.parse.urljoin(host, "/static/preloaders/" + img) response = requests.get(preloader_url) if response.status_code == 200: filename = os.path.join(local_preloader_path, img) open(filename, 'wb').write(response.content) else: message = "Error downloading preloader " + img raise presalytics.lib.exceptions.ApiError(message=message, status_code=response.status_code) except Exception as ex: message = "Could not download preloader {0} from {1}".format(img, host) logger.error(message) # logger.exception(ex) # non-critical error def get_static_files_dict(self): static_files_dict = {} root = self.root_path static = os.path.join(root, "static") for item in os.listdir(static): if os.path.isdir(item): key = os.path.basename(item) value = os.path.abspath(item) static_files_dict[key] = value static_files_dict['favicon.ico'] = os.path.join(static, "favicon.ico") return static_files_dict
Methods
def run(self)
-
Expand source code Browse git
def run(self): app.root_path = self.root_path app.static_folder = self.static_dir app.run(host=self.host, debug=self.debug, port=self.port, use_reloader=self.use_reloader)
def make_local_folders(self, files_path=None)
-
Expand source code Browse git
def make_local_folders(self, files_path=None): if files_path is None: files_path = os.getcwd() if not os.path.exists(files_path): os.mkdir(files_path) root = os.path.join(files_path, "presalytics") if not os.path.exists(root): os.mkdir(root) templates_path = os.path.join(root, "templates") if not os.path.exists(templates_path): os.mkdir(templates_path) static_files_path = os.path.join(root, "static") if not os.path.exists(static_files_path): os.mkdir(static_files_path) css_path = os.path.join(static_files_path, "css") if not os.path.exists(css_path): os.mkdir(css_path) js_path = os.path.join(static_files_path, "js") if not os.path.exists(js_path): os.mkdir(js_path) theme_path = os.path.join(css_path, "theme") if not os.path.exists(theme_path): os.mkdir(theme_path) img_path = os.path.join(static_files_path, "img") if not os.path.exists(img_path): os.mkdir(img_path) local_preloader_path = os.path.join(static_files_path, "preloaders") if not os.path.exists(local_preloader_path): os.mkdir(local_preloader_path) return root
def get_preloaders(self)
-
Expand source code Browse git
def get_preloaders(self): preloaders = [ 'bars.svg' ] host = presalytics.lib.util.get_site_host() local_preloader_path = os.path.join(self.static_dir, "preloaders") for img in preloaders: try: preloader_url = urllib.parse.urljoin(host, "/static/preloaders/" + img) response = requests.get(preloader_url) if response.status_code == 200: filename = os.path.join(local_preloader_path, img) open(filename, 'wb').write(response.content) else: message = "Error downloading preloader " + img raise presalytics.lib.exceptions.ApiError(message=message, status_code=response.status_code) except Exception as ex: message = "Could not download preloader {0} from {1}".format(img, host) logger.error(message)
def get_static_files_dict(self)
-
Expand source code Browse git
def get_static_files_dict(self): static_files_dict = {} root = self.root_path static = os.path.join(root, "static") for item in os.listdir(static): if os.path.isdir(item): key = os.path.basename(item) value = os.path.abspath(item) static_files_dict[key] = value static_files_dict['favicon.ico'] = os.path.join(static, "favicon.ico") return static_files_dict
class Browser (address, delay_time=2, *args, **kwargs)
-
A class that represents a thread of control.
This class can be safely subclassed in a limited fashion. There are two ways to specify the activity: by passing a callable object to the constructor, or by overriding the run() method in a subclass.
This constructor should always be called with keyword arguments. Arguments are:
group should be None; reserved for future extension when a ThreadGroup class is implemented.
target is the callable object to be invoked by the run() method. Defaults to None, meaning nothing is called.
name is the thread name. By default, a unique name is constructed of the form "Thread-N" where N is a small decimal number.
args is the argument tuple for the target invocation. Defaults to ().
kwargs is a dictionary of keyword arguments for the target invocation. Defaults to {}.
If a subclass overrides the constructor, it must make sure to invoke the base class constructor (Thread.init()) before doing anything else to the thread.
Expand source code Browse git
class Browser(threading.Thread): def __init__(self, address, delay_time=2, *args, **kwargs): super(Browser, self).__init__(*args, **kwargs) self.address = address self.delay_time = delay_time def run(self): time.sleep(self.delay_time) webbrowser.open_new_tab(self.address)
Ancestors
- threading.Thread
Methods
def run(self)
-
Method representing the thread's activity.
You may override this method in a subclass. The standard run() method invokes the callable object passed to the object's constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.
Expand source code Browse git
def run(self): time.sleep(self.delay_time) webbrowser.open_new_tab(self.address)