Skip to content

Commit

Permalink
Merge pull request #47 from gisce/py3_compat_restore
Browse files Browse the repository at this point in the history
Restore PY3 compat
  • Loading branch information
polsala authored Oct 4, 2023
2 parents ab6cf1d + 80a3c01 commit b2883c5
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 21 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: Python Testing

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

jobs:
run-tests:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: [ "2.7", "3.11" ]

steps:
- uses: actions/checkout@v3
- name: Set up Python 3
if: matrix.python-version == '3.11'
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Install Python 2
if: matrix.python-version == '2.7'
run: |
sudo apt update
sudo apt install python2 python-pip
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 2
printf '1\n' | sudo update-alternatives --config python
cd /usr/bin
sudo ln -s /usr/bin/pip2 ./pip
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements-dev.txt
pip install -e .
- name: Tests
run: |
mamba
6 changes: 4 additions & 2 deletions qreu/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,9 @@ def add_attachment(self, input_buff, attname=False):
import base64

content = input_buff.getvalue() if isinstance(input_buff, (StringIO, BytesIO)) else input_buff.read()
attachment_str = base64.encodestring(content)
# attachment_str = base64.encodestring(content)
attachment_str = content.decode('utf-8')


filetype = mimetypes.guess_type(filename)[0]
subtype = 'octet-stream'
Expand All @@ -384,7 +386,7 @@ def add_attachment(self, input_buff, attname=False):
attachment.set_charset('utf-8')
attachment.add_header(
'Content-Disposition',
'attachment; filename="%s"' % self.remove_accent(basename(filename))
'attachment; filename="%s"' % self.remove_accent(u'{}'.format(basename(filename)))
)
attachment.add_header('Content-Transfer-Encoding', 'base64')
attachment.set_payload(
Expand Down
5 changes: 3 additions & 2 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mamba==0.9.3
mamba==0.9.3;python_version<="2.7.18"
mamba;python_version>"2.7.18"
expects
mock
mock
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
install_requires=[
'html2text',
'six',
'unidecode'
],
license='MIT',
author='GISCE-TI, S.L.',
Expand Down
34 changes: 17 additions & 17 deletions spec/qreu_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,14 @@ def call_wrongly_html():
filename = attachment['name']
filecontent = attachment['content']
with open(f_path, 'rb') as f:
attachment_str = base64.encodestring(f.read())
try:
attachment_str = str(attachment_str, 'utf-8')
except TypeError:
# Python 2.7 compat
attachment_str = unicode(attachment_str)
# attachment_str = base64.encodestring(f.read())
attachment_str = f.read().decode('utf-8')
# try:
# attachment_str = str(attachment_str, 'utf-8')
# except TypeError as e:
# print(e)
# # Python 2.7 compat
# attachment_str = unicode(attachment_str)
expect(filecontent).to(equal(attachment_str))

with it('must add an iostring as attachment to body'):
Expand All @@ -306,17 +308,15 @@ def call_wrongly_html():
f_name = '0.txt'
with open(f_path, 'rb') as f:
f_data = f.read()
try:
input_iostr = BytesIO(f_data)
except TypeError:
# Python 2.7 compat
input_iostr = StringIO(unicode(f_data))
check_str = base64.encodestring(f_data)
try:
check_str = str(check_str, 'utf-8')
except TypeError:
# Python 2.7 compat
check_str = unicode(check_str)

input_iostr = BytesIO(f_data)
check_str = f_data.decode('utf-8')
# check_str = base64.encodestring(f_data)
# try:
# check_str = str(check_str, 'utf-8')
# except TypeError:
# # Python 2.7 compat
# check_str = unicode(check_str)
e.add_attachment(input_buff=input_iostr, attname=f_name)
for attachment in e.attachments:
filename = attachment['name']
Expand Down

0 comments on commit b2883c5

Please sign in to comment.