Skip to content

Commit

Permalink
Testing newer Python and Django versions
Browse files Browse the repository at this point in the history
This is based off of work from @TTycho, @awais786, and @arpitjain799
  • Loading branch information
mjumbewu committed Oct 24, 2023
2 parents 4d8c65e + 2c28153 commit 0436bb0
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 47 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: "CodeQL"
on:
workflow_dispatch:
#push:
# branches: [master]
#pull_request:
# branches: [master]

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: ["python"]

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
queries: security-and-quality

- name: Autobuild
uses: github/codeql-action/autobuild@v2

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
80 changes: 80 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
name: Unit tests

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:
name: Python ${{ matrix.env.python }} | ${{ matrix.env.TOXENV }}
runs-on: ubuntu-22.04
strategy:
fail-fast: false
matrix:
env:
- python: 2.7
TOXENV: py27-django18-drf2
- python: 2.7
TOXENV: py27-django18-drf36
- python: 2.7
TOXENV: py27-django111-drf2
- python: 2.7
TOXENV: py27-django111-drf36

- python: 3.7
TOXENV: py37-django21-drf3
- python: 3.7
TOXENV: py37-django22-drf3
- python: 3.7
TOXENV: py37-django30-drf3

- python: 3.8
TOXENV: py38-django21-drf3
- python: 3.8
TOXENV: py38-django22-drf3
- python: 3.8
TOXENV: py38-django30-drf3
- python: 3.8
TOXENV: py38-django31-drf3
- python: 3.8
TOXENV: py38-django32-drf3

- python: 3.9
TOXENV: py39-django22-drf3
- python: 3.9
TOXENV: py39-django30-drf3
- python: 3.9
TOXENV: py39-django31-drf3
- python: 3.9
TOXENV: py39-django32-drf3
- python: 3.9
TOXENV: py39-django40-drf3
- python: 3.9
TOXENV: py39-django41-drf3
- python: 3.9
TOXENV: py39-django42-drf3

- python: '3.10'
TOXENV: py310-django32-drf3
- python: '3.10'
TOXENV: py310-django40-drf3
- python: '3.10'
TOXENV: py310-django41-drf3
- python: '3.10'
TOXENV: py310-django42-drf3


