Skip to content

Commit

Permalink
Merge pull request #7 from ickc/python2
Browse files Browse the repository at this point in the history
Python2 support
  • Loading branch information
ickc committed Jan 14, 2017
2 parents 4274a72 + a975634 commit 754032a
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 86 deletions.
1 change: 1 addition & 0 deletions .coveragerc
Expand Up @@ -7,3 +7,4 @@ exclude_lines =
return \[\]
return None
__version__
py2
3 changes: 0 additions & 3 deletions .gitignore
Expand Up @@ -23,6 +23,3 @@ htmlcov/

# rst2html
README.html

# generated pickle
*.pkl
22 changes: 17 additions & 5 deletions .travis.yml
Expand Up @@ -4,13 +4,16 @@ cache:
- pip
# build matrix: different python and pandoc versions
python:
- "2.7"
- "3.3"
- "3.4"
- "3.5"
# - "3.5-dev" # 3.5 development branch
# - "3.6-dev" # 3.6 development branch
# - "nightly" # currently points to 3.7-dev
- "pypy3"
- "3.5-dev" # 3.5 development branch
- "3.6-dev" # 3.6 development branch
- "nightly" # currently points to 3.7-dev
# pypy (version info from [Changelogs — PyPy documentation](http://doc.pypy.org/en/latest/index-of-whatsnew.html))
- "pypy" # PyPy2.7 5.3.1 (CPython 2.7 compatible)
- "pypy3" # PyPy3 2.4.0 (CPython 3.2 compatible)
env:
# - pandocVersion=1.13.2
# - pandocVersion=1.14
Expand All @@ -36,7 +39,7 @@ matrix:
- python: "3.5-dev"
- python: "3.6-dev"
- python: "nightly"
- python: "pypy3"
# - python: "pypy3"
# - env: pandocVersion=1.13.2
# - env: pandocVersion=1.14
# - env: pandocVersion=1.14.0.1
Expand All @@ -62,6 +65,14 @@ install:
sudo dpkg -i $file
- pip install -U pip
- pip install -e .[test]
before_script:
# pasteurize except setup.py & pantable/version.py
- |
mv setup.py setup.py.temp
mv pantable/version.py pantable/version.py.temp
pasteurize -wnj 4 .
mv setup.py.temp setup.py
mv pantable/version.py.temp pantable/version.py
# command to run tests
script:
- make -j4 test
Expand All @@ -71,6 +82,7 @@ deploy:
provider: pypi
user: ickc
password: $pypi_password
distributions: "sdist bdist_wheel"
skip_cleanup: true
on:
tags: true
Expand Down
48 changes: 29 additions & 19 deletions makefile
@@ -1,6 +1,8 @@
SHELL := /usr/bin/env bash

# configure engine
python := python
pip := pip
## LaTeX engine
### LaTeX workflow: pdf; xelatex; lualatex
latexmkEngine := pdf
Expand All @@ -11,7 +13,8 @@ HTMLVersion := html5
## ePub
ePubVersion := epub

filter := pantable/pantable.py
pantable := pantable
pantable2csv := pantable2csv

CSSURL:=https://ickc.github.io/markdown-latex-css

Expand All @@ -23,7 +26,7 @@ pandocArgMD := -f markdown+abbreviations+autolink_bare_uris+markdown_attribute+m
## TeX/PDF
### LaTeX workflow
latexmkArg := -$(latexmkEngine)
pandocArgFragment := $(pandocArgCommon) --filter=$(filter)
pandocArgFragment := $(pandocArgCommon) --filter=$(pantable)
### pandoc workflow
pandocArgStandalone := $(pandocArgFragment) --toc-depth=1 -s -N
## HTML/ePub
Expand All @@ -48,7 +51,9 @@ docsAll := $(docsPdf) docs/index.html README.md README.rst README.html # $(docsH
all: $(testAll) $(docsAll)
docs: $(docsAll)
readme: docs
test: pytest pep8 pylint
test: pytest pep8
coverage html
testFull: pytest pep8 pylint
coverage html

clean:
Expand All @@ -62,11 +67,11 @@ Clean:

# Making dependancies #################################################################################################################################################################################

%.native: %.md $(filter)
pandoc -t native -F $(filter) -o $@ $<
%.pdf: %.md $(filter)
%.native: %.md $(pantable)
pandoc -t native -F $(pantable) -o $@ $<
%.pdf: %.md $(pantable)
pandoc $(pandocArgStandalone) -o $@ $<
%.html: %.md $(filter)
%.html: %.md $(pantable)
pandoc $(pandocArgHTML) $< -o $@

