Skip to content

Commit

Permalink
black'ed
Browse files Browse the repository at this point in the history
  • Loading branch information
bill88t committed Jul 2, 2022
1 parent 0612cb9 commit 4dec786
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 60 deletions.
107 changes: 59 additions & 48 deletions jz.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,117 +5,128 @@

VERSION = "1.1"


def compress(*argv):
"""
Note: compression cannot be done on the controller
itself at the moment.
"""
#starting print
objc = len(argv)-1

# starting print
objc = len(argv) - 1
targetfile = argv[objc]
print(f"Compressing {str(objc)} files onto {targetfile}")
#let's create the string

# let's create the string
ctlstr = ""
datastr = bytes()
# the control string format is:
# file1 file1len file2 file2len |eocf
for i in range(0, objc):
fnamee = argv[i]
if fnamee.find('/') != -1:
if fnamee.find("/") != -1:
# file not in cwd, we have to cut the name for the control string
fnamee = fnamee[fnamee.rfind("/",1)+1:] # remove everything up until the last /, including the slash
ctlstr += f"{fnamee} " # do not remove the whitespace
fnamee = fnamee[
fnamee.rfind("/", 1) + 1 :
] # remove everything up until the last /, including the slash
ctlstr += f"{fnamee} " # do not remove the whitespace
try:
with open(argv[i], "rb") as togetlelen:
inpt = togetlelen.read()
ctlstr += f"{str(len(inpt))} " # do not remove the whitespace
ctlstr += f"{str(len(inpt))} " # do not remove the whitespace
print(f"Loading: {fnamee} ({str(len(inpt))} bytes)")
datastr += inpt #copying bytes, not str
datastr += inpt # copying bytes, not str
del inpt
except OSError:
print(f"Error: Could not open file {argv[i]}") # intentionally letting it show the full path
return(1)
print(
f"Error: Could not open file {argv[i]}"
) # intentionally letting it show the full path
return 1
del i
collect()
collect()
del objc
ctlstr += "|eocf"
total = bytes(ctlstr,"utf-8") + datastr
total = bytes(ctlstr, "utf-8") + datastr
del datastr
del ctlstr
collect()
collect()
#zlib em

# zlib em
out = zlib.compress(total)
del total
#write to .jz

# write to .jz
with open(targetfile, "wb") as jzfile:
jzfile.write(out)
del out
jzfile.flush()
del targetfile

#done
return(0)

# done
return 0


def decompress(filee, directory="."):
print(f"Decompressing {filee} into {directory if directory != '.' else 'the current directory'}")

#dump file inputted
with open(filee,"rb") as inpf:
print(
f"Decompressing {filee} into {directory if directory != '.' else 'the current directory'}"
)

# dump file inputted
with open(filee, "rb") as inpf:
dataa = inpf.read()
#switch to target dir

# switch to target dir
olddir = getcwd()
chdir(directory)
#unzlib data

# unzlib data
unz = zlib.decompress(dataa)
del dataa
collect()
collect()
#read control string
ctlstr = str(unz[:unz.find(bytes("|eocf","utf-8"),0)],"utf-8")

# read control string
ctlstr = str(unz[: unz.find(bytes("|eocf", "utf-8"), 0)], "utf-8")
ctlarr = ctlstr.split()
#set stepper variables
offset = unz.find(bytes("|eocf","utf-8"),0)+5
for i in range(0, int(len(ctlarr)), 2): # skipping over len

# set stepper variables
offset = unz.find(bytes("|eocf", "utf-8"), 0) + 5

for i in range(0, int(len(ctlarr)), 2): # skipping over len
fname = ctlarr[i]
lco = int(ctlarr[i+1])
lco = int(ctlarr[i + 1])
print(f"Extracting: {fname} ({str(lco)} bytes)")
try:
with open(fname,"wb") as fout:
fout.write(unz[offset:offset+lco])
with open(fname, "wb") as fout:
fout.write(unz[offset : offset + lco])
fout.flush()
except OSError:
print("Error: Could not write file")
return(1)
return 1
offset += lco
del lco, fname
collect()
collect()
#cleanup

# cleanup
del unz
#switch back to start dir

# switch back to start dir
chdir(olddir)
del olddir
return(0)
return 0


def help():
print(f"""
print(
f"""
About: jz file compression module.
Version: {VERSION} - Author: Bill Sideris (bill88t)
This project is licenced under the MIT licence.
Usage: jz.compress(\"file1\", \"file2\", \"jz_archive_name\")
jz.decompress(\"jz_archive_name\")
""")
return(0)
"""
)
return 0
30 changes: 18 additions & 12 deletions jz_board.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,50 @@

VERSION = "1.1-board"


def decompress(filee, directory="."):
print(f"Decompressing {filee} into {directory if directory != '.' else 'the current directory'}")
with open(filee,"rb") as inpf:
print(
f"Decompressing {filee} into {directory if directory != '.' else 'the current directory'}"
)
with open(filee, "rb") as inpf:
dataa = inpf.read()
olddir = getcwd()
chdir(directory)
unz = decompress(dataa)
del dataa
collect()
collect()
ctlstr = str(unz[:unz.find(bytes("|eocf","utf-8"),0)],"utf-8")
ctlstr = str(unz[: unz.find(bytes("|eocf", "utf-8"), 0)], "utf-8")
ctlarr = ctlstr.split()
offset = unz.find(bytes("|eocf","utf-8"),0)+5
offset = unz.find(bytes("|eocf", "utf-8"), 0) + 5
for i in range(0, int(len(ctlarr)), 2):
fname = ctlarr[i]
lco = int(ctlarr[i+1])
lco = int(ctlarr[i + 1])
print(f"Extracting: {fname} ({str(lco)} bytes)")
try:
with open(fname,"wb") as fout:
fout.write(unz[offset:offset+lco])
with open(fname, "wb") as fout:
fout.write(unz[offset : offset + lco])
fout.flush()
except OSError:
print("Error: Could not write file")
return(1)
return 1
offset += lco
del lco, fname
collect()
collect()
del unz
chdir(olddir)
del olddir
return(0)
return 0


def help():
print(f"""
print(
f"""
About: jz file compression module, board edition.
Version: {VERSION} - Author: Bill Sideris (bill88t)
This project is licenced under the MIT licence.
Usage: jz.decompress(\"jz_archive_name\")
""")
return(0)
"""
)
return 0

0 comments on commit 4dec786

Please sign in to comment.