Skip to content

Commit

Permalink
start cas2bas
Browse files Browse the repository at this point in the history
  • Loading branch information
jedie committed Sep 5, 2013
1 parent 6673972 commit aca7379
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 14 deletions.
77 changes: 64 additions & 13 deletions PyDC/PyDC/CassetteObjects.py
Expand Up @@ -419,6 +419,55 @@ def add_from_bas(self, filename):
self.current_file.create_from_bas(filename, file_content)
self.files.append(self.current_file)

def add_from_cas(self, source_filepath):
log.debug("Open %s..." % repr(source_filepath))

class CasStream(object):
def __init__(self, source_filepath):
self.source_filepath = source_filepath
self.f = open(source_filepath, "rb")
self.pos = 0

def __iter__(self):
return self
def next(self):
self.last_byte = self.f.read(1)
if self.last_byte == "":
raise StopIteration
self.pos += 1
self.last_codepoint = ord(self.last_byte)
return (self.pos, self.last_codepoint)

cas_stream = CasStream(source_filepath)

leadin_count = None
# for i in iter(cas_stream.next, self.cfg.LEAD_BYTE_CODEPOINT):
# print i
# break

log.debug("Count leading byte.")
# for leadin_count, leadin_byte in iter(cas_stream.next, self.cfg.LEAD_BYTE_CODEPOINT):
for leadin_count, leadin_byte in cas_stream:
if leadin_byte != self.cfg.LEAD_BYTE_CODEPOINT:
break

if leadin_count is None:
log.error("Leadin byte not found in file!")
sys.exit(-1)
log.info("%s x leadin bytes found." % leadin_count)

# sync_byte = cas_stream.next()
sync_byte = cas_stream.last_codepoint
if sync_byte != self.cfg.SYNC_BYTE_CODEPOINT:
log.error("Sync byte wrong. Get %s but excepted %s" % (
repr(sync_byte), hex(self.cfg.SYNC_BYTE_CODEPOINT)
))
else:
log.debug("Sync byte, ok.")

raise TODO


def add_block(self, block_type, block_length, block_codepoints):
if block_type == self.cfg.EOF_BLOCK:
return
Expand Down Expand Up @@ -579,24 +628,26 @@ def pprint_codepoint_stream(self):

import subprocess

# bas -> wav
subprocess.Popen([sys.executable, "../PyDC_cli.py",
"--verbosity=10",
# "--verbosity=5",
# "--logfile=5",
# "--log_format=%(module)s %(lineno)d: %(message)s",
"../test_files/HelloWorld1.bas", "--dst=../test.wav"
]).wait()

print "\n"*3
print "="*79
print "\n"*3
# # bas -> wav
# subprocess.Popen([sys.executable, "../PyDC_cli.py",
# "--verbosity=10",
# # "--verbosity=5",
# # "--logfile=5",
# # "--log_format=%(module)s %(lineno)d: %(message)s",
# # "../test_files/HelloWorld1.bas", "--dst=../test.wav"
# "../test_files/HelloWorld1.bas", "--dst=../test.cas"
# ]).wait()
#
# print "\n"*3
# print "="*79
# print "\n"*3

# # wav -> bas
subprocess.Popen([sys.executable, "../PyDC_cli.py",
# "--verbosity=10",
"--verbosity=7",
"../test.wav", "--dst=../test.bas",
# "../test.wav", "--dst=../test.bas",
"../test.cas", "--dst=../test.bas",
# "../test_files/HelloWorld1 origin.wav", "--dst=../test_files/HelloWorld1.bas",
]).wait()

Expand Down
10 changes: 10 additions & 0 deletions PyDC/PyDC/__init__.py
Expand Up @@ -35,6 +35,16 @@ def bas2cas(source_filepath, destination_filepath, cfg):
c.write_cas(destination_filepath)


def cas2bas(source_filepath, destination_filepath, cfg):
"""
Read .cas file and create a .bas file
"""
c = Cassette(cfg)
c.add_from_cas(source_filepath)
c.print_debug_info()
c.save_bas(destination_filepath)


def bas2wav(source_filepath, destination_filepath, cfg):
"""
Create a wave file from a existing .bas file
Expand Down
6 changes: 5 additions & 1 deletion PyDC/PyDC_cli.py
Expand Up @@ -13,7 +13,8 @@
import os
import sys

from PyDC import TITLE_LINE, VERSION_STRING, wav2bas, bas2wav, analyze, bas2cas
from PyDC import TITLE_LINE, VERSION_STRING, wav2bas, bas2wav, analyze, bas2cas, \
cas2bas
from PyDC.base_cli import Base_CLI
from PyDC.configs import Dragon32Config

Expand Down Expand Up @@ -124,6 +125,9 @@ def run(self):

elif source_ext.startswith(".bas") and dest_ext.startswith(".cas"):
bas2cas(self.source_file, self.destination_file, self.cfg)
elif source_ext.startswith(".cas") and dest_ext.startswith(".bas"):
cas2bas(self.source_file, self.destination_file, self.cfg)

else:
print "ERROR:"
print "%s to %s ???" % (repr(self.source_file), repr(self.destination_file))
Expand Down

0 comments on commit aca7379

Please sign in to comment.