From fe29b6f05037015fd5f47c7ab302d5e958c29052 Mon Sep 17 00:00:00 2001 From: Thor77 Date: Tue, 7 Jun 2016 17:47:47 +0200 Subject: [PATCH] refactor template-loading * use ChoiceLoader([PackageLoader, FileSystemLoader]) instead of FileSystemLoader * add PackageLoader to allow template-access on egg-installations * use correct way to get filesystem-path to package (dirname(__file__)) * remove superflous template_name-arg from tsstats.template.render_template * bump version to 0.6.0 --- docs/source/conf.py | 4 ++-- setup.py | 2 +- tsstats/template.py | 14 ++++++++------ 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/docs/source/conf.py b/docs/source/conf.py index 0f57165..0af3083 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -54,9 +54,9 @@ author = 'Thor77' # built documents. # # The short X.Y version. -version = '0.5' +version = '0.6' # The full version, including alpha/beta/rc tags. -release = '0.5.9' +release = '0.6.0' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/setup.py b/setup.py index 6e55f5f..9ba0229 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import setup setup( name='tsstats', - version='0.5.9', + version='0.6.0', author='Thor77', author_email='thor77@thor77.org', description='A simple Teamspeak stats-generator', diff --git a/tsstats/template.py b/tsstats/template.py index 6df7217..34b0bdc 100644 --- a/tsstats/template.py +++ b/tsstats/template.py @@ -1,18 +1,17 @@ # -*- coding: utf-8 -*- import logging -from os.path import abspath +from os.path import dirname from time import localtime, strftime -from jinja2 import Environment, FileSystemLoader +from jinja2 import ChoiceLoader, Environment, FileSystemLoader, PackageLoader from tsstats.utils import seconds_to_text, sort_clients logger = logging.getLogger('tsstats') -def render_template(clients, output, template_name='tsstats/template.html', - title='TeamspeakStats'): +def render_template(clients, output, title='TeamspeakStats'): ''' render template with `clients` @@ -42,13 +41,16 @@ def render_template(clients, output, template_name='tsstats/template.html', ('Bans', clients_bans), ('passive Bans', clients_pbans)] # render - template_loader = FileSystemLoader(abspath('.')) + template_loader = ChoiceLoader([ + PackageLoader(__package__, ''), + FileSystemLoader(dirname(__file__)) + ]) template_env = Environment(loader=template_loader) def fmttime(timestamp): return strftime('%x %X', localtime(int(timestamp))) template_env.filters['frmttime'] = fmttime - template = template_env.get_template(template_name) + template = template_env.get_template('template.html') with open(output, 'w') as f: f.write(template.render(title=title, objs=objs, debug=logger.level <= logging.DEBUG))