<s> '''<STR_LIT>''' <EOL> from collections import namedtuple <EOL> import io <EOL> import os <EOL> from markwiki import app <EOL> from markwiki . renderer import render_markdown <EOL> Page = namedtuple ( '<STR_LIT>' , [ '<STR_LIT:name>' , '<STR_LIT:path>' ] ) <EOL> Section = namedtuple ( '<STR_LIT>' , [ '<STR_LIT:name>' , '<STR_LIT:path>' ] ) <EOL> class WikiPage ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , page_path ) : <EOL> self . page_path = page_path <EOL> self . _wiki_path = None <EOL> self . _rel_path = None <EOL> @ property <EOL> def content ( self ) : <EOL> '''<STR_LIT>''' <EOL> with io . open ( self . wiki_path , '<STR_LIT:r>' , encoding = '<STR_LIT:utf-8>' ) as wiki : <EOL> return wiki . read ( ) <EOL> @ property <EOL> def exists ( self ) : <EOL> '''<STR_LIT>''' <EOL> return os . path . isfile ( self . wiki_path ) <EOL> @ property <EOL> def html ( self ) : <EOL> '''<STR_LIT>''' <EOL> return render_markdown ( self . wiki_path ) <EOL> @ property <EOL> def sections ( self ) : <EOL> '''<STR_LIT>''' <EOL> section_parts = self . page_path . split ( '<STR_LIT:/>' ) [ : - <NUM_LIT:1> ] <EOL> return _get_sections_from_parts ( section_parts ) <EOL> @ property <EOL> def title ( self ) : <EOL> return os . path . split ( self . page_path ) [ - <NUM_LIT:1> ] <EOL> @ property <EOL> def wiki_path ( self ) : <EOL> '''<STR_LIT>''' <EOL> if not self . _wiki_path : <EOL> self . _wiki_path = os . path . join ( app . config [ '<STR_LIT>' ] , <EOL> self . page_path + '<STR_LIT>' ) <EOL> return self . _wiki_path <EOL> @ property <EOL> def rel_path ( self ) : <EOL> '''<STR_LIT>''' <EOL> if not self . _rel_path : <EOL> self . _rel_path = self . page_path + '<STR_LIT>' <EOL> return self . _rel_path <EOL> def store ( self , content ) : <EOL> '''<STR_LIT>''' <EOL> directory = os . path . dirname ( self . wiki_path ) <EOL> if not os . path . exists ( directory ) : <EOL> try : <EOL> os . makedirs ( directory ) <EOL> except : <EOL> return False <EOL> try : <EOL> with open ( self . wiki_path , '<STR_LIT:wb>' ) as wiki : <EOL> wiki . write ( content . encode ( '<STR_LIT:utf-8>' ) . replace ( '<STR_LIT:\r\n>' , '<STR_LIT:\n>' ) ) <EOL> except IOError : <EOL> return False <EOL> if app . config [ '<STR_LIT>' ] : <EOL> app . gitint . update_file ( self . rel_path ) <EOL> return True <EOL> def delete ( self ) : <EOL> try : <EOL> os . remove ( self . wiki_path ) <EOL> except : <EOL> return False <EOL> return True <EOL> class WikiSection ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , section_path ) : <EOL> self . section_path = section_path <EOL> self . _pages = None <EOL> self . _subsections = None <EOL> @ property <EOL> def pages ( self ) : <EOL> if self . _pages is None : <EOL> self . _get_section_content ( ) <EOL> return self . _pages <EOL> @ property <EOL> def sections ( self ) : <EOL> '''<STR_LIT>''' <EOL> return _get_sections_from_parts ( self . section_path . split ( '<STR_LIT:/>' ) ) <EOL> @ property <EOL> def subsections ( self ) : <EOL> if self . _subsections is None : <EOL> self . _get_section_content ( ) <EOL> return self . _subsections <EOL> def _get_section_content ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . _pages = [ ] <EOL> self . _subsections = [ ] <EOL> directory = os . path . join ( app . config [ '<STR_LIT>' ] , self . section_path ) <EOL> contents = os . listdir ( directory ) <EOL> contents . sort ( ) <EOL> for content in contents : <EOL> if content . endswith ( '<STR_LIT>' ) : <EOL> page_name = content [ : - <NUM_LIT:3> ] <EOL> self . _pages . append ( Page ( page_name , <EOL> os . path . join ( self . section_path , page_name ) ) ) <EOL> elif not content . startswith ( '<STR_LIT:.>' ) : <EOL> self . _subsections . append ( <EOL> Section ( content , os . path . join ( self . section_path , content ) ) ) <EOL> def _get_sections_from_parts ( section_parts ) : <EOL> '''<STR_LIT>''' <EOL> sections = [ ] <EOL> section_path = [ ] <EOL> for part in section_parts : <EOL> if part is '<STR_LIT>' : <EOL> continue <EOL> section_path . append ( part ) <EOL> sections . append ( Section ( part , '<STR_LIT:/>' . join ( section_path ) ) ) <EOL> return sections </s>
