Improved Item class: now thanks to AttrDict all frontmatter fields can be read as attributes
This commit is contained in:
parent
81e7dd88ff
commit
1b7a3f6308
|
@ -0,0 +1,8 @@
|
|||
class AttrDict(dict):
|
||||
"""This dict-based class allows accessing keys like attributes
|
||||
https://stackoverflow.com/questions/4984647/accessing-dict-keys-like-an-attribute
|
||||
"""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.__dict__ = self
|
50
_src/item.py
50
_src/item.py
|
@ -1,13 +1,13 @@
|
|||
import os
|
||||
import frontmatter # from package python-frontmatter
|
||||
import datetime
|
||||
from files import read_this # from itself
|
||||
from config import config
|
||||
from attrdict import AttrDict # from itself
|
||||
|
||||
class Item:
|
||||
class Item(AttrDict):
|
||||
"""This class represents single content file
|
||||
It extracts some pre-defined frontmatter fields using python-frontmatter module"""
|
||||
|
||||
|
||||
title = None
|
||||
tags = []
|
||||
description = None
|
||||
|
@ -17,41 +17,31 @@ class Item:
|
|||
author = None
|
||||
|
||||
def __init__(self, path):
|
||||
# initialize parent
|
||||
super().__init__()
|
||||
|
||||
self.path = path
|
||||
|
||||
frontmatter_data, content = frontmatter.parse(read_this('content/'+path))
|
||||
self.content = content
|
||||
self.frontmatter_data = frontmatter_data
|
||||
|
||||
# fill undefined fields with default values from config
|
||||
for field in config['defaults']:
|
||||
if field not in frontmatter_data:
|
||||
frontmatter_data[field] = config['defaults'][field]
|
||||
|
||||
if 'created' in frontmatter_data:
|
||||
# frontmatter produces date objects if created field is in format yyyy-mm-dd
|
||||
# if it contains time as in ISO 8601, we want to convert it to datetime
|
||||
# https://en.wikipedia.org/wiki/ISO_8601
|
||||
if type(frontmatter_data['created']) == str:
|
||||
frontmatter_data['created'] = datetime.datetime.fromisoformat(
|
||||
frontmatter_data['created'])
|
||||
self.date = str(frontmatter_data['created'].date())
|
||||
self.time = str(frontmatter_data['created'].time())
|
||||
for field in frontmatter_data:
|
||||
if field == 'created':
|
||||
# frontmatter produces date objects if created field is in format yyyy-mm-dd
|
||||
# if it contains time as in ISO 8601, we want to convert it to datetime
|
||||
# https://en.wikipedia.org/wiki/ISO_8601
|
||||
if type(frontmatter_data['created']) == str:
|
||||
frontmatter_data['created'] = datetime.datetime.fromisoformat(
|
||||
frontmatter_data['created'])
|
||||
self.date = str(frontmatter_data['created'].date())
|
||||
self.time = str(frontmatter_data['created'].time())
|
||||
else:
|
||||
self.date = str(frontmatter_data['created'])
|
||||
else:
|
||||
self.date = str(frontmatter_data['created'])
|
||||
|
||||
self.frontmatter_data = frontmatter_data
|
||||
|
||||
if 'title' in frontmatter_data:
|
||||
self.title = frontmatter_data['title']
|
||||
|
||||
if 'tags' in frontmatter_data:
|
||||
self.tags = frontmatter_data['tags']
|
||||
|
||||
if 'description' in frontmatter_data:
|
||||
self.description = frontmatter_data['description']
|
||||
|
||||
if 'source' in frontmatter_data:
|
||||
self.source = frontmatter_data['source']
|
||||
|
||||
if 'author' in frontmatter_data:
|
||||
self.author = frontmatter_data['author']
|
||||
self[field] = frontmatter_data[field]
|
||||
|
|
Reference in New Issue