Skip to content

Commit

Permalink
Reorganise source files to publish as library
Browse files Browse the repository at this point in the history
  • Loading branch information
leoetlino committed Jul 30, 2018
1 parent 66ef866 commit b9751e3
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 94 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
__pycache__/
*.pyc
*.egg-info/
*.dist-info/
dist/
10 changes: 5 additions & 5 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
- [wszst](https://szs.wiimm.de/download.html#os-cygwin)
- Modules: sortedcontainers, PyYAML

## First use (Windows)
## First use

* Install wszst
* Install dependencies: `pip install sortedcontainers pyyaml`
* Install wszst (and reboot if you're on Windows)
* Install this module: `pip install byml`

## BYML to YAML

Just run the byml_to_yml script:

```shell
python byml_to_yml PATH_TO_BYML PATH_TO_YAML
byml_to_yml PATH_TO_BYML PATH_TO_YAML
```

**If the byml is compressed, this tool will automatically decompress them.**
Expand All @@ -28,7 +28,7 @@ Example: to convert to YAML in the same directory as the BYML, use `byml_to_yml
## YAML to BYML

```shell
python yml_to_byml PATH_TO_YAML PATH_TO_BYML
yml_to_byml PATH_TO_YAML PATH_TO_BYML
```

**Add `-b` at the end if big endian should be used. For the Wii U version of Breath of the Wild,
Expand Down
1 change: 1 addition & 0 deletions byml/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .byml import *
79 changes: 79 additions & 0 deletions byml/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env python3
import argparse
import io
import os
import re
import shutil
import sys
import yaml

from . import byml
from . import yaz0_util

def byml_to_yml() -> None:
parser = argparse.ArgumentParser(description='Converts a BYML file to YAML.')
parser.add_argument('byml', help='Path to a BYML file', nargs='?', default='-')
parser.add_argument('yml', help='Path to destination YAML file', nargs='?', default='-')
args = parser.parse_args()

dumper = yaml.CDumper
yaml.add_representer(byml.Int, lambda d, data: d.represent_int(data), Dumper=dumper)
yaml.add_representer(byml.Float, lambda d, data: d.represent_float(data), Dumper=dumper)
yaml.add_representer(byml.UInt, lambda d, data: d.represent_scalar(u'!u', '0x%08x' % data), Dumper=dumper)
yaml.add_representer(byml.Int64, lambda d, data: d.represent_scalar(u'!l', str(data)), Dumper=dumper)
yaml.add_representer(byml.UInt64, lambda d, data: d.represent_scalar(u'!ul', str(data)), Dumper=dumper)
yaml.add_representer(byml.Double, lambda d, data: d.represent_scalar(u'!f64', str(data)), Dumper=dumper)

file = sys.stdin.buffer if args.byml == '-' else open(args.byml, 'rb')
with file:
data = file.read()
if data[0:4] == b'Yaz0':
data = yaz0_util.decompress(data)
root = byml.Byml(data).parse()

if args.byml != '-':
args.yml = args.yml.replace('!!', os.path.splitext(args.byml)[0])
elif '!!' in args.yml:
sys.stderr.write('error: cannot use !! (for input filename) when reading from stdin\n')
sys.exit(1)
output = sys.stdout if args.yml == '-' else open(args.yml, 'w')
with output:
yaml.dump(root, output, Dumper=dumper, allow_unicode=True)

def yml_to_byml() -> None:
parser = argparse.ArgumentParser(description='Converts a YAML file to BYML.')
parser.add_argument('yml', help='Path to a YAML file', nargs='?', default='-')
parser.add_argument('byml', help='Path to destination BYAML file', nargs='?', default='-')
parser.add_argument('-V', '--version', default=2, help='BYML version (1, 2, 3)')
parser.add_argument('-b', '--be', action='store_true', help='Use big endian. Defaults to false.')
args = parser.parse_args()

loader = yaml.CSafeLoader
yaml.add_constructor(u'tag:yaml.org,2002:int', lambda l, node: byml.Int(l.construct_yaml_int(node)), Loader=loader)
yaml.add_constructor(u'tag:yaml.org,2002:float', lambda l, node: byml.Float(l.construct_yaml_float(node)), Loader=loader)
yaml.add_constructor(u'!u', lambda l, node: byml.UInt(l.construct_yaml_int(node)), Loader=loader)
yaml.add_constructor(u'!l', lambda l, node: byml.Int64(l.construct_yaml_int(node)), Loader=loader)
yaml.add_constructor(u'!ul', lambda l, node: byml.UInt64(l.construct_yaml_int(node)), Loader=loader)
yaml.add_constructor(u'!f64', lambda l, node: byml.Double(l.construct_yaml_float(node)), Loader=loader)

file = sys.stdin if args.yml == '-' else open(args.yml, 'r', encoding='utf-8')
with file:
root = yaml.load(file, Loader=loader)
buf = io.BytesIO()
byml.Writer(root, be=args.be, version=args.version).write(buf)
buf.seek(0)

if args.yml != '-':
args.byml = args.byml.replace('!!', os.path.splitext(args.yml)[0])
elif '!!' in args.byml:
sys.stderr.write('error: cannot use !! (for input filename) when reading from stdin\n')
sys.exit(1)

if args.byml != '-':
extension = os.path.splitext(args.byml)[1]
if extension.startswith('.s'):
buf = io.BytesIO(yaz0_util.compress(buf.read()))

output = sys.stdout.buffer if args.byml == '-' else open(args.byml, 'wb')
with output:
shutil.copyfileobj(buf, output)
3 changes: 0 additions & 3 deletions byml.py → byml/byml.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#!/usr/bin/env python3
# Copyright 2018 leoetlino <[email protected]>
# Licensed under MIT

from enum import IntEnum
import ctypes
from sortedcontainers import SortedDict # type: ignore
import struct
import typing
Expand Down
File renamed without changes.
38 changes: 0 additions & 38 deletions byml_to_yml

This file was deleted.

30 changes: 30 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import setuptools

with open("README.md", "r") as fh:
long_description = fh.read()

setuptools.setup(
name="byml",
version="2.0.0-2",
author="leoetlino",
author_email="[email protected]",
description="A simple Nintendo BYML or BYAML v2/v3 parser and writer",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/leoetlino/byml-v2",
packages=setuptools.find_packages(),
classifiers=[
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
"Programming Language :: Python :: 3 :: Only",
"Topic :: Software Development :: Libraries",
],
python_requires='>=3.6',
install_requires=['PyYAML~=3.12', 'sortedcontainers~=2.0'],
entry_points = {
'console_scripts': [
'byml_to_yml = byml.__main__:byml_to_yml',
'yml_to_byml = byml.__main__:yml_to_byml'
]
},
)
48 changes: 0 additions & 48 deletions yml_to_byml

This file was deleted.

0 comments on commit b9751e3

Please sign in to comment.