Skip to content

Commit f7566ff

Browse files
authored
Fix warnings (#3)
* Show help when no args passed * Fix landscape warnings & errors * Fix lint warnings & add make target for publishing * Possible conflict with variable name
1 parent 606f6ef commit f7566ff

File tree

8 files changed

+108
-67
lines changed

8 files changed

+108
-67
lines changed

.pylintrc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[TYPECHECK]
2+
ignored-classes=Header
3+
4+
[SIMILARITIES]
5+
6+
# Minimum lines number of a similarity.
7+
min-similarity-lines=7
8+
9+
[MISCELLANEOUS]
10+
# List of note tags to take in consideration, separated by a comma.
11+
notes=FIXME,XXX

Makefile

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
1-
.PHONY: init test clean coverage lint
1+
.PHONY: test clean coverage lint distclean release
2+
3+
PYTHON ?= python3
24

35
test:
4-
python3 -m unittest discover -v -b
6+
$(PYTHON) -m unittest discover -v -b
57

68
clean:
7-
rm -rf test/*.pyc
8-
rm -rf zxtools/*.pyc
9-
rm -rf test/__pycache__
10-
rm -rf zxtools/__pycache__
11-
rm -rf zxtools.egg-info
9+
rm -rf dist/ build/ *.egg-info
1210
rm -rf coverage.xml
11+
find . -name '__pycache__' | xargs rm -rf
12+
13+
distclean: clean
1314

1415
coverage:
1516
coverage run --source zxtools setup.py test
16-
coverage report -m
17+
coverage report -m --fail-under=80
1718

1819
lint:
1920
pylint zxtools -f parseable -r n
21+
22+
release: clean lint coverage clean
23+
$(PYTHON) setup.py sdist bdist_wheel upload
24+

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
url='http://www.codeatcpp.com',
3131
license='BSD-3-Clause',
3232
packages=find_packages(exclude=('test', 'docs')),
33+
tests_require=['mock'],
3334
extras_require={
3435
'test': dev_requires,
3536
},

test/test_hobeta.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,36 @@
1717
import tempfile
1818
from collections import namedtuple
1919
from zxtools import hobeta
20+
from zxtools import safe_parse_args
21+
from mock import patch
2022

2123

2224
class TestHobeta(unittest.TestCase):
2325
def test_args_parser(self):
2426
with self.assertRaises(SystemExit):
25-
hobeta.parse_args(("-h", "-v"))
27+
with patch('sys.argv', ["hobeta.py", "-h", "-v"]):
28+
hobeta.main()
2629

2730
with self.assertRaises(SystemExit):
28-
hobeta.parse_args(())
31+
with patch('sys.argv', ["hobeta.py"]):
32+
hobeta.main()
33+
34+
with patch('sys.argv', ["hobeta.py", "hobeta-help"]):
35+
hobeta.main()
36+
37+
args_parser = hobeta.create_parser()
2938

3039
temp_in_file = tempfile.mkstemp()[1]
3140
input_file = open(temp_in_file, "w")
3241
input_file.close()
3342
temp_out_file = tempfile.mkstemp()[1]
3443
try:
35-
args = hobeta.parse_args(("info", temp_in_file))
44+
args = safe_parse_args(args_parser, ["info", temp_in_file])
3645
self.assertEqual(args.func, hobeta.show_info)
3746
args.hobeta_file.close()
3847

39-
args = hobeta.parse_args(("strip", temp_in_file, temp_out_file))
48+
args = safe_parse_args(args_parser,
49+
["strip", temp_in_file, temp_out_file])
4050
self.assertEqual(args.func, hobeta.strip_header)
4151
args.hobeta_file.close()
4252
args.output_file.close()

test/test_zeus2txt.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,36 @@
1616
import unittest
1717
from collections import namedtuple
1818
import logging
19+
from mock import patch
1920

2021
from zxtools import zeus2txt
22+
from zxtools import safe_parse_args
23+
2124

2225

2326
class TestZeus2Txt(unittest.TestCase):
2427
def test_args_parser(self):
28+
args_parser = zeus2txt.create_parser()
29+
2530
with self.assertRaises(SystemExit):
26-
zeus2txt.parse_args(("-h", "-v"))
31+
with patch('sys.argv', ["zeus2txt.py", "-h", "-v"]):
32+
zeus2txt.main()
2733

2834
with self.assertRaises(SystemExit):
29-
zeus2txt.parse_args(())
35+
with patch('sys.argv', ["zeus2txt.py"]):
36+
zeus2txt.main()
3037

3138
temp_in_file = tempfile.mkstemp()[1]
3239
input_file = open(temp_in_file, "w")
3340
input_file.close()
3441
temp_out_file = tempfile.mkstemp()[1]
3542
try:
36-
args = zeus2txt.parse_args(("info", temp_in_file))
43+
args = safe_parse_args(args_parser, ["info", temp_in_file])
3744
self.assertEqual(args.func, zeus2txt.show_info)
3845
args.zeus_file.close()
3946

40-
args = zeus2txt.parse_args(("convert",
41-
temp_in_file, temp_out_file))
47+
args = safe_parse_args(args_parser,
48+
["convert", temp_in_file, temp_out_file])
4249
self.assertEqual(args.func, zeus2txt.convert_file)
4350
args.zeus_file.close()
4451
args.output_file.close()

zxtools/__init__.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,35 @@
77
# Licensed under the BSD 3-Clause license.
88
# See LICENSE file in the project root for full license information.
99
#
10+
"""Global constants and templates are here"""
1011

11-
__version__ = '1.0.22'
12+
import sys
13+
import logging
14+
15+
__version__ = '1.0.23'
1216
CHUNK_SIZE = 512 * 1024 # 512 KBytes
17+
18+
19+
def safe_parse_args(parser, args):
20+
"""Safely parse arguments"""
21+
try:
22+
options = parser.parse_args(args)
23+
if len(args) == 0:
24+
raise ValueError
25+
except ValueError:
26+
parser.print_help()
27+
sys.exit(0)
28+
29+
return options
30+
31+
32+
def default_main(parser):
33+
""" Default entry point implementation """
34+
args = safe_parse_args(parser, sys.argv[1:])
35+
if args.verbose:
36+
logging.basicConfig(level=logging.DEBUG)
37+
38+
if hasattr(args, 'func'):
39+
args.func(args)
40+
41+
return args

zxtools/hobeta.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
""" Hobeta file utils """
1111

1212
import os
13-
import sys
1413
import logging
1514
import struct
1615
from collections import namedtuple
1716
import argparse
1817

1918
from zxtools import CHUNK_SIZE
19+
from zxtools import default_main
2020

2121
HEADER_FMT = '<8sBHHBBH'
2222
Header = namedtuple(
@@ -25,6 +25,7 @@
2525

2626

2727
def hobeta_help(*parsed_args):
28+
"""Shows help"""
2829
print(
2930
"Hobeta file has the following format:\n"
3031
"(this is according to http://speccy.info/Hobeta)\n"
@@ -120,7 +121,7 @@ def strip_header(parsed_args):
120121
return bytes_to_copy-length
121122

122123

123-
def parse_args(args):
124+
def create_parser():
124125
""" Parse command line arguments """
125126
parser = argparse.ArgumentParser(description="Hobeta files converter")
126127
parser.add_argument(
@@ -154,25 +155,12 @@ def parse_args(args):
154155
help="Show Hobeta header format description")
155156
help_parser.set_defaults(func=hobeta_help)
156157

157-
try:
158-
options = parser.parse_args(args)
159-
if len(args) == 0:
160-
raise ValueError
161-
except ValueError:
162-
parser.print_help()
163-
sys.exit(0)
164-
165-
return options
158+
return parser
166159

167160

168161
def main():
169-
""" Entry point """
170-
args = parse_args(sys.argv[1:])
171-
if args.verbose:
172-
logging.basicConfig(level=logging.DEBUG)
173-
174-
if hasattr(args, 'func'):
175-
args.func(args)
162+
"""Entry point"""
163+
return default_main(create_parser())
176164

177165

178166
if __name__ == '__main__':

zxtools/zeus2txt.py

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,28 @@
1111

1212
import argparse
1313
import logging
14-
import sys
1514
import io
1615

1716
from zxtools import CHUNK_SIZE
17+
from zxtools import default_main
1818

1919
CODE_ALIGN_WIDTH = 35
2020

2121

2222
def show_info(*parsed_args):
23+
"""Show some statistic about Zeus file"""
24+
# TODO Implement this function
2325
return parsed_args
2426

2527

2628
def read_file(src_file):
29+
"""Read source file for future processing"""
2730
with src_file:
2831
while True:
2932
chunk = src_file.read(CHUNK_SIZE)
3033
if chunk:
31-
for b in chunk:
32-
yield b
34+
for cur_char in chunk:
35+
yield cur_char
3336
else:
3437
break
3538

@@ -61,10 +64,10 @@ def convert_file(parsed_args):
6164
strnum = 0
6265
cur_buffer = ""
6366
cur_line = io.StringIO()
64-
for b in read_file(parsed_args.zeus_file):
67+
for cur_char in read_file(parsed_args.zeus_file):
6568
if process_string:
66-
cur_buffer += "0x%02X " % b
67-
if not b: # End of string
69+
cur_buffer += "0x%02X " % cur_char
70+
if not cur_char: # End of string
6871
process_string = False
6972
strnum_lo = False, 0
7073
cur_str = cur_line.getvalue()
@@ -76,25 +79,25 @@ def convert_file(parsed_args):
7679
print(file=output)
7780
continue
7881
if tab:
79-
print(" "*b, end="", file=cur_line)
82+
print(" "*cur_char, end="", file=cur_line)
8083
tab = False
8184
continue
82-
if b == 0x0A:
85+
if cur_char == 0x0A:
8386
tab = True
8487
continue
85-
if b < ASM_FIRST_TOKEN: # Printable character
86-
print(chr(b), end="", file=cur_line)
88+
if cur_char < ASM_FIRST_TOKEN: # Printable character
89+
print(chr(cur_char), end="", file=cur_line)
8790
continue
8891
try:
89-
print(ASM_META[b-ASM_FIRST_TOKEN], end="", file=cur_line)
92+
print(ASM_META[cur_char-ASM_FIRST_TOKEN], end="", file=cur_line)
9093
except IndexError:
9194
logger.warning("Token not defined: 0x%02X (%d), at line %05d. "
92-
"Skipped.", b, b, strnum)
95+
"Skipped.", cur_char, cur_char, strnum)
9396
else:
9497
if not strnum_lo[0]:
95-
strnum_lo = True, b
98+
strnum_lo = True, cur_char
9699
else:
97-
strnum = strnum_lo[1] + b*256
100+
strnum = strnum_lo[1] + cur_char*256
98101
if strnum == 0xFFFF: # End of file
99102
print(file=output)
100103
break
@@ -106,7 +109,7 @@ def convert_file(parsed_args):
106109
output.close()
107110

108111

109-
def parse_args(args):
112+
def create_parser():
110113
""" Parse command line arguments """
111114
parser = argparse.ArgumentParser(
112115
description="Zeus Z80 assembler files converter")
@@ -137,25 +140,12 @@ def parse_args(args):
137140
action='store_true', help="Include original code in the output file")
138141
convert_parser.set_defaults(func=convert_file)
139142

140-
try:
141-
options = parser.parse_args(args)
142-
if len(args) == 0:
143-
raise ValueError
144-
except ValueError:
145-
parser.print_help()
146-
sys.exit(0)
147-
148-
return options
143+
return parser
149144

150145

151146
def main():
152-
""" Entry point """
153-
args = parse_args(sys.argv[1:])
154-
if args.verbose:
155-
logging.basicConfig(level=logging.DEBUG)
156-
157-
if hasattr(args, 'func'):
158-
args.func(args)
147+
"""Entry point"""
148+
default_main(create_parser())
159149

160150

161151
if __name__ == '__main__':

0 commit comments

Comments
 (0)