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

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: accounts:
github: github:
- GitHub - GitHub
@ -105,7 +85,3 @@ footer_accounts:
- instagram - instagram
- youtube - youtube
- flickr - flickr
piwik:
url: piwik.theorangeone.net
site_id: 1

View file

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

View file

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

View file

@ -17,10 +17,10 @@ def html_to_raw(html):
def get_twiter_tags(instance): def get_twiter_tags(instance):
return { return {
"twitter:card": "summary_large_image", "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:title": instance.metadata.get("title", ""),
"twitter:description": html_to_raw(instance.metadata.get("summary", "")), "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": instance.metadata.get("image", ""),
"twitter:image:alt": html_to_raw(instance.metadata.get("summary", "")), "twitter:image:alt": html_to_raw(instance.metadata.get("summary", "")),
"twitter:url": os.path.join(instance.settings.get("SITEURL", ""), instance.url) "twitter:url": os.path.join(instance.settings.get("SITEURL", ""), instance.url)

View file

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

View file

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

View file

@ -18,7 +18,7 @@ flake8 scripts/ $FLAKE8_IGNORE
flake8 config/ $FLAKE8_IGNORE flake8 config/ $FLAKE8_IGNORE
flake8 tests/ $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 theme/templates/**/*.* theme/templates/*.*
mdspell --en-gb -ranx content/**/*.md content/*.md content/**/*.html content/*.html mdspell --en-gb -ranx content/**/*.md content/*.md content/**/*.html content/*.html

View file

@ -1,6 +1,7 @@
from tests import TestCase from tests import TestCase
from config import settings, DotDictionary
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
import pelicanconf as settings
from config import social as social_settings
from unittest import skipIf from unittest import skipIf
from os import environ from os import environ
@ -16,11 +17,11 @@ class CorePagesTestCase(TestCase):
def test_has_sitemap(self): def test_has_sitemap(self):
content = self.client.get('sitemap.xml') content = self.client.get('sitemap.xml')
self.assertIn(settings.url, content) self.assertIn(settings.SITEURL, content)
def test_has_atom_feed(self): def test_has_atom_feed(self):
content = self.client.get('feed.atom') content = self.client.get('feed.atom')
self.assertIn(settings.url, content) self.assertIn(settings.SITEURL, content)
def test_has_404_page(self): def test_has_404_page(self):
content = self.client.get('.404.html') content = self.client.get('.404.html')
@ -49,7 +50,7 @@ class CorePagesTestCase(TestCase):
content = self.client.get('index.html') content = self.client.get('index.html')
footer = content.footer footer = content.footer
for link in footer.find('p', class_="social").find_all('a'): 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])) self.assertIn("fa fa-", str(list(link.children)[0]))
@skipIf(not environ.get('BUILD_PRODUCTION', False), 'Not building production') @skipIf(not environ.get('BUILD_PRODUCTION', False), 'Not building production')
@ -59,60 +60,10 @@ class CorePagesTestCase(TestCase):
self.assertNotEqual(piwik_script_tag, None) self.assertNotEqual(piwik_script_tag, None)
piwik_script = self.get_children(piwik_script_tag) piwik_script = self.get_children(piwik_script_tag)
self.assertIn('piwik.js', piwik_script) 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') piwik_img = content.find('noscript', id='piwik').find('img')
self.assertIn(settings.piwik.url, piwik_img.attrs['src']) self.assertIn(settings.PIWIK['url'], piwik_img.attrs['src'])
self.assertIn(str(settings.piwik.site_id), 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)
class TestClientTestCase(TestCase): class TestClientTestCase(TestCase):

View file

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