Skip to content

Commit e0cbae1

Browse files
committed
include-code parameter in zeus2txt
1 parent 6a7624b commit e0cbae1

File tree

8 files changed

+68
-46
lines changed

8 files changed

+68
-46
lines changed

README.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
============================================
1+
=====================================
22
Tools to manipulate ZX Spectrum files
3-
============================================
3+
=====================================
44

55
.. image:: https://travis-ci.org/codeatcpp/zxtools.svg?branch=master
66
:target: https://travis-ci.org/codeatcpp/zxtools
@@ -20,7 +20,7 @@ TODO: I have future plans to implement some more tools I need to restore my old
2020

2121
But you can use them in the way you need. And it's very easy to use: download the package, run ``setup.py`` (or install via ``pip install zxtools``), invoke in the following way::
2222

23-
$ python3 -m zxtools.hobeta strip input.hobetta result.zeus
23+
$ python3 -m zxtools.hobeta strip input.hobeta result.zeus
2424
$ python3 -m zxtools.zeus2txt result.zeus listing.txt
2525

2626
NOTE: Python 3 is required to use this package, and Python 2 is not supported but you are welcome to fix it.

test/test_hobeta.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
# Licensed under the BSD 3-Clause license.
99
# See LICENSE file in the project root for full license information.
1010
#
11-
""" Convert Zeus Z80 assembler file to a plain text """
12-
1311
""" hobeta.py tests """
1412

1513
import os
@@ -24,26 +22,26 @@
2422
class TestHobeta(unittest.TestCase):
2523
def test_args_parser(self):
2624
with self.assertRaises(SystemExit):
27-
args = hobeta.parse_args(("-h", "-v"))
25+
hobeta.parse_args(("-h", "-v"))
2826

2927
temp_in_file = tempfile.mkstemp()[1]
3028
input_file = open(temp_in_file, "w")
3129
input_file.close()
3230
temp_out_file = tempfile.mkstemp()[1]
3331
try:
34-
args = hobeta.parse_args(("info", temp_in_file))
35-
self.assertEqual(args.func, hobeta.show_info)
36-
args.hobeta_file.close()
37-
38-
args = hobeta.parse_args(("strip", temp_in_file, temp_out_file))
39-
self.assertEqual(args.func, hobeta.strip_header)
40-
args.hobeta_file.close()
41-
args.output_file.close()
32+
args = hobeta.parse_args(("info", temp_in_file))
33+
self.assertEqual(args.func, hobeta.show_info)
34+
args.hobeta_file.close()
35+
36+
args = hobeta.parse_args(("strip", temp_in_file, temp_out_file))
37+
self.assertEqual(args.func, hobeta.strip_header)
38+
args.hobeta_file.close()
39+
args.output_file.close()
4240
finally:
4341
os.remove(temp_in_file)
4442
os.remove(temp_out_file)
4543

46-
def test_ckechsum(self):
44+
def test_checksum(self):
4745
data = b'F.load.AC\x00\x80\xf9\x06\x00\x07'
4846
self.assertEqual(hobeta.calc_checksum(data), 20661)
4947

