<commit_before>from __future__ import with_statement
import os
from mako.template import Template
from mako.lookup import TemplateLookup
from markdown import markdown
from datetime import datetime
import util

class JEntry:
    def __init__(self, inpath):
        """Read the infile and populate the object"""

        # First patse the config and extract data from infile
        self.inpath = inpath
        self.config = util.parse_config('core.cfg')
        with open(inpath) as infh:
            raw_header, self.content = infh.read().split('\n---\n', 1)

        # Parse the header and populate the context with this
        # information
        self.context = self.__update_context(util.parse_header(raw_header))

        # Get a template ready to write
        tfile = util.view_mapper.get(self.context.get('view', 'default'))
        tlookup = TemplateLookup(directories = ['.'],
                                 output_encoding='utf-8',
                                 encoding_errors='replace')
        self.template = Template(filename = os.path.join('design', tfile),
                                 lookup = tlookup)

    def __render(self, template = None, context = None):
        """Renders a page given template and context"""
        
        if template is None:
            template = self.template
        if context is None:
            context = self.context
        extensions = ['codehilite', 'html_tidy']
        self.context['html_content'] = markdown(self.content, extensions)
        return template.render(**context)

    def __update_header(self, context = None):
        """Updates file header with given context. Function has
        side-effects and returns exit status"""
        
        if context is None:
            context = self.context
        with open(self.inpath, 'w') as infh:
            
            # Write context information in header_table
            for key in util.header_table:
                if context.get(key, None):
                    infh.write(key + ': '
                                        + context[key].__str__() + '\n')

            # Finish header. Write back original content
            infh.write('---\n' + self.content)
            return True
        return False

    def __update_context(self, context = None):
        """Post processing: Given certain values in context,
        calculates others"""
        
        if context is None:
            context = self.context
            
        # permalink might already be in header, in which case we don't
        # want to change it
        title = context.get('title', 'No Title')
        context['permalink'] = context.get('permalink',
                                           util.build_slug(title))
        context['draft'] = context.get('draft', None);
        
        # If pubdate is None, util.build_tiemstamp_h will return the
        # string "[Unpublished]"
        pubdate = context.get('pubdate', None)
        context['pubdate_h'] = util.build_timestamp_h(pubdate)
        return context

    def __write_out(self, outpath):
        """Render page and write it to a file"""

        with open(outpath, 'w') as outfh:
            outfh.write(self.__render())
        return True

    def publish(self, context = None):
        """Sets published and pubdate, updates header and context
        appropriately, and renders the page"""
        
        if context is None:
            context = self.context

        if not context['pubdate']:
            context['pubdate'] = datetime.now().strftime(util.time_isofmt)
        context = self.__update_context(context)
        self.__update_header(context)
        self.__write_out(util.build_path(self.config['outdir'],<commit_msg>Bugfix: Keyerror on missing pubdate<commit_after>from __future__ import with_statement
import os
from mako.template import Template
from mako.lookup import TemplateLookup
from markdown import markdown
from datetime import datetime
import util

class JEntry:
    def __init__(self, inpath):
        """Read the infile and populate the object"""

        # First patse the config and extract data from infile
        self.inpath = inpath
        self.config = util.parse_config('core.cfg')
        with open(inpath) as infh:
            raw_header, self.content = infh.read().split('\n---\n', 1)

        # Parse the header and populate the context with this
        # information
        self.context = self.__update_context(util.parse_header(raw_header))

        # Get a template ready to write
        tfile = util.view_mapper.get(self.context.get('view', 'default'))
        tlookup = TemplateLookup(directories = ['.'],
                                 output_encoding='utf-8',
                                 encoding_errors='replace')
        self.template = Template(filename = os.path.join('design', tfile),
                                 lookup = tlookup)

    def __render(self, template = None, context = None):
        """Renders a page given template and context"""
        
        if template is None:
            template = self.template
        if context is None:
            context = self.context
        extensions = ['codehilite', 'html_tidy']
        self.context['html_content'] = markdown(self.content, extensions)
        return template.render(**context)

    def __update_header(self, context = None):
        """Updates file header with given context. Function has
        side-effects and returns exit status"""
        
        if context is None:
            context = self.context
        with open(self.inpath, 'w') as infh:
            
            # Write context information in header_table
            for key in util.header_table:
                if context.get(key, None):
                    infh.write(key + ': '
                                        + context[key].__str__() + '\n')

            # Finish header. Write back original content
            infh.write('---\n' + self.content)
            return True
        return False

    def __update_context(self, context = None):
        """Post processing: Given certain values in context,
        calculates others"""
        
        if context is None:
            context = self.context
            
        # permalink might already be in header, in which case we don't
        # want to change it
        title = context.get('title', 'No Title')
        context['permalink'] = context.get('permalink',
                                           util.build_slug(title))
        context['draft'] = context.get('draft', None);
        
        # If pubdate is None, util.build_tiemstamp_h will return the
        # string "[Unpublished]"
        pubdate = context.get('pubdate', None)
        context['pubdate_h'] = util.build_timestamp_h(pubdate)
        return context

    def __write_out(self, outpath):
        """Render page and write it to a file"""

        with open(outpath, 'w') as outfh:
            outfh.write(self.__render())
        return True

    def publish(self, context = None):
        """Sets published and pubdate, updates header and context
        appropriately, and renders the page"""
        
        if context is None:
            context = self.context

        if not context.get('pubdate', None):
            context['pubdate'] = datetime.now().strftime(util.time_isofmt)
        context = self.__update_context(context)
        self.__update_header(context)
        self.__write_out(util.build_path(self.config['outdir'],
{'fraction_non_alphanumeric': 0.09572763684913217, 'fraction_numerical': 0.000534045393858478, 'mean_word_length': 1.9928086296444267, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6654498', 'n_tokens_mistral': 1941, 'n_tokens_neox': 1895, 'n_words': 580}