# Path: pystil/utils.py
# def on(host, table=Visit.__table__):
#     """Generate a filter on a host """
#     if host == 'all':
#         return literal(True)
#     return table.c.host.like('%' + host)
# 
# def between(from_date, to_date, table=Visit.__table__):
#     """Generate a filter between 2 dates"""
#     return ((from_date <= table.c.date) &
#             (table.c.date < to_date + timedelta(1)))
# 
# def parse_referrer(referrer, with_query=False, host_only=False,
#                    second_pass=False):
#     """Return a pretty format for most search engines"""
#     if referrer:
#         up = urlparse(referrer)
#         netloc = up.netloc
#         if not netloc:
#             if with_query:
#                 return u"Local: %s" % up.path
#             if second_pass:
#                 return referrer
#             return "Local"
# 
#         query = up.query
#         parsed = parse_qs(query)
#         search = parsed.get('q', parsed.get('p', parsed.get('rdata', None)))
#         if search:
#             # TODO Yahoo variable encoding
#             if with_query:
#                 return u"Organic: %s %s" % (netloc, try_decode(search[0]))
#             return u"Organic: %s" % netloc
#         if host_only:
#             return netloc
#         return referrer
#     return "Direct"
# 
# Path: pystil/context.py
# class Hdr(RequestHandler):
#     @property
#     def db(self):
#         return self.application.db
# 
#     @property
#     def log(self):
#         return log
# 
#     def prepare(self):
#         if not self.get_secure_cookie('_pystil_site'):
#             self.log.info('Setting secure cookie')
#             self.set_secure_cookie('_pystil_site', 'local|' + str(uuid4()))
# 
#     def on_finish(self):
#         self.db.rollback()
# 
# class url(object):
#     def __init__(self, url):
#         self.url = url
# 
#     def __call__(self, cls):
#         pystil.add_handlers(
#             r'.*$',
#             (unnamed_url(self.url, cls, name=cls.__name__),)
#         )
#         return cls
# 
# Path: pystil/db.py
# def string(pkey=False):
# def integer(pkey=False):
# def decimal():
# def datetime():
# def date_column(pkey=False):
# class Visit(Base):
# class CriterionView(Base):
# 
# Path: pystil/i18n.py
# def labelize(string, lang):
#     """return the label for a criteria"""
#     if lang == 'fr':
#         return {
#             'new': 'Nouvelles visites',
#             'unique': 'Visites',
#             'all': 'Pages vues',
#             'spent_time': u'Temps passé sur de site',
#             'hour': 'Visites par heure'
#         }[string]
# 
#     return {
#         'new': 'New visits',
#         'unique': 'Visits',
#         'all': 'Page hits',
#         'spent_time': 'Time spent on site',
#         'hour': 'Visits per hour'
#     }[string]
# 
# def titlize(string, lang):
#     """return the label for a criteria"""
#     if lang == 'fr':
#         return {
#             'all': 'Statistiques par jour',
#             'asn': "Top fournisseur d'accès",
#             'country_code': "Carte du monde des visites",
#             'host': 'Top sites',
#             'page': 'Pages les plus vues',
#             'hash': 'Hashs les plus vus',
#             'referrer_domain': 'Top référeurs',
#             'hour': 'Visites par heure',
#             'subdomain': 'Top sous domaines',
#             'browser_name': 'Top navigateurs',
#             'browser_name_version': 'Top version de navigateur',
#             'size': "Top tailles d'écran",
#             'platform': 'Top plateforme',
#             'spent_time': 'Temps passé sur le site',
#             'country': 'Top pays',
#             'day': 'Top jours',
#             'ip': 'Top adresses IP',
#         }[string]
# 
#     return {
#         'all': 'Stats by day',
#         'asn': 'Top access networks',
#         'country_code': 'Visit worldmap',
#         'host': 'Top sites',
#         'page': 'Most viewed pages',
#         'hash': 'Most viewed hashes',
#         'referrer_domain': 'Best referrers',
#         'hour': 'Visits per hour',
#         'subdomain': 'Top subdomains',
#         'browser_name': 'Top browsers',
#         'browser_name_version': 'Top browser versions',
#         'size': 'Top screen sizes',
#         'platform': 'Top platforms',
#         'spent_time': 'Time spent on site',
#         'country': 'Top countries',
#         'day': 'Top days',
#         'ip': 'Top IP addresses'
#     }[string]


from datetime import date, timedelta
from pystil.utils import on, between, parse_referrer
from pystil.context import Hdr, url
from pystil.db import Visit, count, distinct
from pystil.i18n import labelize, titlize
from sqlalchemy import desc, func
from pygal.style import Style
from pygal.util import cut
import pygal
    def filter(self, query):
        return (query
                .filter(on(self.site, self.table))
                .filter(between(self.from_date, self.to_date, self.table))
                .filter(self.get_restrict()))

    def get_query(self):
        return (
            self.filter(self.db.query(
                self.criteria.label("key"), self.count_col.label("count")))
            .group_by(self.criteria))

    def render(self):
        self.chart = self.get_chart()
        self.populate()
        self.chart.title = titlize(self.criteria_name, self.lang)
        return self.chart.render()

    def render_load(self):
        self.chart = self.type(
            fill=True,
            human_readable=True,
            style=PystilStyle,
            width=1000,
            height=400,
            js=[
                self.host + '/static/js/svg.jquery.js',
                self.host + '/static/js/pygal-tooltips.js'
            ])
        self.chart.no_data_text = 'Loading'

