<s> '''<STR_LIT>''' <EOL> import elementtree . ElementTree as ET <EOL> from django . template import loader <EOL> import datetime <EOL> import string <EOL> import os <EOL> from config import Config <EOL> class ETWrap : <EOL> '''<STR_LIT>''' <EOL> namespace = { } <EOL> def __init__ ( self , element , ns_dict = { } , ns_uri = "<STR_LIT>" ) : <EOL> '''<STR_LIT>''' <EOL> self . _element = element <EOL> self . _ns_dict = ns_dict if len ( ns_dict ) > <NUM_LIT:0> else ETWrap . namespace <EOL> self . _ns = ns_uri <EOL> def __getattr__ ( self , tag ) : <EOL> '''<STR_LIT>''' <EOL> if tag . startswith ( "<STR_LIT>" ) : <EOL> raise AttributeError ( tag ) <EOL> if tag == '<STR_LIT>' : <EOL> return self . _element . attrib <EOL> if tag == '<STR_LIT:text>' : <EOL> return self . _element . text <EOL> if tag == '<STR_LIT>' : <EOL> return self . _element . text <EOL> return self . _element . findtext ( self . _ns + tag ) <EOL> def ns ( self , prefix ) : <EOL> '''<STR_LIT>''' <EOL> for name , uri in self . _ns_dict . iteritems ( ) : <EOL> if name == prefix : <EOL> return ETWrap ( self . _element , self . _ns_dict , uri ) <EOL> return self <EOL> def unwrap ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . _element <EOL> def et_sub ( element , tag , text = None ) : <EOL> '''<STR_LIT>''' <EOL> sub = ET . SubElement ( element , tag ) <EOL> if text != None : <EOL> sub . text = text <EOL> return sub <EOL> def onlyAlphaNum ( dirty , repl = '<STR_LIT:U+0020>' , whitelist = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> keepers = '<STR_LIT>' % ( string . ascii_letters , string . digits , whitelist ) <EOL> replace_chars = string . maketrans ( keepers , '<STR_LIT:\x00>' * len ( keepers ) ) <EOL> if repl != '<STR_LIT>' : <EOL> replace_chars = string . maketrans ( replace_chars , repl * <NUM_LIT> ) <EOL> return dirty . encode ( '<STR_LIT:ascii>' , '<STR_LIT:ignore>' ) . translate ( replace_chars ) <EOL> else : <EOL> return dirty . encode ( '<STR_LIT:ascii>' , '<STR_LIT:ignore>' ) . translate ( None , replace_chars ) <EOL> def cleanCSV ( csv ) : <EOL> '''<STR_LIT>''' <EOL> parts = onlyAlphaNum ( csv , '<STR_LIT>' , '<STR_LIT>' ) . split ( '<STR_LIT:U+002C>' ) <EOL> parts = [ r . strip ( ) for r in parts if r != '<STR_LIT>' ] <EOL> return '<STR_LIT:U+002C>' . join ( set ( parts ) ) <EOL> def parse_and_get_ns ( file ) : <EOL> '''<STR_LIT>''' <EOL> events = "<STR_LIT:start>" , "<STR_LIT>" <EOL> root = None <EOL> ns = { } <EOL> for event , elem in ET . iterparse ( file , events ) : <EOL> if event == "<STR_LIT>" : <EOL> if elem [ <NUM_LIT:0> ] in ns and ns [ elem [ <NUM_LIT:0> ] ] != elem [ <NUM_LIT:1> ] : <EOL> raise KeyError ( "<STR_LIT>" ) <EOL> ns [ elem [ <NUM_LIT:0> ] ] = "<STR_LIT>" % elem [ <NUM_LIT:1> ] <EOL> elif event == "<STR_LIT:start>" : <EOL> if root is None : <EOL> root = elem <EOL> return ( ET . ElementTree ( root ) , ns ) <EOL> def getURL ( date , slug , type = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> if date == None or type != '<STR_LIT>' : <EOL> return '<STR_LIT>' % ( slug ) <EOL> return '<STR_LIT>' % ( date . year , date . month , date . day , slug ) <EOL> def getFilepath ( filename ) : <EOL> '''<STR_LIT>''' <EOL> module_dir = os . path . dirname ( __file__ ) <EOL> return os . path . join ( module_dir , filename ) <EOL> def getContentFilepath ( date , slug ) : <EOL> '''<STR_LIT>''' <EOL> filename = ( "<STR_LIT>" ) % ( date . strftime ( '<STR_LIT>' ) , slug ) <EOL> filename = os . path . join ( Config . POST_DIR , filename ) <EOL> return getFilepath ( filename ) <EOL> def checkBaseline ( ) : <EOL> '''<STR_LIT>''' <EOL> filepath = getFilepath ( Config . POST_DIR ) <EOL> if not os . path . exists ( filepath ) : <EOL> os . makedirs ( filepath , <NUM_LIT:0> <NUM_LIT> ) <EOL> filename = os . path . join ( Config . POST_DIR , Config . CATALOG_FILE ) <EOL> filepath = getFilepath ( filename ) <EOL> if not os . path . exists ( filepath ) : <EOL> root = ET . Element ( "<STR_LIT>" ) <EOL> root . set ( "<STR_LIT:version>" , "<STR_LIT>" ) <EOL> tree = ET . ElementTree ( root ) <EOL> tree . write ( filepath , "<STR_LIT>" ) <EOL> return filepath <EOL> def getTemplate ( filename , isSU = False ) : <EOL> '''<STR_LIT>''' <EOL> template_dir = Config . SU_TEMPLATES if isSU else Config . THEME_TEMPLATES <EOL> if template_dir : <EOL> filename = os . path . join ( template_dir , filename ) <EOL> return loader . get_template ( filename ) </s>