@@ -101,8 +99,8 @@ def test_parse_info2(self):
10199
def strip_header(test_input_file, ignore_header_info):
102100
temp_output_path = tempfile.mkstemp()[1]
103101
temp_output_file = open(temp_output_path, "wb")
104-
Args = namedtuple('Args', "hobeta_file output_file ignore_header")
105-
parsed_args = Args(test_input_file,
102+
args = namedtuple('Args', "hobeta_file output_file ignore_header")
103+
parsed_args = args(test_input_file,
106104
temp_output_file,
107105
ignore_header_info)
108106
copied_bytes = hobeta.strip_header(parsed_args)
@@ -160,10 +158,6 @@ def test_strip_header3(self):
160158
temp_output_file.close()
161159
os.remove(temp_output_path)
162160

163-
def test_args_parser(self):
164-
parser = hobeta.parse_args(['hobeta-help'])
165-
self.assertTrue(parser.func)
166-
167161

168162
if __name__ == '__main__':
169163
unittest.main()

test/test_trdos.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
# Licensed under the BSD 3-Clause license.
99
# See LICENSE file in the project root for full license information.
1010
#
11-
""" Convert Zeus Z80 assembler file to a plain text """
12-
1311
""" trdos.py tests """
1412

1513
import unittest

test/test_zeus2txt.py

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
# Licensed under the BSD 3-Clause license.
99
# See LICENSE file in the project root for full license information.
1010
#
11-
""" Convert Zeus Z80 assembler file to a plain text """
12-
1311
""" zeus2txt.py tests """
1412

1513
import io
@@ -25,33 +23,34 @@
2523
class TestZeus2Txt(unittest.TestCase):
2624
def test_args_parser(self):
2725
with self.assertRaises(SystemExit):
28-
args = zeus2txt.parse_args(("-h", "-v"))
26+
zeus2txt.parse_args(("-h", "-v"))
2927

3028
temp_in_file = tempfile.mkstemp()[1]
3129
input_file = open(temp_in_file, "w")
3230
input_file.close()
3331
temp_out_file = tempfile.mkstemp()[1]
3432
try:
35-
args = zeus2txt.parse_args(("info", temp_in_file))
36-
self.assertEqual(args.func, zeus2txt.show_info)
37-
args.zeus_file.close()
33+
args = zeus2txt.parse_args(("info", temp_in_file))
34+
self.assertEqual(args.func, zeus2txt.show_info)
35+
args.zeus_file.close()
3836

39-
args = zeus2txt.parse_args(("convert", temp_in_file, temp_out_file))
40-
self.assertEqual(args.func, zeus2txt.convert_file)
41-
args.zeus_file.close()
42-
args.output_file.close()
37+
args = zeus2txt.parse_args(("convert",
38+
temp_in_file, temp_out_file))
39+
self.assertEqual(args.func, zeus2txt.convert_file)
40+
args.zeus_file.close()
41+
args.output_file.close()
4342
finally:
4443
os.remove(temp_in_file)
4544
os.remove(temp_out_file)
4645

4746
@staticmethod
48-
def prepare_convert_args(test_data):
47+
def prepare_convert_args(test_data, include_code=False):
4948
test_file = io.BytesIO(test_data)
5049
temp_output_path = tempfile.mkstemp()[1]
5150
temp_output_file = open(temp_output_path, "w")
5251

53-
Args = namedtuple('Args', "zeus_file output_file")
54-
parsed_args = Args(test_file, temp_output_file)
52+
args = namedtuple('Args', "zeus_file output_file include_code")
53+
parsed_args = args(test_file, temp_output_file, include_code)
5554
return parsed_args, temp_output_path, temp_output_file
5655

5756
def test_undefined_token(self):
@@ -64,7 +63,7 @@ def test_undefined_token(self):
6463
temp_output_file.close()
6564
temp_output_file = open(temp_output_path, "r")
6665
lines = temp_output_file.read().splitlines()
67-
self.assertEqual(lines, ["00010 ,42",""])
66+
self.assertEqual(lines, ["00010 ,42", ""])
6867
finally:
6968
temp_output_file.close()
7069
os.remove(temp_output_path)
@@ -78,7 +77,24 @@ def test_no_eof(self):
7877
temp_output_file.close()
7978
temp_output_file = open(temp_output_path, "r")
8079
lines = temp_output_file.read().splitlines()
81-
self.assertEqual(lines, ["00010 ADD BC,42",])
80+
self.assertEqual(lines, ["00010 ADD BC,42", ])
81+
finally:
82+
temp_output_file.close()
83+
os.remove(temp_output_path)
84+
85+
def test_include_code(self):
86+
args, temp_output_path, temp_output_file = self.prepare_convert_args(
87+
b"\x0A\x00\x0A\x06\x82\x87\x2C\x34\x32\x00", True)
88+
89+
try:
90+
zeus2txt.convert_file(args)
91+
temp_output_file.close()
92+
temp_output_file = open(temp_output_path, "r")
93+
lines = temp_output_file.read().splitlines()
94+
self.assertEqual(lines, ["00010 ADD BC,42"
95+
" ; 0x000A "
96+
"0x0A 0x06 0x82 0x87 "
97+
"0x2C 0x34 0x32 0x00 ", ])
8298
finally:
8399
temp_output_file.close()
84100
os.remove(temp_output_path)
@@ -423,5 +439,6 @@ def setUp(self):
423439
b"\x20\x20\x4E\x4F\x50\x0A"
424440
)
425441