# readme
Expand All @@ -89,25 +94,26 @@ README.html: README.rst
# Deploy to PyPI
## by Travis, properly git tagged
pypi:
git tag -a v$$(python setup.py --version) -m 'Deploy to PyPI' && git push origin v$$(python setup.py --version)
git tag -a v$$($(python) setup.py --version) -m 'Deploy to PyPI' && git push origin v$$($(python) setup.py --version)
## Manually
pypiManual:
python setup.py register -r pypitest && python setup.py sdist upload -r pypitest && python setup.py register -r pypi && python setup.py sdist upload -r pypi
$(python) setup.py register -r pypitest && $(python) setup.py sdist upload -r pypitest && $(python) setup.py register -r pypi && $(python) setup.py sdist upload -r pypi

init:
pip install -r requirements.txt
pip install -r tests/requirements.txt
$(pip) install -r requirements.txt
$(pip) install -r tests/requirements.txt

dev:
$(pip) install -e .[test]

pytest: $(testNative) tests/tables.pkl tests/test_idempotent.native
python3 -m pytest -vv --cov=pantable tests
pytest: $(testNative) tests/test_idempotent.native
$(python) -m pytest -vv --cov=pantable tests
pytestLite:
python3 -m pytest -vv --cov=pantable tests
%.pkl: %.md
pandoc -F tests/to_pkl.py $< > /dev/null
$(python) -m pytest -vv --cov=pantable tests
tests/reference_idempotent.native: tests/test_pantable.md
pandoc --normalize -t native -F pantable/pantable.py -F pantable/pantable2csv.py -F pantable/pantable.py -F pantable/pantable2csv.py -o $@ $<
pandoc --normalize -t native -F $(pantable) -F $(pantable2csv) -F $(pantable) -F $(pantable2csv) -o $@ $<
tests/test_idempotent.native: tests/reference_idempotent.native
pandoc --normalize -f native -t native -F pantable/pantable.py -F pantable/pantable2csv.py -o $@ $<
pandoc --normalize -f native -t native -F $(pantable) -F $(pantable2csv) -o $@ $<

# check python styles
pep8:
Expand All @@ -119,14 +125,18 @@ pyflakes:
flake8:
flake8 .
pylint:
pylint pantable/pantable.py
pylint pantable

# cleanup python
autopep8:
autopep8 . --recursive --in-place --pep8-passes 2000 --verbose
autopep8Aggressive:
autopep8 . --recursive --in-place --pep8-passes 2000 --verbose --aggressive --aggressive

# pasteurize
past:
pasteurize -wnj 4 .

# cleanup markdown
cleanup: style normalize
## Normalize white spaces:
Expand Down
16 changes: 11 additions & 5 deletions pantable/pantable.py
@@ -1,5 +1,3 @@
#!/usr/bin/env python3

r"""
Panflute filter to parse table in fenced YAML code blocks.
Currently only CSV table is supported.
Expand Down Expand Up @@ -48,8 +46,12 @@
import io
import panflute

import sys
py2 = sys.version_info[0] == 2

# begin helper functions


def to_bool(to_be_bool, default=True):
"""
Do nothing if to_be_bool is boolean,
Expand Down Expand Up @@ -155,7 +157,8 @@ def parse_alignment(alignment_string, number_of_columns):
# prepare alignment_string
try:
# test valid type
if not isinstance(alignment_string, str):
str_universal = basestring if py2 else str
if not isinstance(alignment_string, str_universal):
raise TypeError
number_of_alignments = len(alignment_string)
# truncate and debug if too long
Expand Down Expand Up @@ -195,13 +198,16 @@ def read_data(include, data):
Return None when the include path is invalid.
"""
if include is None:
with io.StringIO(data) as file:
if py2:
data = data.encode('utf-8')
io_universal = io.BytesIO if py2 else io.StringIO
with io_universal(data) as file:
raw_table_list = list(csv.reader(file))
else:
try:
with open(str(include)) as file:
raw_table_list = list(csv.reader(file))
except FileNotFoundError:
except IOError: # FileNotFoundError is not in Python2
raw_table_list = None
panflute.debug("pantable: file not found from the path", include)
return raw_table_list
Expand Down
12 changes: 7 additions & 5 deletions pantable/pantable2csv.py
@@ -1,5 +1,3 @@
#!/usr/bin/env python3

r"""
Panflute filter to convert any native pandoc tables into the CSV table format used by pantable:
Expand Down Expand Up @@ -56,6 +54,9 @@
import panflute
import yaml

import sys
py2 = sys.version_info[0] == 2


