Initial commit
This commit is contained in:
132
utils.py
Normal file
132
utils.py
Normal file
@@ -0,0 +1,132 @@
|
||||
from itertools import chain
|
||||
import io
|
||||
import os
|
||||
import os.path
|
||||
import zipfile
|
||||
|
||||
# import os
|
||||
# import subprocess
|
||||
|
||||
|
||||
def getMameResource(emulatorDir, typeofResource, is_dir):
|
||||
resource = os.path.join(emulatorDir, typeofResource)
|
||||
if is_dir:
|
||||
if not os.path.isdir(resource):
|
||||
return None
|
||||
if os.path.isfile(os.path.join(resource, typeofResource + ".zip")):
|
||||
resource = zipfile.ZipFile(os.path.join(resource, typeofResource + ".zip"))
|
||||
else:
|
||||
resource = os.path.join(resource, typeofResource)
|
||||
else:
|
||||
if not os.path.isfile(resource):
|
||||
return None
|
||||
with open(resource, "rt", encoding="latin1") as f:
|
||||
resource = f.read()
|
||||
|
||||
return resource
|
||||
|
||||
|
||||
# def aspect_scale(img, bx, by):
|
||||
# """Scales 'img' to fit into box bx/by.
|
||||
# This method will retain the original image's aspect ratio"""
|
||||
# ix, iy = img.get_size()
|
||||
# if ix > iy:
|
||||
# # fit to width
|
||||
# scale_factor = bx / float(ix)
|
||||
# sy = scale_factor * iy
|
||||
# if sy > by:
|
||||
# scale_factor = by / float(iy)
|
||||
# sx = scale_factor * ix
|
||||
# sy = by
|
||||
# else:
|
||||
# sx = bx
|
||||
# else:
|
||||
# # fit to height
|
||||
# scale_factor = by / float(iy)
|
||||
# sx = scale_factor * ix
|
||||
# if sx > bx:
|
||||
# scale_factor = bx / float(ix)
|
||||
# sx = bx
|
||||
# sy = scale_factor * iy
|
||||
# else:
|
||||
# sy = by
|
||||
|
||||
# return pygame.transform.scale(img, (int(sx), int(sy)))
|
||||
|
||||
|
||||
# def image_from_data(data, image_size):
|
||||
# if data:
|
||||
# with io.BytesIO(data) as f:
|
||||
# surface = pygame.image.load(f)
|
||||
# surface = aspect_scale(surface, image_size[0], image_size[1])
|
||||
# else:
|
||||
# surface = pygame.Surface(image_size) # pylint: disable=E1121
|
||||
|
||||
# return surface
|
||||
|
||||
|
||||
# def addscanlines(surface):
|
||||
# width = surface.get_width()
|
||||
# for y in range(surface.get_height()):
|
||||
# if y % 2:
|
||||
# pygame.draw.line(surface, (0, 0, 0), (0, y), (width, y))
|
||||
|
||||
# return surface
|
||||
|
||||
|
||||
def truncline(text, font, maxwidth):
|
||||
real = len(text)
|
||||
stext = text
|
||||
size = font.size(text)[0]
|
||||
cut = 0
|
||||
a = 0
|
||||
done = 1
|
||||
while size > maxwidth:
|
||||
a = a + 1
|
||||
n = text.rsplit(None, a)[0]
|
||||
if stext == n:
|
||||
cut += 1
|
||||
stext = n[:-cut]
|
||||
else:
|
||||
stext = n
|
||||
size = font.size(stext)[0]
|
||||
real = len(stext)
|
||||
done = 0
|
||||
return real, done, stext
|
||||
|
||||
|
||||
def wrapline(text, font, maxwidth):
|
||||
done = 0
|
||||
wrapped = []
|
||||
|
||||
while not done:
|
||||
nl, done, stext = truncline(text, font, maxwidth)
|
||||
wrapped.append(stext.strip())
|
||||
text = text[nl:]
|
||||
return wrapped
|
||||
|
||||
|
||||
def wrap_multi_line(text, font, maxwidth):
|
||||
"""returns text taking new lines into account."""
|
||||
if type(text) is str:
|
||||
lines = chain(*(wrapline(line, font, maxwidth) for line in text.splitlines()))
|
||||
else:
|
||||
lines = chain(*(wrapline(line, font, maxwidth) for line in text))
|
||||
|
||||
return list(lines)
|
||||
|
||||
|
||||
# def run_emulator(emu_type, exe, rom):
|
||||
# old_path = os.getcwd()
|
||||
# os.chdir(os.path.split(exe)[0])
|
||||
|
||||
# if emu_type == 'MAME':
|
||||
# subprocess.run([exe, rom])
|
||||
# elif emu_type == 'MESS':
|
||||
# subprocess.run([exe, rom])
|
||||
# elif emu_type == 'CSV':
|
||||
# subprocess.run([exe.replace('<romname>', rom)])
|
||||
|
||||
# os.chdir(old_path)
|
||||
|
||||
# return True
|
||||
Reference in New Issue
Block a user