initial commit for zig project
This commit is contained in:
parent
3b822e4b9a
commit
24e4f01a3e
9 changed files with 19 additions and 161 deletions
14
build.zig
Normal file
14
build.zig
Normal file
|
@ -0,0 +1,14 @@
|
|||
const Builder = @import("std").build.Builder;
|
||||
|
||||
pub fn build(b: *Builder) void {
|
||||
const mode = b.standardReleaseOptions();
|
||||
const exe = b.addExecutable("scritcher", "src/main.zig");
|
||||
exe.setBuildMode(mode);
|
||||
exe.install();
|
||||
|
||||
const run_cmd = exe.run();
|
||||
run_cmd.step.dependOn(b.getInstallStep());
|
||||
|
||||
const run_step = b.step("run", "Run the app");
|
||||
run_step.dependOn(&run_cmd.step);
|
||||
}
|
|
@ -1,2 +0,0 @@
|
|||
from .main import main
|
||||
__all__ = ['main']
|
|
@ -1,2 +0,0 @@
|
|||
class InterpreterError(Exception):
|
||||
pass
|
|
@ -1,78 +0,0 @@
|
|||
import os
|
||||
import shlex
|
||||
import tempfile
|
||||
import shutil
|
||||
|
||||
from pathlib import Path
|
||||
from PIL import Image
|
||||
|
||||
from .utils import load_file_path
|
||||
from .error import InterpreterError
|
||||
from .image import GlitchImage
|
||||
|
||||
|
||||
class Interpreter:
|
||||
"""Interpreter for scritcher instructions/statements."""
|
||||
def __init__(self):
|
||||
self.orig_path = None
|
||||
self.img = None
|
||||
|
||||
def _cmd_noop(self):
|
||||
pass
|
||||
|
||||
def _cmd_load(self, loadpath: str):
|
||||
source_path = load_file_path(loadpath)
|
||||
self.orig_path = source_path
|
||||
|
||||
# create a temporary file to hold bmp data
|
||||
handle, bmp_path = tempfile.mkstemp('.bmp')
|
||||
os.close(handle)
|
||||
|
||||
self.img = GlitchImage(source_path, Path(bmp_path))
|
||||
self.img.load()
|
||||
|
||||
def _cmd_quicksave(self):
|
||||
suffix = self.img.original.suffix
|
||||
name = self.img.original.name.replace(suffix, '')
|
||||
parent_folder = self.orig_path.parents[0]
|
||||
|
||||
# we need to search all files that match the pattern NAME_g*
|
||||
# then we save on the one _g suffix above it.
|
||||
|
||||
# e.g if the original path is "miya.png", we want to search for
|
||||
# files named "miya_g1.raw", "miya_g2.raw", and then we want to write
|
||||
# on "miya_g3.raw".
|
||||
index = 0
|
||||
for glitched_out in parent_folder.glob(f'{name}_g*'):
|
||||
try:
|
||||
idx = int(glitched_out.name.strip(name + '_g')[0])
|
||||
|
||||
# glob() doesnt seem to show a stable order. anyways, we can
|
||||
# just only update our index when its the maximum found
|
||||
if idx > index:
|
||||
index = idx
|
||||
except (IndexError, ValueError):
|
||||
continue
|
||||
|
||||
# create our next glitched path
|
||||
out_path = shutil.copyfile(
|
||||
self.img.path, parent_folder / f'{name}_g{index + 1}.raw')
|
||||
print('saved to', out_path)
|
||||
|
||||
|
||||
def run(self, line: str):
|
||||
"""Run a single line."""
|
||||
print(f'running {line!r}')
|
||||
|
||||
args = shlex.split(line)
|
||||
command = args[0]
|
||||
|
||||
if command != 'load' and self.img is None:
|
||||
print('warn: no file loaded.')
|
||||
|
||||
try:
|
||||
method = getattr(self, f'_cmd_{command}')
|
||||
except AttributeError:
|
||||
raise InterpreterError(f'Command {command!r} not found')
|
||||
|
||||
method(*args[1:])
|
|
@ -1,17 +0,0 @@
|
|||
import logging
|
||||
from PIL import Image
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
class GlitchImage:
|
||||
"""A wrapper class around PIL.Image"""
|
||||
def __init__(self, orig_path, target_path):
|
||||
self.original = orig_path
|
||||
self.path = target_path
|
||||
|
||||
def load(self):
|
||||
"""Load the given image, convert it to BMP, and write it on the
|
||||
given target path."""
|
||||
log.info('opening %r into %r', self.original, self.path)
|
||||
img = Image.open(self.original)
|
||||
img.save(self.path, 'BMP')
|
|
@ -1,32 +0,0 @@
|
|||
import sys
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
from .executer import Interpreter
|
||||
from .error import InterpreterError
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
|
||||
def main():
|
||||
try:
|
||||
scri_path = Path(sys.argv[1]).resolve()
|
||||
except IndexError:
|
||||
print(f'usage: {sys.argv[0]} path/to/file.scri')
|
||||
return
|
||||
|
||||
full_text = scri_path.read_text()
|
||||
full_text = full_text.replace('\n', '')
|
||||
stmts = full_text.split(';')
|
||||
|
||||
interp = Interpreter()
|
||||
|
||||
try:
|
||||
for stmt in stmts:
|
||||
if not stmt:
|
||||
continue
|
||||
|
||||
interp.run(stmt)
|
||||
|
||||
print('OK')
|
||||
except InterpreterError as err:
|
||||
print(f'Interpreter error. {err.args[0]!r}')
|
|
@ -1,16 +0,0 @@
|
|||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from .error import InterpreterError
|
||||
|
||||
def load_file_path(arg: str) -> Path:
|
||||
"""load given argument."""
|
||||
|
||||
try:
|
||||
index = int(arg.split(':')[1])
|
||||
except ValueError:
|
||||
raise InterpreterError('Invalid argument index')
|
||||
except IndexError:
|
||||
return Path(arg).resolve()
|
||||
|
||||
return Path(sys.argv[2 + index])
|
14
setup.py
14
setup.py
|
@ -1,14 +0,0 @@
|
|||
from setuptools import setup
|
||||
|
||||
setup(
|
||||
name='scritcher',
|
||||
version='0.1',
|
||||
py_modules=['scritcher'],
|
||||
install_requires=[
|
||||
'Pillow==6.0.0',
|
||||
],
|
||||
entry_points='''
|
||||
[console_scripts]
|
||||
scritcher=scritcher:main
|
||||
''',
|
||||
)
|
5
src/main.zig
Normal file
5
src/main.zig
Normal file
|
@ -0,0 +1,5 @@
|
|||
const std = @import("std");
|
||||
|
||||
pub fn main() anyerror!void {
|
||||
std.debug.warn("All your base are belong to us.\n");
|
||||
}
|
Loading…
Reference in a new issue