1
Fork 0

simplify external settings

This commit is contained in:
Jake Howard 2017-01-13 21:41:40 +00:00
parent 51451e0638
commit 1375206853
10 changed files with 44 additions and 124 deletions

View file

@ -2,29 +2,12 @@ import yaml
import os.path
class DotDictionary(dict):
def __getattr__(self, attr):
value = self[attr]
if type(value) == dict:
value = DotDictionary(value)
return value
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__
settings_dir = os.path.dirname(__file__)
class WrappedSettings:
def __init__(self):
self.settings_dir = os.path.join(os.path.dirname(__file__), 'config.yml')
settings = open(self.settings_dir)
self.settings = yaml.safe_load(settings)
def get_config(filename):
with open(os.path.join(settings_dir, '{}.yml'.format(filename))) as f:
return yaml.safe_load(f)
def __getattr__(self, name):
value = self.settings[name]
if type(value) == dict:
value = DotDictionary(value)
return value
def __str__(self):
return str(self.settings)
settings = WrappedSettings()
social = get_config('social')

View file

@ -1,24 +1,4 @@
---
author: Jake Howard
site_name: TheOrangeOne
url: https://theorangeone.net
timezone: Europe/London
language: en
pelican_plugins:
- sitemap
- filetime_from_git
- pelican-jinja2content
- metatags
- autopages
- screenfetch
- post_build
- static_build
sitemap_format: xml
accounts:
github:
- GitHub
@ -105,7 +85,3 @@ footer_accounts:
- instagram
- youtube
- flickr
piwik:
url: piwik.theorangeone.net
site_id: 1

View file

