import yaml import os import glob import re class Index( object ): def __init__( self, path ): self.path = path self.fields = {} self.doc = "" def path_short( self ): # XXX needs tidy return "content/pages/index.md" def open( self ): self.read() def write( self ): outdata = ( '''\ --- %(yaml)s --- %(md)s ''' % { 'yaml': yaml.dump( self.fields, default_flow_style = False ), 'md': self.doc, } ) outfile = open( self.path, 'w', encoding = "utf-8" ) outfile.write( outdata ) return outdata def read( self ): indata = open( self.path, 'r', encoding = "utf-8" ).read() ( pre, yamldata, docdata ) = re.split( "^---$", indata, flags = re.MULTILINE ) self.fields = yaml.load( yamldata ) self.doc = docdata.strip() def _toggle( self, fieldname ): if self.fields.get( fieldname ): del self.fields[ fieldname ] else: self.fields[ fieldname ] = True return " ".join( [ x for x in [ 'incident', 'degraded', 'maintenance', 'notice', 'ok' ] if self.fields.get( x ) ] ) def toggle_incident( self ): return self._toggle( 'incident' ) def toggle_degraded( self ): return self._toggle( 'degraded' ) def toggle_maintenance( self ): return self._toggle( 'maintenance' ) def toggle_notice( self ): return self._toggle( 'notice' ) def toggle_ok( self ): return self._toggle( 'ok' ) def set_flags( self, flags ): for fieldname in [ 'incident', 'degraded', 'maintenance', 'notice', 'ok' ]: self.fields[ fieldname ] = fieldname in flags class Event( Index ): def __init__( self, path, glob ): super().__init__( path ) self.glob = glob def path_short( self ): # XXX needs tidy return "content/event/" + os.path.basename( self.path ) def open( self ): if os.path.exists( self.path ): self.read() return False for path in glob.glob( self.glob ): if os.path.exists( path ): self.path = path self.read() return False return True def set_title( self, title ): self.fields[ '$title@' ] = title def set_slug( self, slug ): self.fields[ '$slug' ] = slug def set_published( self, published ): if not self.fields.get( '$date' ): self.fields[ '$date' ] = {} if not self.fields[ '$date' ].get( 'published' ): self.fields[ '$date' ][ 'published' ] = published return published def set_started( self, started ): if not self.fields.get( '$date' ): self.fields[ '$date' ] = {} self.fields[ '$date' ][ 'started' ] = started return started def set_expected( self, expected ): if not self.fields.get( '$date' ): self.fields[ '$date' ] = {} self.fields[ '$date' ][ 'expected' ] = expected return expected def set_finished( self, finished ): if not self.fields.get( '$date' ): self.fields[ '$date' ] = {} self.fields[ '$date' ][ 'finished' ] = finished return finished def set_resolved( self, resolved ): if not self.fields.get( '$date' ): self.fields[ '$date' ] = {} self.fields[ '$date' ][ 'resolved' ] = resolved return resolved def add_timeline( self, time, line ): if not self.fields.get( 'timeline' ): self.fields[ 'timeline' ] = [] self.fields[ 'timeline' ].append( { 'time': time, 'line': line } )