-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_ascii.py
100 lines (71 loc) · 2.78 KB
/
read_ascii.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""
DwLoadServer - A DWLOAD server written in Python
================================================
:created: 2014 by Jens Diemer - www.jensdiemer.de
:copyleft: 2014 by the DwLoadServer team, see AUTHORS for more details.
:license: GNU GPL v3 or above, see LICENSE for more details.
"""
import logging
import os
from dwload_server.utils.file_tools import backup_rename, padding, fnmatch_case_insensitve
from dragonlib.api import Dragon32API
from dwload_server import constants
from dwload_server.utils import hook_handler
from dragonlib.utils.logging_utils import log_hexlines
log = logging.getLogger(__name__)
def change_filepath(server, new_filepath):
# FIXME: Hackish!
old_filepath = server.filepath
log.info("Change filename from %r to %r", old_filepath, new_filepath)
server.filepath = new_filepath
@hook_handler.register_pre_hook(constants.OP_READ_EXTENDED)
def read_ascii_read_pre_hook(server, filepath, lsn):
if not fnmatch_case_insensitve(filepath, "*.BAS"):
log.info("Don't convert to Dragon DOS Binary: No .BAS file, ok")
return
dwl_filepath = filepath + ".dwl"
if lsn != 0:
if os.path.isfile(dwl_filepath):
change_filepath(server, dwl_filepath)
return
# We read the first sector of the last requested filepath
log.info("Read ASCII listing from %r...", filepath)
with open(filepath) as f:
basic_program_ascii = f.read()
api = Dragon32API()
data = api.bas2bin(basic_program_ascii,
# FIXME:
load_address=0x1e01, exec_address=0x1e01 # from example AUTOLOAD.DWL
)
log.debug("size before padding: %i Bytes", len(data))
log_hexlines(data, msg="before padding")
data = padding(data, size=256, b=b"\x00")
log_hexlines(data, msg="after padding")
log.debug("size after padding: %i Bytes", len(data))
backup_rename(dwl_filepath)
with open(dwl_filepath, "wb") as f:
f.write(data)
change_filepath(server, dwl_filepath)
if __name__ == "__main__":
from dragonlib.utils.logging_utils import setup_logging
setup_logging(
# level=1 # hardcore debug ;)
level=10 # DEBUG
# level=20 # INFO
# level=30 # WARNING
# level=40 # ERROR
# level=50 # CRITICAL/FATAL
)
filepath = os.path.expanduser("~/dwload-files/FOO.BAS")
with open(filepath) as f:
basic_program_ascii = f.read()
api = Dragon32API()
data = api.bas2bin(basic_program_ascii,
# FIXME:
load_address=0x1e01, exec_address=0x1e01 # from example
)
log.debug("size before padding: %i Bytes", len(data))
data = padding(data, size=256, b=b"\x00")
log.debug("size after padding: %i Bytes", len(data))
log.debug(repr(data))
print(" --- END --- ")