# Path: pystil/utils.py
# def on(host, table=Visit.__table__):
#     """Generate a filter on a host """
#     if host == 'all':
#         return literal(True)
#     return table.c.host.like('%' + host)
# 
# def between(from_date, to_date, table=Visit.__table__):
#     """Generate a filter between 2 dates"""
#     return ((from_date <= table.c.date) &
#             (table.c.date < to_date + timedelta(1)))
# 
# def parse_referrer(referrer, with_query=False, host_only=False,
#                    second_pass=False):
#     """Return a pretty format for most search engines"""
#     if referrer:
#         up = urlparse(referrer)
#         netloc = up.netloc
#         if not netloc:
#             if with_query:
#                 return u"Local: %s" % up.path
#             if second_pass:
#                 return referrer
#             return "Local"
# 
#         query = up.query
#         parsed = parse_qs(query)
#         search = parsed.get('q', parsed.get('p', parsed.get('rdata', None)))
#         if search:
#             # TODO Yahoo variable encoding
#             if with_query:
#                 return u"Organic: %s %s" % (netloc, try_decode(search[0]))
#             return u"Organic: %s" % netloc
#         if host_only:
#             return netloc
#         return referrer
#     return "Direct"
# 
# Path: pystil/context.py
# class Hdr(RequestHandler):
#     @property
#     def db(self):
#         return self.application.db
# 
#     @property
#     def log(self):
#         return log
# 
#     def prepare(self):
#         if not self.get_secure_cookie('_pystil_site'):
#             self.log.info('Setting secure cookie')
#             self.set_secure_cookie('_pystil_site', 'local|' + str(uuid4()))
# 
#     def on_finish(self):
#         self.db.rollback()
# 
# class url(object):
#     def __init__(self, url):
#         self.url = url
# 
#     def __call__(self, cls):
#         pystil.add_handlers(
#             r'.*$',
#             (unnamed_url(self.url, cls, name=cls.__name__),)
#         )
#         return cls
# 
# Path: pystil/db.py
# def string(pkey=False):
# def integer(pkey=False):
# def decimal():
# def datetime():
# def date_column(pkey=False):
# class Visit(Base):
# class CriterionView(Base):
# 
# Path: pystil/i18n.py
# def labelize(string, lang):
#     """return the label for a criteria"""
#     if lang == 'fr':
#         return {
#             'new': 'Nouvelles visites',
#             'unique': 'Visites',
#             'all': 'Pages vues',
#             'spent_time': u'Temps passé sur de site',
#             'hour': 'Visites par heure'
#         }[string]
# 
#     return {
#         'new': 'New visits',
#         'unique': 'Visits',
#         'all': 'Page hits',
#         'spent_time': 'Time spent on site',
#         'hour': 'Visits per hour'
#     }[string]
# 
# def titlize(string, lang):
#     """return the label for a criteria"""
#     if lang == 'fr':
#         return {
#             'all': 'Statistiques par jour',
#             'asn': "Top fournisseur d'accès",
#             'country_code': "Carte du monde des visites",
#             'host': 'Top sites',
#             'page': 'Pages les plus vues',
#             'hash': 'Hashs les plus vus',
#             'referrer_domain': 'Top référeurs',
#             'hour': 'Visites par heure',
#             'subdomain': 'Top sous domaines',
#             'browser_name': 'Top navigateurs',
#             'browser_name_version': 'Top version de navigateur',
#             'size': "Top tailles d'écran",
#             'platform': 'Top plateforme',
#             'spent_time': 'Temps passé sur le site',
#             'country': 'Top pays',
#             'day': 'Top jours',
#             'ip': 'Top adresses IP',
#         }[string]
# 
#     return {
#         'all': 'Stats by day',
#         'asn': 'Top access networks',
#         'country_code': 'Visit worldmap',
#         'host': 'Top sites',
#         'page': 'Most viewed pages',
#         'hash': 'Most viewed hashes',
#         'referrer_domain': 'Best referrers',
#         'hour': 'Visits per hour',
#         'subdomain': 'Top subdomains',
#         'browser_name': 'Top browsers',
#         'browser_name_version': 'Top browser versions',
#         'size': 'Top screen sizes',
#         'platform': 'Top platforms',
#         'spent_time': 'Time spent on site',
#         'country': 'Top countries',
#         'day': 'Top days',
#         'ip': 'Top IP addresses'
#     }[string]


# Path: pystil/charts.py
from datetime import date, timedelta
from pystil.utils import on, between, parse_referrer
from pystil.context import Hdr, url
from pystil.db import Visit, count, distinct
from pystil.i18n import labelize, titlize
from sqlalchemy import desc, func
from pygal.style import Style
from pygal.util import cut
import pygal

    def filter(self, query):
        return (query
                .filter(on(self.site, self.table))
                .filter(between(self.from_date, self.to_date, self.table))
                .filter(self.get_restrict()))

    def get_query(self):
        return (
            self.filter(self.db.query(
                self.criteria.label("key"), self.count_col.label("count")))
            .group_by(self.criteria))

    def render(self):
        self.chart = self.get_chart()
        self.populate()
        self.chart.title = titlize(self.criteria_name, self.lang)
        return self.chart.render()

    def render_load(self):
        self.chart = self.type(
            fill=True,
            human_readable=True,
            style=PystilStyle,
            width=1000,
            height=400,
            js=[
                self.host + '/static/js/svg.jquery.js',
                self.host + '/static/js/pygal-tooltips.js'
            ])
        self.chart.no_data_text = 'Loading'

        self.chart.title = titlize(self.criteria_name, self.lang)