From 137520685320f7493a2067477cd9bf1a660f4fc3 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Fri, 13 Jan 2017 21:41:40 +0000 Subject: [PATCH] simplify external settings --- config/__init__.py | 27 +++---------- config/{config.yml => social.yml} | 24 ------------ pelicanconf.py | 30 +++++++++----- plugins/links.py | 10 ++--- plugins/metatags.py | 4 +- plugins/pelican-jinja2content.py | 1 - plugins/static_build.py | 1 + scripts/test | 2 +- tests/tests_common.py | 65 ++++--------------------------- tests/tests_pages.py | 4 +- 10 files changed, 44 insertions(+), 124 deletions(-) rename config/{config.yml => social.yml} (80%) diff --git a/config/__init__.py b/config/__init__.py index e7e2577..36e7039 100644 --- a/config/__init__.py +++ b/config/__init__.py @@ -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') diff --git a/config/config.yml b/config/social.yml similarity index 80% rename from config/config.yml rename to config/social.yml index 40ff73d..25d2e5a 100644 --- a/config/config.yml +++ b/config/social.yml @@ -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 diff --git a/pelicanconf.py b/pelicanconf.py index e014f9d..aa62203 100644 --- a/pelicanconf.py +++ b/pelicanconf.py @@ -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 = { diff --git a/plugins/links.py b/plugins/links.py index b8d21e4..f1de30f 100644 --- a/plugins/links.py +++ b/plugins/links.py @@ -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(): diff --git a/plugins/metatags.py b/plugins/metatags.py index 8e6e28c..6c23684 100644 --- a/plugins/metatags.py +++ b/plugins/metatags.py @@ -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) diff --git a/plugins/pelican-jinja2content.py b/plugins/pelican-jinja2content.py index 0600a43..18381a2 100644 --- a/plugins/pelican-jinja2content.py +++ b/plugins/pelican-jinja2content.py @@ -1,5 +1,4 @@ import os - from pelican import signals from pelican import contents diff --git a/plugins/static_build.py b/plugins/static_build.py index 378dbb5..9dbe701 100644 --- a/plugins/static_build.py +++ b/plugins/static_build.py @@ -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'] diff --git a/scripts/test b/scripts/test index 703cb8c..94662a3 100755 --- a/scripts/test +++ b/scripts/test @@ -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 diff --git a/tests/tests_common.py b/tests/tests_common.py index ebc8e68..7f98edf 100644 --- a/tests/tests_common.py +++ b/tests/tests_common.py @@ -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): diff --git a/tests/tests_pages.py b/tests/tests_pages.py index 12d4333..2e2ffb9 100644 --- a/tests/tests_pages.py +++ b/tests/tests_pages.py @@ -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):