steps:
- uses: actions/checkout@v2
- name: setup python
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.env.python }}
- name: Install tox
run: pip install tox
- name: Run Tests
env:
TOXENV: django${{ matrix.env.TOXENV }}
run: tox -e ${{ matrix.env.TOXENV }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ build/
*.egg-info/
*~

/env*/
/.settings/
/.pydevproject
/.project
Expand Down
2 changes: 0 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
Django>=1.3
djangorestframework
six>=1.4.1
unicodecsv
15 changes: 4 additions & 11 deletions rest_framework_csv/parsers.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import unicodecsv as csv
#import csv
import csv
import codecs
import io
import six

from django.conf import settings
from rest_framework.parsers import BaseParser
from rest_framework.exceptions import ParseError
from rest_framework_csv.orderedrows import OrderedRows


def unicode_csv_reader(csv_data, dialect=csv.excel, charset='utf-8', **kwargs):
csv_reader = csv.reader(csv_data, dialect=dialect, encoding=charset, **kwargs)
for row in csv_reader:
yield row

def universal_newlines(stream):
# It's possible that the stream was not opened in universal
# newline mode. If not, we may have a single "row" that has a
Expand All @@ -40,9 +33,9 @@ def parse(self, stream, media_type=None, parser_context=None):
encoding = parser_context.get('encoding', settings.DEFAULT_CHARSET)

try:
strdata = stream.read()
binary = universal_newlines(strdata)
rows = unicode_csv_reader(binary, delimiter=delimiter, charset=encoding)
strdata = stream.read().decode(encoding)
lines = universal_newlines(strdata)
rows = csv.reader(lines, dialect=csv.excel, delimiter=delimiter)
data = OrderedRows(next(rows))
for row in rows:
row_data = dict(zip(data.header, row))
Expand Down
25 changes: 9 additions & 16 deletions rest_framework_csv/renderers.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
from __future__ import unicode_literals
import codecs
import unicodecsv as csv
import csv
from django.conf import settings
from rest_framework.renderers import *
from six import BytesIO, text_type
from io import StringIO
from rest_framework_csv.orderedrows import OrderedRows
from rest_framework_csv.misc import Echo
from types import GeneratorType

from logging import getLogger
log = getLogger(__name__)

# six versions 1.3.0 and previous don't have PY2
try:
from six import PY2
except ImportError:
import sys
PY2 = sys.version_info[0] == 2


class CSVRenderer(BaseRenderer):
"""
Expand Down Expand Up @@ -52,12 +45,12 @@ def render(self, data, media_type=None, renderer_context={}, writer_opts=None):
encoding = renderer_context.get('encoding', settings.DEFAULT_CHARSET)

table = self.tablize(data, header=header, labels=labels)
csv_buffer = BytesIO()
csv_writer = csv.writer(csv_buffer, encoding=encoding, **writer_opts)
csv_buffer = StringIO()
csv_writer = csv.writer(csv_buffer, **writer_opts)
for row in table:
csv_writer.writerow(row)

return csv_buffer.getvalue()
return csv_buffer.getvalue().encode(encoding)

def tablize(self, data, header=None, labels=None):
"""
Expand Down Expand Up @@ -156,7 +149,7 @@ def nest_flat_item(self, flat_item, prefix):
def flatten_list(self, l):
flat_list = {}
for index, item in enumerate(l):
index = text_type(index)
index = str(index)
flat_item = self.flatten_item(item)
nested_item = self.nest_flat_item(flat_item, index)
flat_list.update(nested_item)
Expand All @@ -165,7 +158,7 @@ def flatten_list(self, l):
def flatten_dict(self, d):
flat_dict = {}
for key, item in d.items():
key = text_type(key)
key = str(key)
flat_item = self.flatten_item(item)
nested_item = self.nest_flat_item(flat_item, key)
flat_dict.update(nested_item)
Expand Down Expand Up @@ -230,9 +223,9 @@ def render(self, data, media_type=None, renderer_context={}):

table = self.tablize(data, header=header, labels=labels)
csv_buffer = Echo()
csv_writer = csv.writer(csv_buffer, encoding=encoding, **writer_opts)
csv_writer = csv.writer(csv_buffer, **writer_opts)
for row in table:
yield csv_writer.writerow(row)
yield csv_writer.writerow(row).encode(encoding)


class PaginatedCSVRenderer (CSVRenderer):
Expand Down
13 changes: 7 additions & 6 deletions rest_framework_csv/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from __future__ import unicode_literals

import csv
from six import BytesIO, PY3
import sys
from io import BytesIO
from types import GeneratorType

from django.test import TestCase
Expand Down Expand Up @@ -134,8 +135,8 @@ def test_render_data_with_writer_opts_set_via_CSVRenderer(self):
data = [{'a': 'test', 'b': 'hello'}, {'a': 'foo', 'b': 'bar'}]
writer_opts = {
'quoting': csv.QUOTE_ALL,
'quotechar': '|' if PY3 else b'|',
'delimiter': ';' if PY3 else b';',
'quotechar': '|',
'delimiter': ';',
}
renderer.writer_opts = writer_opts
dump = renderer.render(data)
Expand All @@ -149,8 +150,8 @@ def test_render_data_with_writer_opts_set_via_renderer_context(self):
data = [{'a': 'test', 'b': 'hello'}, {'a': 'foo', 'b': 'bar'}]
writer_opts = {
'quoting': csv.QUOTE_ALL,
'quotechar': '|' if PY3 else b'|',
'delimiter': ';' if PY3 else b';',
'quotechar': '|',
'delimiter': ';',
}
dump = renderer.render(data, renderer_context={'writer_opts': writer_opts})
self.assertEquals(dump.count(b';'), 3)
Expand Down Expand Up @@ -246,7 +247,7 @@ def test_parse_file_with_only_carriage_returns(self):

parser = CSVParser()

with open(CSVFILE, 'rbU') as csv_file:
with open(CSVFILE, 'rbU' if sys.version_info <= (3, 10) else 'rb') as csv_file:
data = parser.parse(csv_file)
self.assertEqual(data, [{'Name': 'Kathryn Miller', 'ID': '67', 'Country': 'United States'},
{'Name': 'Jen Mark', 'ID': '78', 'Country': 'Canada'}])
Expand Down
12 changes: 6 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
author = 'Mjumbe Wawatu Ukweli'
author_email = '[email protected]'
license = 'BSD'
install_requires = ['djangorestframework', 'six', 'unicodecsv']
install_requires = ['djangorestframework']


def get_version(package):
Expand Down Expand Up @@ -77,12 +77,12 @@ def get_package_data(package):
"License :: OSI Approved :: BSD License",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.3",
"Programming Language :: Python :: 3.4",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Framework :: Django",
],
)
16 changes: 10 additions & 6 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
[tox]
envlist = py27-django{18,111}-drf{2,36},
py35-django{18,111}-drf{2,36},
py36-django{111,21,22}-drf3
envlist = py36-django{111,21,22}-drf3
py37-django{21,22,30}-drf3
py38-django{21,22,30,31,32}-drf3
py39-django{22,30,31,32,41,42}-drf3
py310-django{32,40,41,42}-drf3

[testenv]
commands = python manage.py test
deps =
six>=1.4.1
django18: Django>=1.8,<1.9
django111: Django>=1.11,<2.0
django21: Django>=2.1,<2.2
django22: Django>=2.2,<3
drf2: djangorestframework>=2,<3
drf36: djangorestframework>=3,<3.7
django30: Django>=3.0,<3.1
django31: Django>=3.1,<3.2
django32: Django>=3.2,<3.3
django41: Django>=4.1,<4.2
django42: Django>=4.2,<4.3
drf3: djangorestframework>=3,<4

0 comments on commit 0436bb0

Please sign in to comment.