Skip to content

Commit

Permalink
Merge pull request #66 from roverdotcom/DEV-103519-add-gha
Browse files Browse the repository at this point in the history
  • Loading branch information
JVenberg authored Feb 23, 2024
2 parents aca7106 + a9f5320 commit d69f93b
Show file tree
Hide file tree
Showing 16 changed files with 244 additions and 246 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/lint-and-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Run Tests and Linter

on:
pull_request:
branches:
- master
- main

jobs:
lint-and-test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: [3.8.18, 3.9.18, 3.10.13, 3.11.8]
django-version: [3.2, 4.2]

name: Lint and Test (Python ${{ matrix.python-version }} - Django ${{ matrix.django-version }})

steps:
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
pip install -q Django==${{ matrix.django-version }}
pip install -e .[flake8,tests]
- name: Add current directory to PYTHONPATH
run: echo "PYTHONPATH=$PWD" >> $GITHUB_ENV

- name: Lint with flake8
run: flake8

- name: Test with pytest
run: pytest
30 changes: 30 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: trailing-whitespace
args: [--markdown-linebreak-ext=md]
- id: end-of-file-fixer
- id: check-toml
- id: check-added-large-files
- id: debug-statements
- repo: https://github.com/PyCQA/flake8
rev: "7.0.0"
hooks:
- id: flake8
additional_dependencies:
- flake8-bugbear
- flake8-isort
- repo: https://github.com/psf/black
rev: "24.2.0"
hooks:
- id: black
- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
- id: isort
name: isort (python)
- repo: https://github.com/myint/autoflake
rev: v2.2.1
hooks:
- id: autoflake
14 changes: 0 additions & 14 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
include README.md
recursive-include django_inlinecss *.py
recursive-include django_inlinecss *.py
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Build Status](https://travis-ci.org/roverdotcom/django-inlinecss.png?branch=master)](https://travis-ci.org/roverdotcom/django-inlinecss)
[![Build Status](https://travis-ci.org/roverdotcom/django-inlinecss.svg?branch=master)](https://travis-ci.org/roverdotcom/django-inlinecss)

## About

Expand All @@ -14,8 +14,8 @@ template language.

- BeautifulSoup
- cssutils
- Python 2.7+,3.4+
- Django 1.11+
- Python 3.8+
- Django 3.2+


#### Step 2: Install django_inlinecss
Expand Down
4 changes: 2 additions & 2 deletions django_inlinecss/__version__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
VERSION = (0, 3, 0)
VERSION = (0, 4, 0)

__version__ = '.'.join(map(str, VERSION))
__version__ = ".".join(map(str, VERSION))
20 changes: 8 additions & 12 deletions django_inlinecss/conf.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals


try:
import importlib
except ImportError:
from django.utils import importlib

DEFAULT_ENGINE = 'django_inlinecss.engines.PynlinerEngine'
DEFAULT_CSS_LOADER = 'django_inlinecss.css_loaders.StaticfilesStorageCSSLoader'
DEFAULT_ENGINE = "django_inlinecss.engines.PynlinerEngine"
DEFAULT_CSS_LOADER = "django_inlinecss.css_loaders.StaticfilesStorageCSSLoader"


def load_class_by_path(path):
i = path.rfind('.')
module_path, class_name = path[:i], path[i + 1:]
i = path.rfind(".")
module_path, class_name = path[:i], path[i + 1 :]
module = importlib.import_module(module_path)
return getattr(module, class_name)


def get_engine():
from django.conf import settings
engine_path = getattr(settings, 'INLINECSS_ENGINE', DEFAULT_ENGINE)

engine_path = getattr(settings, "INLINECSS_ENGINE", DEFAULT_ENGINE)
return load_class_by_path(engine_path)


def get_css_loader():
from django.conf import settings
engine_path = getattr(settings, 'INLINECSS_CSS_LOADER', DEFAULT_CSS_LOADER)

engine_path = getattr(settings, "INLINECSS_CSS_LOADER", DEFAULT_CSS_LOADER)
return load_class_by_path(engine_path)
15 changes: 5 additions & 10 deletions django_inlinecss/css_loaders.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from django.contrib.staticfiles import finders
from django.contrib.staticfiles.storage import staticfiles_storage


class BaseCSSLoader(object):
class BaseCSSLoader:
def __init__(self):
pass

Expand All @@ -28,15 +23,15 @@ def load(self, path):
expanded_path = finders.find(path)

if expanded_path is None:
raise IOError('{} does not exist'.format(path))
raise OSError(f"{path} does not exist")

with open(expanded_path, 'rb') as css_file:
return css_file.read().decode('utf-8')
with open(expanded_path, "rb") as css_file:
return css_file.read().decode("utf-8")


class StaticfilesStorageCSSLoader(BaseCSSLoader):
def load(self, path):
"""
Retrieve CSS contents with staticfiles storage
"""
return staticfiles_storage.open(path).read().decode('utf-8')
return staticfiles_storage.open(path).read().decode("utf-8")
9 changes: 1 addition & 8 deletions django_inlinecss/engines.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from builtins import object

import pynliner


class EngineBase(object):
class EngineBase:
def __init__(self, html, css):
self.html = html
self.css = css
Expand Down
20 changes: 6 additions & 14 deletions django_inlinecss/templatetags/inlinecss.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from django import template
from django.utils.encoding import smart_text
from django.utils.encoding import smart_str

from django_inlinecss import conf


register = template.Library()


Expand All @@ -19,28 +13,26 @@ def __init__(self, nodelist, filter_expressions):

def render(self, context):
rendered_contents = self.nodelist.render(context)
css = ''
css = ""
for expression in self.filter_expressions:
path = expression.resolve(context, True)
if path is not None:
path = smart_text(path)
path = smart_str(path)

css_loader = conf.get_css_loader()()
css = ''.join((css, css_loader.load(path)))
css = "".join((css, css_loader.load(path)))

engine = conf.get_engine()(html=rendered_contents, css=css)
return engine.render()


@register.tag
def inlinecss(parser, token):
nodelist = parser.parse(('endinlinecss',))
nodelist = parser.parse(("endinlinecss",))

# prevent second parsing of endinlinecss
parser.delete_first_token()

args = token.split_contents()[1:]

return InlineCssNode(
nodelist,
[parser.compile_filter(arg) for arg in args])
return InlineCssNode(nodelist, [parser.compile_filter(arg) for arg in args])
2 changes: 1 addition & 1 deletion django_inlinecss/tests/static/foobar.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ div.foo {
margin: 10px 15px 20px 25px;
}
div.bar {
padding: 10px 15px 20px 25px;
padding: 10px 15px 20px 25px;
}
26 changes: 11 additions & 15 deletions django_inlinecss/tests/test_css_loaders.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
"""
Test CSS loaders
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from django.conf import settings
from django.test import TestCase
Expand All @@ -14,34 +10,34 @@
from django_inlinecss.css_loaders import StaticfilesStorageCSSLoader


@override_settings(STATICFILES_DIRS=[settings.STATIC_ROOT], STATIC_ROOT='')
@override_settings(STATICFILES_DIRS=[settings.STATIC_ROOT], STATIC_ROOT="")
class StaticfilesFinderCSSLoaderTestCase(TestCase):
def setUp(self):
self.loader = StaticfilesFinderCSSLoader()
super(StaticfilesFinderCSSLoaderTestCase, self).setUp()
super().setUp()

def test_loads_existing_css_file(self):
css = self.loader.load('bar.css')
self.assertIn('div.bar {', css)
css = self.loader.load("bar.css")
self.assertIn("div.bar {", css)

def test_load_file_does_not_exist(self):
with self.assertRaises(IOError) as e:
self.loader.load('missing.css')
self.loader.load("missing.css")

self.assertEqual(str(e.exception), 'missing.css does not exist')
self.assertEqual(str(e.exception), "missing.css does not exist")


class StaticfilesStorageCSSLoaderTestCase(TestCase):
def setUp(self):
self.loader = StaticfilesStorageCSSLoader()
super(StaticfilesStorageCSSLoaderTestCase, self).setUp()
super().setUp()

def test_loads_existing_css_file(self):
css = self.loader.load('bar.css')
self.assertIn('div.bar {', css)
css = self.loader.load("bar.css")
self.assertIn("div.bar {", css)

def test_load_file_does_not_exist(self):
with self.assertRaises(IOError) as e:
self.loader.load('missing.css')
self.loader.load("missing.css")

self.assertEqual(e.exception.strerror, 'No such file or directory')
self.assertEqual(e.exception.strerror, "No such file or directory")
Loading

0 comments on commit d69f93b

Please sign in to comment.