pelican-plugin-jsonresume/jsonresume_reader.py

69 lines
2.2 KiB
Python

"""
JSONResume Reader
==========
Version 0
"""
import os
import json
import locale
import logging
import subprocess
from datetime import date
from pelican import readers
from pelican import signals
class JSONResumeReader(readers.RstReader):
enabled = True
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def read(self, filename):
content, metadata = super().read(filename)
if not('jsonresume' in metadata and os.path.isfile(metadata['jsonresume'])):
raise Exception('please give a jsonresume metadata field for this type')
if not(f'jsonresume_{self._language_code}' in metadata and os.path.isfile(metadata[f'jsonresume_{self._language_code}'])):
raise Exception('please give a jsonresume_languagecode metadata field for this type')
metadata['resume'] = json.load(open(metadata['jsonresume']))
metadata['language'] = json.load(open(metadata[f'jsonresume_{self._language_code}']))
metadata['locale'] = self.settings.get('LOCALE', [self.settings.get('DEFAULT_LOCALE', None)])
countryname = lambda code: 'langue'
def setlocale(fct):
def wrapped(d):
set_locale = self.settings.get('LOCALE', [self.settings.get('DEFAULT_LOCALE', None)])
if set_locale is not None and len(set_locale) > 0:
locale.setlocale(locale.LC_TIME, set_locale[0])
return fct(d)
return wrapped
@setlocale
def format_date_short(d):
return date.fromisoformat(d).strftime('%B %Y').capitalize()
format_date_full = lambda d: date.fromisoformat(d).strftime('%d %B %Y').capitalize()
isinfinite = lambda d: date.fromisoformat(d).isoformat() == '2999-01-01'
metadata['utils'] = {
'get_country_name': countryname,
'format_date_short': format_date_short,
'format_date_full': format_date_full,
'isinfinite': isinfinite
}
return content, metadata
def add_reader(readers):
readers.reader_classes['jsonresume'] = JSONResumeReader
def register():
signals.readers_init.connect(add_reader)