<s> import re <EOL> import os <EOL> import codecs <EOL> import textile <EOL> from operator import attrgetter <EOL> from xierpa3 . adapters . adapter import Adapter <EOL> from xierpa3 . toolbox . transformer import TX <EOL> from xierpa3 . toolbox . storage . article import Article <EOL> class TextileFileAdapter ( Adapter ) : <EOL> u"""<STR_LIT>""" <EOL> C = Adapter . C <EOL> DATACLASS = Article <EOL> CHAPTER_TAG = '<STR_LIT>' <EOL> FIELDVALUE = re . compile ( '<STR_LIT>' ) <EOL> COMMASPLIT = re . compile ( '<STR_LIT>' ) <EOL> u"""<STR_LIT>""" <EOL> def initialize ( self ) : <EOL> self . _cache = { } <EOL> self . ranked = [ ] <EOL> self . _readArticles ( ) <EOL> self . _cacheArticleFieldReferences ( ) <EOL> def _readArticles ( self ) : <EOL> u"""<STR_LIT>""" <EOL> for id , path in self . getIdPaths ( ) : <EOL> self . updateArticle ( id , self . root + path ) <EOL> def keys ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _cache . keys ( ) <EOL> def __getitem__ ( self , key ) : <EOL> """<STR_LIT>""" <EOL> return self . getArticle ( id = key ) <EOL> def updateArticle ( self , id , path ) : <EOL> u"""<STR_LIT>""" <EOL> article = None <EOL> wiki = self . readWikiFile ( path ) <EOL> if wiki is not None : <EOL> article = self . compileArticle ( wiki ) <EOL> article . id = id <EOL> article . source = wiki <EOL> article . ranking = TX . asInt ( article . ranking ) or <NUM_LIT:0> <EOL> article . blogresponse = TX . asBool ( article . blogresponse ) <EOL> article . path = path <EOL> article . modificationTime = os . path . getmtime ( path ) <EOL> self . _cacheArticle ( article ) <EOL> return article <EOL> @ classmethod <EOL> def readWikiFile ( cls , fsPath ) : <EOL> u"""<STR_LIT>""" <EOL> extension = '<STR_LIT:.>' + cls . C . EXTENSION_TXT <EOL> if not fsPath . endswith ( extension ) : <EOL> fsPath += extension <EOL> if os . path . exists ( fsPath ) : <EOL> f = codecs . open ( fsPath , encoding = '<STR_LIT:utf-8>' , mode = '<STR_LIT>' ) <EOL> wiki = f . read ( ) <EOL> f . close ( ) <EOL> else : <EOL> wiki = None <EOL> return wiki <EOL> def _splitFieldValue ( self , line ) : <EOL> u"""<STR_LIT>""" <EOL> found = self . FIELDVALUE . findall ( line ) <EOL> if found : <EOL> fieldName , value = found [ <NUM_LIT:0> ] <EOL> if fieldName in self . C . ADAPTER_COMMAFIELDS : <EOL> value = self . COMMASPLIT . findall ( value ) [ : - <NUM_LIT:1> ] <EOL> return fieldName , value <EOL> return None , None <EOL> def compileArticle ( self , wiki ) : <EOL> u"""<STR_LIT>""" <EOL> article = self . newArticle ( ) <EOL> text = [ ] <EOL> wiki = wiki . replace ( '<STR_LIT:\r>' , '<STR_LIT:\n>' ) <EOL> for line in wiki . split ( '<STR_LIT:\n>' ) : <EOL> if line . startswith ( '<STR_LIT:$>' ) : <EOL> fieldName , value = self . _splitFieldValue ( line ) <EOL> if fieldName is not None : <EOL> article [ fieldName ] = value <EOL> else : <EOL> text . append ( line ) <EOL> article . chapters = [ ] <EOL> for chapter in ( '<STR_LIT:\n>' . join ( text ) ) . split ( self . CHAPTER_TAG ) : <EOL> article . chapters . append ( textile . textile ( chapter ) ) <EOL> return article <EOL> def _cacheArticleFieldReferences ( self ) : <EOL> u"""<STR_LIT>""" <EOL> self . _urls = { } <EOL> self . _categories = { } <EOL> self . _levels = { } <EOL> for id , article in self . _cache . items ( ) : <EOL> for url in ( article . urls or [ ] ) : <EOL> if not self . _urls . has_key ( url ) : <EOL> self . _urls [ url ] = [ ] <EOL> self . _urls [ url ] . append ( article ) <EOL> for category in ( article . categories or [ ] ) : <EOL> if not self . _categories . has_key ( category ) : <EOL> self . _categories [ category ] = [ ] <EOL> self . _categories [ category ] . append ( article ) <EOL> for level in ( article . levels or [ ] ) : <EOL> if not self . _levels . has_key ( level ) : <EOL> self . _levels [ level ] = [ ] <EOL> self . _levels [ level ] . append ( article ) <EOL> def _cacheArticle ( self , article ) : <EOL> u"""<STR_LIT>""" <EOL> self . _cache [ article . id ] = article <EOL> self . _rankArticles ( ) <EOL> def _rankArticles ( self ) : <EOL> u"""<STR_LIT>""" <EOL> self . ranked = self . _cache . values ( ) <EOL> self . ranked . sort ( key = attrgetter ( '<STR_LIT>' ) , reverse = True ) <EOL> def getRankedArticles ( self ) : <EOL> u"""<STR_LIT>""" <EOL> return self . ranked <EOL> def getCachedArticle ( self , ** kwargs ) : <EOL> u"""<STR_LIT>""" <EOL> id = kwargs . get ( '<STR_LIT:id>' ) <EOL> article = self . _cache . get ( id ) <EOL> if article is not None and article . modificationTime != os . path . getmtime ( article . path ) : <EOL> article = self . updateArticle ( article . id , article . path ) <EOL> self . _cacheArticleFieldReferences ( ) <EOL> if article is None and kwargs . get ( '<STR_LIT:url>' ) is not None : <EOL> article = self . getArticleByUrl ( kwargs . get ( '<STR_LIT:url>' ) ) <EOL> if article is None and kwargs . get ( '<STR_LIT:path>' ) is not None : <EOL> article = self . getArticleByUrl ( kwargs . get ( '<STR_LIT:path>' ) ) <EOL> if article is None and kwargs . get ( '<STR_LIT:id>' ) is not None : <EOL> article = self . getArticleByIndex ( kwargs . get ( '<STR_LIT:id>' ) , kwargs . get ( '<STR_LIT>' ) ) <EOL> return article <EOL> def getArticleByIndex ( self , index , selector = None ) : <EOL> u"""<STR_LIT>""" <EOL> id = sorted ( self . _cache . keys ( ) ) [ <NUM_LIT:0> ] <EOL> return self . _cache [ id ] <EOL> def getArticleByUrl ( self , url ) : <EOL> u"""<STR_LIT>""" <EOL> article = None <EOL> fields = self . getFields ( ) <EOL> articles = fields . urls . get ( url ) <EOL> if articles : <EOL> article = articles [ <NUM_LIT:0> ] <EOL> return article <EOL> def getCachedArticles ( self ) : <EOL> return self . _cache <EOL> def getIdPaths ( self , path = None , idPaths = None ) : <EOL> if path is None : <EOL> path = '<STR_LIT>' <EOL> if idPaths is None : <EOL> idPaths = [ ] <EOL> extension = '<STR_LIT:.>' + self . C . EXTENSION_TXT <EOL> try : <EOL> for name in os . listdir ( self . root + path ) : <EOL> filePath = path + name <EOL> if name and name [ <NUM_LIT:0> ] in '<STR_LIT>' : <EOL> continue <EOL> if os . path . isdir ( self . root + filePath ) : <EOL> self . getIdPaths ( filePath + '<STR_LIT:/>' , idPaths ) <EOL> elif not name . endswith ( extension ) : <EOL> continue <EOL> else : <EOL> name = name . replace ( extension , '<STR_LIT>' ) <EOL> idPaths . append ( ( name , filePath ) ) <EOL> except Exception , e : <EOL> print e <EOL> return idPaths <EOL> def getChapters ( self , article ) : <EOL> if article . items is None : <EOL> return [ ] <EOL> return article . items <EOL> def getChapterByIndex ( self , index , article ) : <EOL> u"""<STR_LIT>""" <EOL> if <NUM_LIT:0> <= index < len ( article . chapters ) : <EOL> return article . chapters [ index ] <EOL> return None <EOL> def getChapterTitleByIndex ( self , index , article ) : <EOL> u"""<STR_LIT>""" <EOL> chapter = self . getChapterByIndex ( index , article ) <EOL> if chapter is not None : <EOL> return chapter . find ( '<STR_LIT>' ) <EOL> return None <EOL> def getPageTitle ( self , ** kwargs ) : <EOL> article = self . getArticle ( ** kwargs ) <EOL> if article is not None : <EOL> return self . newArticle ( text = article . name ) <EOL> return None <EOL> def getArticles ( self , start = <NUM_LIT:0> , count = <NUM_LIT:1> , omit = None , ** kwargs ) : <EOL> u"""<STR_LIT>""" <EOL> articles = [ ] <EOL> if omit is None : <EOL> chapters = self . ranked [ start : start + count ] <EOL> else : <EOL> if not isinstance ( omit , ( list , tuple ) ) : <EOL> omit = [ omit ] <EOL> for item in self . ranked [ start : ] : <EOL> if not item . id in omit : <EOL> articles . append ( item ) <EOL> if len ( articles ) >= count : <EOL> break <EOL> return articles <EOL> def getMobilePages ( self , count = None , ** kwargs ) : <EOL> return self . getPages ( count ) <EOL> def getArticle ( self , ** kwargs ) : <EOL> u"""<STR_LIT>""" <EOL> return self . getCachedArticle ( ** kwargs ) <EOL> def getFeaturedArticles ( self , id , start , count ) : <EOL> u"""<STR_LIT>""" <EOL> articles = [ ] <EOL> article = self . getArticle ( id ) <EOL> if article : <EOL> for index , featured in enumerate ( article . featured [ start : start + count ] ) : <EOL> if index == count : <EOL> break <EOL> featuredArticle = self . getArticle ( featured . attrib [ '<STR_LIT:id>' ] ) <EOL> if featuredArticle is not None : <EOL> articles . append ( featuredArticle ) <EOL> return articles <EOL> def getFields ( self ) : <EOL> u"""<STR_LIT>""" <EOL> article = self . newArticle ( ) <EOL> article . urls = self . _urls <EOL> article . categories = self . _categories <EOL> article . levels = self . _levels <EOL> return article <EOL> def getMenuArticles ( self , ** kwargs ) : <EOL> u"""<STR_LIT>""" <EOL> articles = [ ] <EOL> article = self . getArticle ( ** kwargs ) <EOL> if article . menu : <EOL> for menuId in article . menu : <EOL> menuArticle = self . getArticle ( id = menuId ) <EOL> if menuArticle is not None and menuArticle . tag : <EOL> articles . append ( menuArticle ) <EOL> return articles <EOL> def getLogo ( self , ** kwargs ) : <EOL> u"""<STR_LIT>""" <EOL> return self . newArticle ( url = '<STR_LIT>' , <EOL> src = '<STR_LIT>' ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> test = """<STR_LIT>""" <EOL> print textile . textile ( test ) </s>
