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

Inital support for py3 #14

Merged
merged 9 commits into from
Feb 23, 2023
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
36 changes: 36 additions & 0 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: BANKBARCODE_CI

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

jobs:
build:

runs-on: ubuntu-latest
strategy:
# You can use PyPy versions in python-version.
# For example, pypy2 and pypy3
matrix:
python-version: ["2.7", "3.6"]

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
if [ -f requirements-dev.txt ]; then pip install -r requirements-dev.txt; fi
pip install -e .
- name: Run test
run: |
mamba --enable-coverage
12 changes: 0 additions & 12 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion bankbarcode/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
try:
__version__ = __import__('pkg_resources') \
.get_distribution(__name__).version
except Exception, e:
except Exception as e:
__version__ = 'unknown'
11 changes: 7 additions & 4 deletions bankbarcode/bankbarcode.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from barcode import generate
from StringIO import StringIO
import six
from six.moves import StringIO
from six import BytesIO
from six import string_types


class BankBarcode(object):
Expand Down Expand Up @@ -30,7 +33,7 @@ def _check_length(self, name, value, expected_length, description):
return True

def _strip_dotsvg(self, path):
if isinstance(path, basestring) and path[-4:] == '.svg':
if isinstance(path, string_types) and path[-4:] == '.svg':
new_path = path[:-4]
else:
new_path = path
Expand Down Expand Up @@ -74,6 +77,6 @@ def svg(self, writer_options=None):
http://pythonhosted.org/pyBarcode/writers/index.html?#common-options
:return: a string with the barcode in SVG format
"""
f = StringIO()
f = BytesIO()
self.save(f, writer_options)
return f.getvalue()
return f.getvalue().decode('utf-8')
22 changes: 12 additions & 10 deletions bankbarcode/cuaderno57.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# coding=utf-8
from __future__ import absolute_import, unicode_literals
from datetime import datetime

from bankbarcode import BankBarcode
from .bankbarcode import BankBarcode
from decimal import Decimal
from six import text_type, string_types


class Recibo(BankBarcode):
Expand Down Expand Up @@ -72,7 +74,7 @@ def _check_amount(self, amount):
raise ValueError('{} have more than 2 decimals'.format(name))
if decimal > 99999999.99:
raise ValueError('{} is too big'.format(name))
value = unicode(int(decimal * 100)).zfill(10)
value = text_type(int(decimal * 100)).zfill(10)
expected_length = 10
description = 'amount lenth should be 10'
return self._check_length(name, value, expected_length, description)
Expand All @@ -88,7 +90,7 @@ def _check_due_date(self, due_date, suffix):
name = 'due_date'
if due_date is None:
return True
if isinstance(due_date, basestring):
if isinstance(due_date, string_types):
try:
date = datetime.strptime(due_date, '%Y-%m-%d')
except:
Expand Down Expand Up @@ -225,7 +227,7 @@ def amount(self, value):

:param value: the amount (Importe)
"""
unicode_value = unicode(value)
unicode_value = text_type(value)
if self._check_amount(unicode_value):
self._amount = unicode_value

Expand All @@ -244,7 +246,7 @@ def due_date(self, due_date):
self._due_date = None
else:
if self._check_due_date(due_date, self.suffix):
if isinstance(due_date, basestring):
if isinstance(due_date, string_types):
due_date = datetime.strptime(due_date, '%Y-%m-%d')
self._due_date = due_date
self._notice = self._due_date.strftime('%d%m%y')
Expand Down Expand Up @@ -275,19 +277,19 @@ def checksum(self):
decimals = int(Decimal(sum) / 97 % 1 * 100)
if not decimals:
return '00'
return unicode(100 - decimals).zfill(2)
return text_type(100 - decimals).zfill(2)

def code(self):
"""
Generate the code for the barcode

:return: an unicode string with the code for the barcode
"""
id_application = u'90'
format_type = u'507'
parity = u'0'
id_application = '90'
format_type = '507'
parity = '0'

amount100 = unicode(self.amount100()).zfill(10)
amount100 = text_type(self.amount100()).zfill(10)