442+
426443
if __name__ == '__main__':
427444
unittest.main()

zxtools/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
# Licensed under the BSD 3-Clause license.
99
# See LICENSE file in the project root for full license information.
1010
#
11-
""" Convert Zeus Z80 assembler file to a plain text """
1211

1312
CHUNK_SIZE = 512 * 1024 # 512 KBytes
1413

zxtools/hobeta.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
def hobeta_help(*parsed_args):
2929
print(
3030
"Hobeta file has the following format:\n"
31-
"(this is accroding to http://speccy.info/Hobeta)\n"
31+
"(this is according to http://speccy.info/Hobeta)\n"
3232
"\n"
3333
"0 1 2 3\n"
3434
"0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1\n"

zxtools/trdos.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from collections import namedtuple
1414

15-
# TR-DOS diskette stucture description
15+
# TR-DOS diskette structure description
1616
#
1717
# 0 track contains FAT in sectors 0..7
1818
###############################################################################

zxtools/zeus2txt.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
from zxtools import CHUNK_SIZE
1919

20+
CODE_ALIGN_WIDTH = 35
21+
2022

2123
def show_info(*parsed_args):
2224
return
@@ -32,6 +34,7 @@ def read_file(src_file):
3234
else:
3335
break
3436

37+
3538
ASM_FIRST_TOKEN = 128
3639
ASM_META = [
3740
"A", "ADC ", "ADD ", "AF'", "AF", "AND ", "B", "BC", "BIT ", "C",
@@ -48,7 +51,8 @@ def read_file(src_file):
4851

4952

5053
def convert_file(parsed_args):
51-
""" Convert Zeus Z80 assembler file specified in zeus_file to the plain text and print it to the output_file """
54+
""" Convert Zeus Z80 assembler file specified in zeus_file to the plain
55+
text and print it to the output_file """
5256
logger = logging.getLogger('convert_file')
5357

5458
process_string = False
@@ -58,11 +62,17 @@ def convert_file(parsed_args):
5862
strnum = 0
5963
for b in read_file(parsed_args.zeus_file):
6064
if process_string:
65+
cur_buffer += "0x%02X " % b
6166
if not b: # End of string
6267
process_string = False
6368
strnum_lo = False, 0
64-
print(file=cur_line)
65-
print(cur_line.getvalue(), file=output)
69+
cur_str = cur_line.getvalue()
70+
print(cur_str, end="", file=output)
71+
if parsed_args.include_code:
72+
print(" "*(CODE_ALIGN_WIDTH-len(cur_str))+";",
73+
"0x%04X " % strnum + cur_buffer, file=output)
74+
else:
75+
print(file=output)
6676
continue
6777
if tab:
6878
print(" "*b, end="", file=cur_line)
@@ -77,8 +87,8 @@ def convert_file(parsed_args):
7787
try:
7888
print(ASM_META[b-ASM_FIRST_TOKEN], end="", file=cur_line)
7989
except IndexError:
80-
logger.warning("Token not defined: 0x%02X (%d), at line %05d"
81-
% (b, b, strnum))
90+
logger.warning("Token not defined: 0x%02X (%d), at line %05d. "
91+
"Skipped." % (b, b, strnum))
8292
else:
8393
if not strnum_lo[0]:
8494
strnum_lo = True, b
@@ -89,6 +99,7 @@ def convert_file(parsed_args):
8999
break
90100
cur_line = io.StringIO()
91101
cur_line.truncate(0)
102+
cur_buffer = ""
92103
print("%05d" % strnum, end=" ", file=cur_line)
93104
process_string = True
94105
output.close()
@@ -120,6 +131,9 @@ def parse_args(args):
120131
convert_parser.add_argument(
121132
'output_file', metavar='output-file',
122133
type=argparse.FileType('w'), help="Path to the output file")
134+
convert_parser.add_argument(
135+
'--include-code', dest='include_code',
136+
action='store_true', help="Include original code in the output file")
123137
convert_parser.set_defaults(func=convert_file)
124138

125139
return parser.parse_args(args)

0 commit comments

Comments
 (0)