def ast2markdown(ast):
"""
Expand All @@ -68,7 +69,7 @@ def ast2markdown(ast):
)


def table2csv(elem, doc):
def table2csv(elem, *__):
"""
find Table element and return a csv table in code-block with class "table"
"""
Expand All @@ -95,7 +96,7 @@ def table2csv(elem, doc):
options['markdown'] = True

# option in YAML
yaml_metadata = yaml.dump(options)
yaml_metadata = yaml.safe_dump(options)

# table in panflute AST
table_body = elem.content
Expand All @@ -106,7 +107,8 @@ def table2csv(elem, doc):
for cell in row.content]
for row in table_body]
# table in CSV
with io.StringIO() as file:
io_universal = io.BytesIO if py2 else io.StringIO
with io_universal() as file:
writer = csv.writer(file)
writer.writerows(table_list)
csv_table = file.getvalue()
Expand Down
3 changes: 3 additions & 0 deletions setup.cfg
@@ -1,2 +1,5 @@
[metadata]
description-file = README.rst

[bdist_wheel]
universal=1
10 changes: 4 additions & 6 deletions setup.py
Expand Up @@ -64,11 +64,9 @@
# Specify the Python versions you support here. In particular, ensure
# that you indicate whether you support Python 2, Python 3 or both.
# https://pypi.python.org/pypi?%3Aaction=list_classifiers
# 'Programming Language :: Python :: 2',
# 'Programming Language :: Python :: 2.6',
# 'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.2',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
Expand All @@ -92,15 +90,15 @@
# your project is installed. For an analysis of "install_requires" vs pip's
# requirements files see:
# https://packaging.python.org/en/latest/requirements.html
install_requires=['panflute>=1.6.8'],
install_requires=['panflute>=1.8.2'],

# List additional groups of dependencies here (e.g. development
# dependencies). You can install these using the following syntax,
# for example:
# $ pip install -e .[dev,test]
extras_require={
'dev': ['check-manifest'],
'test': ['shutilwhich', 'pep8', 'pylint', 'pytest', 'pytest-cov', 'coverage', 'coveralls'],
'test': ['shutilwhich', 'pep8', 'pylint', 'pytest', 'pytest-cov', 'coverage', 'coveralls', 'future'],
},

# If there are data files included in your packages that need to be
Expand Down
22 changes: 13 additions & 9 deletions tests/pantable/test_convert2table.py
Expand Up @@ -2,6 +2,7 @@
`header` and `markdown` is checked by `test_to_bool` instead
"""
from .context import convert2table
from panflute import *


