You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.1 KiB
Python

import yaml
import os
import glob
import re
class Event( object ):
def __init__( self, path, glob ):
self.path = path
self.glob = glob
self.fields = {}
self.doc = ""
def path_short( self ):
# XXX needs tidy
return "content/event/" + os.path.basename( self.path )
def open( self ):
for path in glob.glob( self.glob ):
if os.path.exists( path ):
self.path = path
self.read()
return False
return True
def write( self ):
outfile = open( self.path, 'w', encoding = "utf-8" )
outfile.write( ( '''\
---
%(yaml)s
---
%(md)s
''' % { 'yaml': yaml.dump( self.fields, default_flow_style = False ),
'md': self.doc,
} ) )
def read( self ):
indata = open( self.path, 'r', encoding = "utf-8" ).read()
try:
( pre, yamldata, docdata ) = re.split( "^---$", indata, flags = re.MULTILINE )
except ValueError:
return
self.yaml = yaml.load( yamldata )
self.doc = docdata.strip()