@ -4,15 +4,13 @@ from git import Repo
import sys, os
sys.path.insert(0, os.path.realpath('./'))
from config import settings
# Global core settings
AUTHOR = settings.author
SITENAME = settings.site_name
SITEURL = settings.url
AUTHOR = "Jake Howard"
SITENAME = "TheOrangeOne"
SITEURL = "https://theorangeone.net"
PATH = 'content'
TIMEZONE = settings.timezone
DEFAULT_LANG = settings.language
TIMEZONE = "Europe/London"
DEFAULT_LANG = "en"
PAGE_PATHS = ["pages"]
THEME = "theme"
THEME_STATIC_DIR = "static"
@ -34,7 +32,10 @@ REPO = Repo(search_parent_directories=True)
BUILD_PRODUCTION = 'BUILD_PRODUCTION' in os.environ
from plugins import image_resizer
META_IMAGES = image_resizer.generate()
PIWIK = settings.piwik
PIWIK = {
'url': 'piwik.theorangeone.net',
'site_id': '1'
}
# Disable some pages
TAG_URL = False
@ -62,13 +63,22 @@ FEED_DOMAIN = SITEURL
# Setup plugins
PLUGIN_PATHS = ["plugins", "pelican_plugins"]
PLUGINS = settings.pelican_plugins
PLUGINS = [
'sitemap',
'filetime_from_git',
'pelican-jinja2content',
'metatags',
'autopages',
'screenfetch',
'post_build',
'static_build'
]
if BUILD_PRODUCTION:
PLUGINS.append("minify") # only minify on production build
SITEMAP = {
"format": settings.sitemap_format
"format": 'xml'
}
CATEGORY_PAGE_PATH = "theme/templates/categories"
MINIFY = {

View file

@ -1,6 +1,6 @@
from collections import namedtuple
from random import shuffle
from config import settings, DotDictionary
from config import social
ProjectLink = namedtuple("ProjectLink", ["name", "url", "image"])
@ -8,20 +8,20 @@ ProjectLink = namedtuple("ProjectLink", ["name", "url", "image"])
def accounts():
links = {}
for key, (site, user, url, icon) in settings.accounts.items():
links[key] = DotDictionary({
for key, (site, user, url, icon) in social['accounts'].items():
links[key] = {
'key': key,
'site': site,
'username': user,
'url': url.format(user),
'icon': icon
})
}
return links
def footer():
all_accounts = accounts()
return [all_accounts[account] for account in settings.footer_accounts]
return [all_accounts[account] for account in social['footer_accounts']]
def index_projects():

View file

@ -17,10 +17,10 @@ def html_to_raw(html):
def get_twiter_tags(instance):
return {
"twitter:card": "summary_large_image",
"twitter:site": instance.settings.get("ACCOUNTS")["twitter"].username,
"twitter:site": instance.settings.get("ACCOUNTS")["twitter"]['username'],
"twitter:title": instance.metadata.get("title", ""),
"twitter:description": html_to_raw(instance.metadata.get("summary", "")),
"twitter:creator": instance.settings.get("ACCOUNTS")["twitter"].username,
"twitter:creator": instance.settings.get("ACCOUNTS")["twitter"]['username'],
"twitter:image": instance.metadata.get("image", ""),
"twitter:image:alt": html_to_raw(instance.metadata.get("summary", "")),
"twitter:url": os.path.join(instance.settings.get("SITEURL", ""), instance.url)

View file

@ -1,5 +1,4 @@
import os
from pelican import signals
from pelican import contents

View file

@ -6,6 +6,7 @@ logger = logging.getLogger(__file__)
def static_build(*args, **kwargs):
return
if NODE_PRODUCTION:
logger.info('Building Production...')
UGLIFY_ARGS = ['--compress', '--screw-ie8', '--define', '--stats', '--keep-fnames']

View file

@ -18,7 +18,7 @@ flake8 scripts/ $FLAKE8_IGNORE
flake8 config/ $FLAKE8_IGNORE
flake8 tests/ $FLAKE8_IGNORE
yamllint config/config.yml
yamllint config/social.yml
mdspell --en-gb -ranx theme/templates/**/*.* theme/templates/*.*
mdspell --en-gb -ranx content/**/*.md content/*.md content/**/*.html content/*.html

View file

@ -1,6 +1,7 @@
from tests import TestCase
from config import settings, DotDictionary
from bs4 import BeautifulSoup
import pelicanconf as settings
from config import social as social_settings
from unittest import skipIf
from os import environ
@ -16,11 +17,11 @@ class CorePagesTestCase(TestCase):
def test_has_sitemap(self):
content = self.client.get('sitemap.xml')
self.assertIn(settings.url, content)
self.assertIn(settings.SITEURL, content)
def test_has_atom_feed(self):
content = self.client.get('feed.atom')
self.assertIn(settings.url, content)
self.assertIn(settings.SITEURL, content)
def test_has_404_page(self):
content = self.client.get('.404.html')
@ -49,7 +50,7 @@ class CorePagesTestCase(TestCase):
content = self.client.get('index.html')
footer = content.footer
for link in footer.find('p', class_="social").find_all('a'):
self.assertIn(link.attrs['alt'], settings.footer_accounts)
self.assertIn(link.attrs['alt'], social_settings['footer_accounts'])
self.assertIn("fa fa-", str(list(link.children)[0]))
@skipIf(not environ.get('BUILD_PRODUCTION', False), 'Not building production')
@ -59,60 +60,10 @@ class CorePagesTestCase(TestCase):
self.assertNotEqual(piwik_script_tag, None)
piwik_script = self.get_children(piwik_script_tag)
self.assertIn('piwik.js', piwik_script)
self.assertIn(str(settings.piwik.site_id), piwik_script)
self.assertIn(str(settings.PIWIK['site_id']), piwik_script)
piwik_img = content.find('noscript', id='piwik').find('img')
self.assertIn(settings.piwik.url, piwik_img.attrs['src'])
self.assertIn(str(settings.piwik.site_id), piwik_img.attrs['src'])
class DotDictionaryTestCase(TestCase):
def setUp(self):
self.test_dict = DotDictionary({
'foo': 'bar',
'bar': {
'foo': 'bar'
}
})
def test_returns_value(self):
self.assertEqual(self.test_dict.foo, 'bar')
def test_returns_self_on_dict(self):
self.assertEqual(self.test_dict.bar, {
'foo': 'bar'
})
self.assertIsInstance(self.test_dict.bar, DotDictionary)
def test_set(self):
self.test_dict.baz = 'foo'
self.assertEqual(self.test_dict, {
'foo': 'bar',
'baz': 'foo',
'bar': {
'foo': 'bar'
}
})
def test_delete(self):
del self.test_dict.bar
with self.assertRaises(KeyError):
print(self.test_dict.bar)
self.assertEqual(self.test_dict, {
'foo': 'bar'
})
class WrappedSettingTestCase(TestCase):
def test_has_data(self):
self.assertIsInstance(settings.settings, dict)
self.assertTrue(len(settings.settings))
def test_returns_values(self):
self.assertEqual(settings.language, 'en')
self.assertEqual(settings.timezone, 'Europe/London')
def test_returns_dict(self):
self.assertIsInstance(settings.accounts, DotDictionary)
self.assertIn(settings.PIWIK['url'], piwik_img.attrs['src'])
self.assertIn(str(settings.PIWIK['site_id']), piwik_img.attrs['src'])
class TestClientTestCase(TestCase):

View file

@ -1,5 +1,5 @@
from tests import TestCase
from config import settings
from config import social as social_settings
import os.path
@ -44,7 +44,7 @@ class AboutPageTestCase(TestCase):
self.assertEqual(len(tags), 1)
tag = tags[0]
self.assertEqual('medium', tag.attrs['data-theme'])
self.assertEqual(settings.accounts.github[1], tag.attrs['data-github'])
self.assertEqual(social_settings['accounts']['github'][1], tag.attrs['data-github'])
class Page404TestCase(TestCase):