Skip to content

Commit

Permalink
Use inifile instead of ConfigParser and add tests of loading configs
Browse files Browse the repository at this point in the history
  • Loading branch information
spenczar committed Aug 24, 2016
1 parent 172bff3 commit 5caa92c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 27 deletions.
26 changes: 11 additions & 15 deletions lektor_s3.py
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
import ConfigParser
import mimetypes
import os
import posixpath
Expand All @@ -12,6 +11,7 @@

import boto3
import botocore.exceptions
import inifile
from s3transfer.manager import TransferManager


Expand Down Expand Up @@ -43,9 +43,7 @@ def config_filename(self):
return os.path.join(self.env.root_path, 'configs', 's3.ini')

def get_config(self):
config = ConfigParser.ConfigParser()
config.read(self.config_filename)
return config
return inifile.IniFile(self.config_filename)

def split_bucket_uri(self, target_url):
bucket = target_url.netloc
Expand Down Expand Up @@ -163,36 +161,34 @@ def create_headers(self, filename):
headers = {'ContentType': mime}

# Set defaults first
defaults = self.config.section_as_dict("DEFAULTS")
for upload_arg in TransferManager.ALLOWED_UPLOAD_ARGS:
upload_arg_value = self.config.defaults().get(upload_arg.lower())
upload_arg_value = defaults.get(upload_arg)
if upload_arg_value:
headers.update({upload_arg: upload_arg_value})

# Set file-specific rules
config_sections = self.config.sections()
for section in config_sections:
items = self.config.items(section)
options = dict((x, y) for x, y in items)
section_kvs = self.config.section_as_dict(section)
section_applies = False

if 'match' in options:
match = self.config.get(section, 'match')
if re.search(match, filename):
if 'match' in section_kvs:
if re.search(section_kvs['match'], filename):
section_applies = True

if not section_applies and 'extensions' in options:
if not section_applies and 'extensions' in section_kvs:
ext = os.path.splitext(filename)[1][1:]
extensions = self.config.get(section, 'extensions')
extensions = section_kvs['extensions']
if ext in extensions.split(','):
section_applies = True

if not section_applies:
continue

for upload_arg in TransferManager.ALLOWED_UPLOAD_ARGS:
if self.config.has_option(section, upload_arg):
upload_arg_value = self.config.get(section, upload_arg)
headers.update({upload_arg: upload_arg_value})
if upload_arg in section_kvs:
headers.update({upload_arg: section_kvs[upload_arg]})

return headers

Expand Down
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -25,6 +25,7 @@
'Lektor',
'boto3>=1.1.4',
's3transfer',
'inifile',
],
classifiers=[
'Development Status :: 4 - Beta',
Expand Down
57 changes: 45 additions & 12 deletions tests/test_config.py
@@ -1,24 +1,57 @@
import ConfigParser

from contextlib import contextmanager
import os
import shutil
import tempfile
from unittest import TestCase

import inifile
from lektor.environment import Environment
import lektor_s3


class MockEnvironment(object):
def __init__(self, root_path):
self.root_path = root_path

@contextmanager
def TemporaryDirectory():
name = tempfile.mkdtemp()
try:
yield name
finally:
shutil.rmtree(name)


class TestLoadConfig(TestCase):
def test_no_config_file(self):
publisher = lektor_s3.S3Publisher(MockEnvironment(""), "")
self.assertEqual(len(publisher.get_config().sections()), 0)

def test_load_config_file(self):
with TemporaryDirectory() as tempdir:
os.mkdir(os.path.join(tempdir, "configs"))
config = inifile.IniFile(os.path.join(tempdir, "configs", "s3.ini"))
config["section.key"] = "value"
config.save()

publisher = lektor_s3.S3Publisher(MockEnvironment(tempdir), "")
have = publisher.get_config().section_as_dict("section")
self.assertEqual(have["key"], "value")


class TestConfigureHeaders(TestCase):
def setUp(self):
self.old_get_config = lektor_s3.S3Publisher.get_config
def monkeypatched_get_config(self, *args, **kwargs):
config = ConfigParser.RawConfigParser(defaults={'CacheControl': 'public,max-age=3600'})
config.add_section('fonts')
config.set('fonts', 'ContentType', 'application/font-woff2')
config.set('fonts', 'extensions', 'woff2')
config.add_section('media')
config.set('media', 'CacheControl', 'public,max-age=259200')
config.set('media', 'extensions', 'jpg,jpeg,png,mp4')
config.add_section('static files')
config.set('static files', 'CacheControl', 'public,max-age=31536000')
config.set('static files', 'match', '.(css|js|woff|woff2)$')
config = inifile.IniData(mapping={
"DEFAULTS.CacheControl": "public,max-age=3600",
"fonts.ContentType": "application/font-woff2",
"fonts.extensions": "woff2",
"media.CacheControl": "public,max-age=259200",
"media.extensions": "jpg,jpeg,png,mp4",
"static files.CacheControl": "public,max-age=31536000",
"static files.match": "\.(css|js|woff|woff2)$",
})
return config
lektor_s3.S3Publisher.get_config = monkeypatched_get_config

Expand Down

0 comments on commit 5caa92c

Please sign in to comment.