def test_convert2table():
Expand All @@ -10,22 +11,25 @@ def test_convert2table():
1,2
3,4
'''
table = convert2table({'width': [0, 0]}, data)
assert str(
table) == "Table(TableRow(TableCell(Plain(Str(1))) TableCell(Plain(Str(2)))) TableRow(TableCell(Plain(Str(3))) TableCell(Plain(Str(4)))); alignment=['AlignDefault', 'AlignDefault'], width=[0.0, 0.0], rows=2, cols=2)"
table_converted = convert2table({'width': [0, 0]}, data)
table_referenced = Table(TableRow(TableCell(Plain(Str('1'))), TableCell(Plain(Str('2')))), TableRow(TableCell(
Plain(Str('3'))), TableCell(Plain(Str('4')))), alignment=['AlignDefault', 'AlignDefault'], width=[0.0, 0.0])
assert repr(table_converted) == repr(table_referenced)
# empty header_row
table = convert2table({'header': False}, data)
assert str(
table) == "Table(TableRow(TableCell(Plain(Str())) TableCell(Plain(Str()))) TableRow(TableCell(Plain(Str(1))) TableCell(Plain(Str(2)))) TableRow(TableCell(Plain(Str(3))) TableCell(Plain(Str(4)))); alignment=['AlignDefault', 'AlignDefault'], width=[0.5, 0.5], rows=3, cols=2)"
table_converted = convert2table({'header': False}, data)
table_referenced = Table(TableRow(TableCell(Plain(Str(''))), TableCell(Plain(Str('')))), TableRow(TableCell(Plain(Str('1'))), TableCell(Plain(
Str('2')))), TableRow(TableCell(Plain(Str('3'))), TableCell(Plain(Str('4')))), alignment=['AlignDefault', 'AlignDefault'], width=[0.5, 0.5])
assert repr(table_converted) == repr(table_referenced)
# empty table
data = ','
table = convert2table({}, data)
assert table == []
# 1 row table
data = '1,2'
table = convert2table({}, data)
assert str(
table) == r"""Table(TableRow(TableCell(Plain(Str(1))) TableCell(Plain(Str(2)))); alignment=['AlignDefault', 'AlignDefault'], width=[0.5, 0.5], rows=1, cols=2)"""
table_converted = convert2table({}, data)
table_referenced = Table(TableRow(TableCell(Plain(Str('1'))), TableCell(
Plain(Str('2')))), alignment=['AlignDefault', 'AlignDefault'], width=[0.5, 0.5])
assert repr(table_converted) == repr(table_referenced)
# empty data
table = convert2table({}, '')
assert table == []
Expand Down
27 changes: 17 additions & 10 deletions tests/pantable/test_parse_table_list.py
Expand Up @@ -2,27 +2,34 @@
"""
from .context import parse_table_list
from panflute import *


def test_parse_table_list():
markdown = False
raw_table_list = [['1', '2'], ['3', '4']]
assert str(parse_table_list(markdown, raw_table_list)
) == '[TableRow(TableCell(Plain(Str(1))) TableCell(Plain(Str(2)))), TableRow(TableCell(Plain(Str(3))) TableCell(Plain(Str(4))))]'
table_list_converted = parse_table_list(markdown, raw_table_list)
table_list_referenced = [TableRow(TableCell(Plain(Str('1'))), TableCell(Plain(
Str('2')))), TableRow(TableCell(Plain(Str('3'))), TableCell(Plain(Str('4'))))]
assert repr(table_list_converted) == repr(table_list_referenced)
markdown = True
raw_table_list = [['**markdown**', '~~like this~~'],
['$E=mc^2$', '`great`']]
assert str(parse_table_list(markdown, raw_table_list)
) == '''[TableRow(TableCell(Para(Strong(Str(markdown)))) TableCell(Para(Strikeout(Str(like) Space Str(this))))), TableRow(TableCell(Para(Math(E=mc^2; format='InlineMath'))) TableCell(Para(Code(great))))]'''
table_list_converted = parse_table_list(markdown, raw_table_list)
table_list_referenced = [TableRow(TableCell(Para(Strong(Str('markdown')))), TableCell(Para(Strikeout(Str('like'), Space, Str(
'this'))))), TableRow(TableCell(Para(Math('E=mc^2', format='InlineMath'))), TableCell(Para(Code('great'))))]
assert repr(table_list_converted) == repr(table_list_referenced)
# test irregular table
markdown = True
raw_table_list = [['1', '', '', '', '', ''],
['2', '3', '4', '5', '6', '7']]
assert str(parse_table_list(markdown, raw_table_list)
) == '''[TableRow(TableCell(Para(Str(1))) TableCell() TableCell() TableCell() TableCell() TableCell()), TableRow(TableCell(Para(Str(2))) TableCell(Para(Str(3))) TableCell(Para(Str(4))) TableCell(Para(Str(5))) TableCell(Para(Str(6))) TableCell(Para(Str(7))))]'''
table_list_converted = parse_table_list(markdown, raw_table_list)
table_list_referenced = [TableRow(TableCell(Para(Str('1'))), TableCell(), TableCell(), TableCell(), TableCell(), TableCell()), TableRow(TableCell(
Para(Str('2'))), TableCell(Para(Str('3'))), TableCell(Para(Str('4'))), TableCell(Para(Str('5'))), TableCell(Para(Str('6'))), TableCell(Para(Str('7'))))]
assert repr(table_list_converted) == repr(table_list_referenced)
markdown = False
assert str(
parse_table_list(
markdown,
raw_table_list)) == '''[TableRow(TableCell(Plain(Str(1))) TableCell(Plain(Str())) TableCell(Plain(Str())) TableCell(Plain(Str())) TableCell(Plain(Str())) TableCell(Plain(Str()))), TableRow(TableCell(Plain(Str(2))) TableCell(Plain(Str(3))) TableCell(Plain(Str(4))) TableCell(Plain(Str(5))) TableCell(Plain(Str(6))) TableCell(Plain(Str(7))))]'''
table_list_converted = parse_table_list(markdown, raw_table_list)
table_list_referenced = [TableRow(TableCell(Plain(Str('1'))), TableCell(Plain(Str(''))), TableCell(Plain(Str(''))), TableCell(Plain(Str(''))), TableCell(Plain(Str(''))), TableCell(Plain(
Str('')))), TableRow(TableCell(Plain(Str('2'))), TableCell(Plain(Str('3'))), TableCell(Plain(Str('4'))), TableCell(Plain(Str('5'))), TableCell(Plain(Str('6'))), TableCell(Plain(Str('7'))))]
assert repr(table_list_converted) == repr(table_list_referenced)
return

0 comments on commit 754032a

Please sign in to comment.