Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop Python 2 support #75

Merged
merged 7 commits into from
Mar 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Publish Python Package

on:
release:
types: [created]

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.5, 3.6, 3.7, 3.8, 3.9]
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- uses: actions/cache@v2
name: Configure pip caching
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/setup.py') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install dependencies
run: |
pip install -e '.[test]'
- name: Run tests
run: python test.py
deploy:
runs-on: ubuntu-latest
needs: [test]
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: "3.8"
- uses: actions/cache@v2
name: Configure pip caching
with:
path: ~/.cache/pip
key: ${{ runner.os }}-publish-pip-${{ hashFiles('**/setup.py') }}
restore-keys: |
${{ runner.os }}-publish-pip-
- name: Install dependencies
run: |
pip install setuptools wheel twine
- name: Publish
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }}
run: |
python setup.py sdist bdist_wheel
twine upload --verbose dist/*
29 changes: 29 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Test

on: [push]

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.5, 3.6, 3.7, 3.8, 3.9]
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- uses: actions/cache@v2
name: Configure pip caching
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/setup.py') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install dependencies
run: |
pip install -e '.[test]'
- name: Run tests
run: |
python test.py
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ docs/_build/
target/

.DS_Store
.vscode/
5 changes: 0 additions & 5 deletions .pipignore

This file was deleted.

6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
language: python
dist: xenial
python:
- "2.7"
- "3.4"
- "3.5"
- "3.6"
- "3.7"
- "3.8"
- "3.9"
# command to install dependencies
install:
# use pyaml for tests above py26
- pip install -r requirements.txt
- "python setup.py install"
# command to run tests
script: python setup.py test
script: python test.py
3 changes: 0 additions & 3 deletions .vscode/settings.json

This file was deleted.

20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
Python Frontmatter
==================
# Python Frontmatter

[Jekyll](http://jekyllrb.com/)-style YAML front matter offers a useful way to add arbitrary, structured metadata to text documents, regardless of type.

This is a small package to load and parse files (or just text) with YAML front matter.

[![Build Status](https://travis-ci.org/eyeseast/python-frontmatter.svg?branch=master)](https://travis-ci.org/eyeseast/python-frontmatter)

Install:
--------
## Install:

pip install python-frontmatter


Usage:
------
## Usage:

```python
import frontmatter
Expand All @@ -31,13 +27,15 @@ Or a file (or file-like object):
```python
>>> with open('tests/hello-world.markdown') as f:
... post = frontmatter.load(f)

```

Or load from text:

```python
>>> with open('tests/hello-world.markdown') as f:
... post = frontmatter.loads(f.read())

```

Access content:
Expand All @@ -49,13 +47,15 @@ Well, hello there, world.
# this works, too
>>> print(post)
Well, hello there, world.

```

Use metadata (metadata gets proxied as post keys):

```python
>>> print(post['title'])
Hello, world!

```

Metadata is a dictionary, with some handy proxies:
Expand All @@ -68,6 +68,7 @@ Metadata is a dictionary, with some handy proxies:
>>> post['excerpt'] = 'tl;dr'
>>> pprint(post.metadata)
{'excerpt': 'tl;dr', 'layout': 'post', 'title': 'Hello, world!'}

```

If you don't need the whole post object, just parse:
Expand All @@ -77,6 +78,7 @@ If you don't need the whole post object, just parse:
... metadata, content = frontmatter.parse(f.read())
>>> print(metadata['title'])
Hello, world!

```

Write back to plain text, too:
Expand All @@ -90,6 +92,8 @@ title: Hello, world!
---
Well, hello there, world.

```

Or write to a file (or file-like object):

```python
Expand All @@ -103,5 +107,5 @@ layout: post
title: Hello, world!
---
Well, hello there, world.
```

```
31 changes: 12 additions & 19 deletions frontmatter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
"""
Python Frontmatter: Parse and manage posts with YAML frontmatter
"""
from __future__ import unicode_literals

import codecs
import re

import six

from .util import u
from .default_handlers import YAMLHandler, JSONHandler, TOMLHandler
Expand Down Expand Up @@ -38,7 +36,7 @@ def detect_format(text, handlers):

``text`` should be unicode text about to be parsed.

``handlers`` is a dictionary where keys are opening delimiters
``handlers`` is a dictionary where keys are opening delimiters
and values are handler instances.
"""
for pattern, handler in handlers.items():
Expand Down Expand Up @@ -93,9 +91,9 @@ def parse(text, encoding="utf-8", handler=None, **defaults):

def check(fd, encoding="utf-8"):
"""
Check if a file-like object or filename has a frontmatter,
Check if a file-like object or filename has a frontmatter,
return True if exists, False otherwise.

If it contains a frontmatter but it is empty, return True as well.

::
Expand All @@ -118,7 +116,7 @@ def checks(text, encoding="utf-8"):
"""
Check if a text (binary or unicode) has a frontmatter,
return True if exists, False otherwise.

If it contains a frontmatter but it is empty, return True as well.

::
Expand All @@ -134,7 +132,7 @@ def checks(text, encoding="utf-8"):

def load(fd, encoding="utf-8", handler=None, **defaults):
"""
Load and parse a file-like object or filename,
Load and parse a file-like object or filename,
return a :py:class:`post <frontmatter.Post>`.

::
Expand Down Expand Up @@ -202,12 +200,12 @@ def dump(post, fd, encoding="utf-8", handler=None, **kwargs):

def dumps(post, handler=None, **kwargs):
"""
Serialize a :py:class:`post <frontmatter.Post>` to a string and return text.
Serialize a :py:class:`post <frontmatter.Post>` to a string and return text.
This always returns unicode text, which can then be encoded.

Passing ``handler`` will change how metadata is turned into text. A handler
passed as an argument will override ``post.handler``, with
:py:class:`YAMLHandler <frontmatter.default_handlers.YAMLHandler>` used as
passed as an argument will override ``post.handler``, with
:py:class:`YAMLHandler <frontmatter.default_handlers.YAMLHandler>` used as
a default.
::

Expand Down Expand Up @@ -239,15 +237,15 @@ def dumps(post, handler=None, **kwargs):
class Post(object):
"""
A post contains content and metadata from Front Matter. This is what gets
returned by :py:func:`load <frontmatter.load>` and :py:func:`loads <frontmatter.loads>`.
Passing this to :py:func:`dump <frontmatter.dump>` or :py:func:`dumps <frontmatter.dumps>`
returned by :py:func:`load <frontmatter.load>` and :py:func:`loads <frontmatter.loads>`.
Passing this to :py:func:`dump <frontmatter.dump>` or :py:func:`dumps <frontmatter.dumps>`
will turn it back into text.

For convenience, metadata values are available as proxied item lookups.
For convenience, metadata values are available as proxied item lookups.
"""

def __init__(self, content, handler=None, **metadata):
self.content = u(content)
self.content = str(content)
self.metadata = metadata
self.handler = handler

Expand All @@ -271,11 +269,6 @@ def __bytes__(self):
return self.content.encode("utf-8")

def __str__(self):
if six.PY2:
return self.__bytes__()
return self.content

def __unicode__(self):
return self.content

def get(self, key, default=None):
Expand Down
7 changes: 3 additions & 4 deletions frontmatter/default_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@


"""
from __future__ import unicode_literals

import json
import re
Expand Down Expand Up @@ -141,7 +140,7 @@

class BaseHandler(object):
"""
BaseHandler lays out all the steps to detecting, splitting, parsing and
BaseHandler lays out all the steps to detecting, splitting, parsing and
exporting front matter metadata.

All default handlers are subclassed from BaseHandler.
Expand Down Expand Up @@ -169,7 +168,7 @@ def detect(self, text):
Decide whether this handler can parse the given ``text``,
and return True or False.

Note that this is *not* called when passing a handler instance to
Note that this is *not* called when passing a handler instance to
:py:func:`frontmatter.load <frontmatter.load>` or :py:func:`loads <frontmatter.loads>`.
"""
if self.FM_BOUNDARY.match(text):
Expand Down Expand Up @@ -207,7 +206,7 @@ class YAMLHandler(BaseHandler):

def load(self, fm, **kwargs):
"""
Parse YAML front matter. This uses yaml.SafeLoader by default.
Parse YAML front matter. This uses yaml.SafeLoader by default.
"""
kwargs.setdefault("Loader", SafeLoader)
return yaml.load(fm, **kwargs)
Expand Down
3 changes: 1 addition & 2 deletions frontmatter/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
"""
Utilities for handling unicode and other repetitive bits
"""
import six


def u(text, encoding="utf-8"):
"Return unicode text, no matter what"

if isinstance(text, six.binary_type):
if isinstance(text, bytes):
text = text.decode(encoding)

# it's already unicode
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
PyYAML
six
toml
pyaml
toml
Loading