<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> from collections import namedtuple <EOL> from itertools import chain <EOL> import re <EOL> from django . template import Library , Node , TemplateSyntaxError <EOL> from analytical . utils import ( is_internal_ip , disable_html , <EOL> get_required_setting , get_identity ) <EOL> DOMAINPATH_RE = re . compile ( r'<STR_LIT>' ) <EOL> SITEID_RE = re . compile ( r'<STR_LIT>' ) <EOL> TRACKING_CODE = """<STR_LIT>""" <EOL> VARIABLE_CODE = '<STR_LIT>' <EOL> IDENTITY_CODE = '<STR_LIT>' <EOL> DEFAULT_SCOPE = '<STR_LIT>' <EOL> PiwikVar = namedtuple ( '<STR_LIT>' , ( '<STR_LIT:index>' , '<STR_LIT:name>' , '<STR_LIT:value>' , '<STR_LIT>' ) ) <EOL> register = Library ( ) <EOL> @ register . tag <EOL> def piwik ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> bits = token . split_contents ( ) <EOL> if len ( bits ) > <NUM_LIT:1> : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" % bits [ <NUM_LIT:0> ] ) <EOL> return PiwikNode ( ) <EOL> class PiwikNode ( Node ) : <EOL> def __init__ ( self ) : <EOL> self . domain_path = get_required_setting ( '<STR_LIT>' , DOMAINPATH_RE , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . site_id = get_required_setting ( '<STR_LIT>' , SITEID_RE , <EOL> "<STR_LIT>" ) <EOL> def render ( self , context ) : <EOL> custom_variables = context . get ( '<STR_LIT>' , ( ) ) <EOL> complete_variables = ( var if len ( var ) >= <NUM_LIT:4> else var + ( DEFAULT_SCOPE , ) <EOL> for var in custom_variables ) <EOL> variables_code = ( VARIABLE_CODE % PiwikVar ( * var ) . _asdict ( ) <EOL> for var in complete_variables ) <EOL> userid = get_identity ( context , '<STR_LIT>' ) <EOL> if userid is not None : <EOL> variables_code = chain ( variables_code , ( <EOL> IDENTITY_CODE % { '<STR_LIT>' : userid } , <EOL> ) ) <EOL> html = TRACKING_CODE % { <EOL> '<STR_LIT:url>' : self . domain_path , <EOL> '<STR_LIT>' : self . site_id , <EOL> '<STR_LIT>' : '<STR_LIT>' . join ( variables_code ) <EOL> } <EOL> if is_internal_ip ( context , '<STR_LIT>' ) : <EOL> html = disable_html ( html , '<STR_LIT>' ) <EOL> return html <EOL> def contribute_to_analytical ( add_node ) : <EOL> PiwikNode ( ) <EOL> add_node ( '<STR_LIT>' , PiwikNode ) </s>