code = (
'{id_application}'
Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
mamba
mamba<=0.10.0
expects
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# python-barcode
# We need python-barcode v0.8, to have Code128 (EAN128), not released yet
# https://bitbucket.org/whitie/python-barcode/issues/16/pypi-08-release-request
https://bitbucket.org/whitie/python-barcode/get/6c22b96a2ca2.zip
# https://bitbucket.org/whitie/python-barcode/get/6c22b96a2ca2.zip 0.8.0 fro py2 compat check
python-barcode==0.8.0
7 changes: 2 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@
author_email='devel@gisce.net',
# We need python-barcode v0.8, to have Code128 (EAN128), not released yet
# https://bitbucket.org/whitie/python-barcode/issues/16/pypi-08-release-request
dependency_links=[
"https://bitbucket.org/whitie/python-barcode/get/6c22b96.zip"
"#egg=pybarcode-0.8b1"
],
install_requires=[
'pybarcode>=0.8b1'
'python-barcode==0.8.0',
'six'
],
description='barcodes for financial documents'
)
6 changes: 3 additions & 3 deletions spec/cuaderno57_Recibo507_due_date_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def callback():
ref = '12345678901'
notice = '123456'
amount = '6543.21'
due_date = datetime(2015, 11, 01)
due_date = datetime(2015, 11, 0o1)
recibo = Recibo507(entity, suffix, ref, notice, amount, due_date)
expect(callback).to(
raise_error(ValueError,
Expand All @@ -23,7 +23,7 @@ def callback():
ref = '00000000015'
notice = '300815'
amount = '53.98'
due_date = datetime(2015, 11, 01)
due_date = datetime(2015, 11, 0o1)
recibo = Recibo507(entity, suffix, ref, notice, amount, due_date)
expect(recibo.notice).not_to(equal(notice))
expect(recibo.notice).to(equal(due_date.strftime('%d%m%y')))
Expand All @@ -35,7 +35,7 @@ def callback():
notice = '300815'
amount = '53.98'
checksum = '27'
due_date = datetime(2015, 11, 01)
due_date = datetime(2015, 11, 0o1)
recibo = Recibo507(entity, suffix, ref, notice, amount, due_date)
expect(recibo.notice).to(equal(due_date.strftime('%d%m%y')))
expect(recibo.checksum()).to(equal(checksum))
Expand Down
41 changes: 22 additions & 19 deletions spec/cuaderno57_Recibo507_input_spec.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from sys import maxint
from sys import maxsize
from expects import expect, be_true, raise_error
from random import randint, uniform, random
from bankbarcode.cuaderno57 import Recibo507
from six import text_type
import sys
maxint = sys.maxsize

with description('Check input values for Recibo507'):

Expand All @@ -17,11 +20,11 @@
with context('Check entity'):

with it('return True if length of value is 8, a NIF/CIF, without the letter'):
value = unicode(randint(0, 99999999)).zfill(8)
value = text_type(randint(0, 99999999)).zfill(8)
expect(self.recibo507._check_entity(value)).to(be_true)

with it('raise a value error if length of value is less than 8'):
short_value = unicode(randint(0, 9999999))
short_value = text_type(randint(0, 9999999))
short_error = 'entity is too short, entity should be a NIF/CIF, without the letter'

def callback():
Expand All @@ -30,7 +33,7 @@ def callback():
expect(callback).to(raise_error(ValueError, short_error))

with it('raise a value error if length of value is greater than 8'):
long_value = unicode(randint(100000000, maxint))
long_value = text_type(randint(100000000, maxint))
long_error = 'entity is too long, entity should be a NIF/CIF, without the letter'

def callback():
Expand All @@ -41,19 +44,19 @@ def callback():
with context('Check suffix'):

with it('return True if the length of value is 3'):
value = unicode(randint(0, 999)).zfill(3)
value = text_type(randint(0, 999)).zfill(3)
expect(self.recibo507._check_suffix(value)).to(be_true)

with it('raise a value error if length of value is less than 3'):
short_value = unicode(randint(0, 99))
short_value = text_type(randint(0, 99))

def callback():
self.recibo507._check_suffix(short_value)

expect(callback).to(raise_error(ValueError, 'suffix is too short, suffix lenth should be 3'))

with it('raise a value error if length of value is greater than 3'):
long_value = unicode(randint(1000, maxint))
long_value = text_type(randint(1000, maxint))

def callback():
self.recibo507._check_suffix(long_value)
Expand All @@ -63,19 +66,19 @@ def callback():
with context('Check reference'):

with it('return True if the length of value is 11'):
value = unicode(randint(0, 9999999999)).zfill(11)
value = text_type(randint(0, 9999999999)).zfill(11)
expect(self.recibo507._check_ref(value)).to(be_true)

with it('raise a value error if length of value is less than 11'):
short_value = unicode(randint(0, 9999999999))
short_value = text_type(randint(0, 9999999999))

def callback():
self.recibo507._check_ref(short_value)

expect(callback).to(raise_error(ValueError, 'ref is too short, ref lenth should be 11'))

with it('raise a value error if length of value is greater than 11'):
long_value = unicode(randint(100000000000, maxint))
long_value = text_type(randint(100000000000, maxint))

def callback():
self.recibo507._check_ref(long_value)
Expand All @@ -85,19 +88,19 @@ def callback():
with context('Check identification'):

with it('return True if the length of value is 6'):
value = unicode(randint(0, 999999)).zfill(6)
value = text_type(randint(0, 999999)).zfill(6)
expect(self.recibo507._check_notice(value)).to(be_true)

with it('raise a value error if length of value is less than 6'):
short_value = unicode(randint(0, 99999))
short_value = text_type(randint(0, 99999))

def callback():
self.recibo507._check_notice(short_value)

expect(callback).to(raise_error(ValueError, 'notice is too short, notice lenth should be 6'))

with it('raise a value error if length of value is greater than 6'):
long_value = unicode(randint(1000000, maxint))
long_value = text_type(randint(1000000, maxint))

def callback():
self.recibo507._check_notice(long_value)
Expand All @@ -107,23 +110,23 @@ def callback():
with context('Check amount'):

with it('return True if amount is a unicode with less than 100000000 with 2 decimals'):
value = unicode(round(uniform(0, 99999999.99), 2))
value = text_type(round(uniform(0, 99999999.99), 2))
expect(self.recibo507._check_amount(value)).to(be_true)

with it('return True is amount is a int less than 100000000'):
value = randint(0, 99999999)
expect(self.recibo507._check_amount(value)).to(be_true)

with it('raise a value error if amount isn\'t less than 100000000'):
big_value = unicode(round(uniform(100000000, maxint), 2))
big_value = text_type(round(uniform(100000000, maxint), 2))

def callback():
self.recibo507._check_amount(big_value)

expect(callback).to(raise_error(ValueError, 'amount is too big'))

with it('raise a value error if amount have more than 2 decimals'):
float_value = unicode(round(random(), randint(3, 10)))
float_value = text_type(round(random(), randint(3, 10)))

def callback():
self.recibo507._check_amount(float_value)
Expand All @@ -133,23 +136,23 @@ def callback():
with context('Check amount'):

with it('return True if amount is a unicode with less than 100000000 with 2 decimals'):
value = unicode(round(uniform(0, 99999999.99), 2))
value = text_type(round(uniform(0, 99999999.99), 2))
expect(self.recibo507._check_amount(value)).to(be_true)

with it('return True is amount is a int less than 100000000'):
value = randint(0, 99999999)
expect(self.recibo507._check_amount(value)).to(be_true)

with it('raise a value error if amount isn\'t less than 100000000'):
big_value = unicode(round(uniform(100000000, maxint), 2))
big_value = text_type(round(uniform(100000000, maxint), 2))

def callback():
self.recibo507._check_amount(big_value)

expect(callback).to(raise_error(ValueError, 'amount is too big'))

with it('raise a value error if amount have more than 2 decimals'):
float_value = unicode(round(random(), randint(3, 10)))
float_value = text_type(round(random(), randint(3, 10)))

def callback():
self.recibo507._check_amount(float_value)
Expand Down
Loading