Skip to content

Commit

Permalink
Merge pull request #1 from cytopia/release-0.2
Browse files Browse the repository at this point in the history
Release 0.2
  • Loading branch information
cytopia authored Jan 14, 2020
2 parents 6e79f91 + 53901af commit c7316ba
Show file tree
Hide file tree
Showing 7 changed files with 328 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
badchars.egg-info/
build/
dist/
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 cytopia <https://github.com/cytopia>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
110 changes: 110 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
ifneq (,)
.error This Makefile requires GNU Make.
endif

# -------------------------------------------------------------------------------------------------
# Default configuration
# -------------------------------------------------------------------------------------------------
.PHONY: help build clean


VERSION = 2.7

# -------------------------------------------------------------------------------------------------
# Default Target
# -------------------------------------------------------------------------------------------------
help:
@echo "lint Lint source code"
@echo "build Build Python package"
@echo "dist Create source and binary distribution"
@echo "sdist Create source distribution"
@echo "bdist Create binary distribution"
@echo "clean Build"


# -------------------------------------------------------------------------------------------------
# Lint Targets
# -------------------------------------------------------------------------------------------------

lint: pycodestyle pydocstyle black

pycodestyle:
docker run --rm -v $(PWD):/data cytopia/pycodestyle --show-source --show-pep8 badchars

pydocstyle:
docker run --rm -v $(PWD):/data cytopia/pydocstyle badchars

black:
docker run --rm -v ${PWD}:/data cytopia/black -l 100 badchars


# -------------------------------------------------------------------------------------------------
# Build Targets
# -------------------------------------------------------------------------------------------------

dist: sdist bdist

sdist:
docker run \
--rm \
$$(tty -s && echo "-it" || echo) \
-v $(PWD):/data \
-w /data \
-u $$(id -u):$$(id -g) \
python:$(VERSION)-alpine \
python setup.py sdist

bdist:
docker run \
--rm \
$$(tty -s && echo "-it" || echo) \
-v $(PWD):/data \
-w /data \
-u $$(id -u):$$(id -g) \
python:$(VERSION)-alpine \
python setup.py bdist_wheel --universal

build:
docker run \
--rm \
$$(tty -s && echo "-it" || echo) \
-v $(PWD):/data \
-w /data \
-u $$(id -u):$$(id -g) \
python:$(VERSION)-alpine \
python setup.py build


# -------------------------------------------------------------------------------------------------
# Publish Targets
# -------------------------------------------------------------------------------------------------

checkbuild:
docker run \
--rm \
$$(tty -s && echo "-it" || echo) \
-v $(PWD):/data \
-w /data \
python:$(VERSION)-alpine \
sh -c "pip install twine \
&& twine check dist/*"

deploy:
docker run \
--rm \
$$(tty -s && echo "-it" || echo) \
-v $(PWD):/data \
-w /data \
python:$(VERSION)-alpine \
sh -c "pip install twine \
&& twine upload dist/*"


# -------------------------------------------------------------------------------------------------
# Misc Targets
# -------------------------------------------------------------------------------------------------

clean:
-rm -rf badchars.egg-info/
-rm -rf dist/
-rm -rf build/
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,18 @@ optional arguments:
## Examples

#### Plain
```
```bash
$ badchars
```
```
\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff
```

#### C code
```
```bash
$ badchars -f c
```
```cpp
char badchars[] =
"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10"
"\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20"
Expand All @@ -64,8 +68,10 @@ char badchars[] =
```

#### Python
```
```bash
$ badchars -f python
```
```python
badchars = (
"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10"
"\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20"
Expand Down
137 changes: 137 additions & 0 deletions badchars
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#!/usr/bin/env python
"""Print badchars."""

import sys
import argparse
import math


# -------------------------------------------------------------------------------------------------
# GLOBALS
# -------------------------------------------------------------------------------------------------
DEFAULT_LENGTH = 255
FORMATS = {
"c": {
"block": {"pre": "char badchars[] = \n", "post": '";'},
"line": {"pre": ' "', "post": '"'},
},
"js": {
"block": {"pre": "var badchars = \n", "post": '";'},
"line": {"pre": ' "', "post": '" +'},
},
"php": {
"block": {"pre": "$badchars = \n", "post": '";'},
"line": {"pre": ' "', "post": '" +'},
},
"python": {
"block": {"pre": "badchars = (\n", "post": '"\n)'},
"line": {"pre": ' "', "post": '"'},
},
}


# -------------------------------------------------------------------------------------------------
# DECORATOR
# -------------------------------------------------------------------------------------------------
def formatOutput(f):
"""Decorate/format the output of badchars."""

def new_f(length, format):
if format is not None:
sys.stdout.write(FORMATS[format]["block"]["pre"])
f(length, format)
if format is not None:
sys.stdout.write(FORMATS[format]["block"]["post"])

return new_f


# -------------------------------------------------------------------------------------------------
# FUNCTIONS
# -------------------------------------------------------------------------------------------------
@formatOutput
def print_badchars(length, format=None):
"""Print bad characters."""
if format is None:
for x in range(1, length + 1):
sys.stdout.write("\\x" + "{:02x}".format(x))
else:
linebreak = 16
rows = int(math.ceil(float(length) / linebreak))
count = 0
for row in range(1, rows + 1):
sys.stdout.write(FORMATS[format]["line"]["pre"])
for char in range(1, linebreak + 1):
if count == length:
break
count += 1
sys.stdout.write("\\x" + "{:02x}".format(count))
if count == length:
break
sys.stdout.write(FORMATS[format]["line"]["post"])
sys.stdout.write("\n")


# -------------------------------------------------------------------------------------------------
# ARG HELPER
# -------------------------------------------------------------------------------------------------
def _args_check_length(value):
"""Check arguments for valid length."""
min_len = 1
intvalue = int(value)

if intvalue < min_len:
raise argparse.ArgumentTypeError("%s is an invalid length." % value)
return intvalue


def _args_check_format(value):
"""Check arguments for valid format."""
if value not in FORMATS:
raise argparse.ArgumentTypeError("%s is an invalid format." % value)
return value


# -------------------------------------------------------------------------------------------------
# ENTRYPOINT
# -------------------------------------------------------------------------------------------------
def main():
"""Start the program."""
parser = argparse.ArgumentParser(description="Badchar generator.")
parser.add_argument(
"-v",
"--version",
action="version",
version="%(prog)s 0.2 by cytopia",
help="Show version information,",
)
parser.add_argument(
"-l",
"--length",
metavar="int",
required=False,
type=_args_check_length,
help="Length of badchars to create. Default: " + str(DEFAULT_LENGTH),
)
parser.add_argument(
"-f",
"--format",
metavar="str",
required=False,
type=_args_check_format,
help="Format output: " + ", ".join(FORMATS.keys()),
)
args = parser.parse_args()
length = DEFAULT_LENGTH if args.length is None else args.length

print_badchars(length, args.format)
sys.stdout.write("\n")


if __name__ == "__main__":
# Catch Ctrl+c and exit without error message
try:
main()
except KeyboardInterrupt:
print()
sys.exit(1)
8 changes: 8 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[metadata]
description-file = README.md

[bdist_wheel]
universal=1

[pycodestyle]
max-line-length = 100
40 changes: 40 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from setuptools import setup

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

setup(
name="badchars",
version="0.2",
description="A badchar generator for different formats.",
license="MIT",
long_description=long_description,
long_description_content_type="text/markdown",
author="cytopia",
author_email="[email protected]",
url="https://github.com/cytopia/badchars",
install_requires=["argparse"],
scripts=[
"badchars"
],
classifiers=[
# How mature is this project? Common values are
# 3 - Alpha
# 4 - Beta
# 5 - Production/Stable
'Development Status :: 5 - Production/Stable',

# Indicate who your project is intended for
'Intended Audience :: Developers',
'Topic :: Software Development :: Build Tools',

# License
"License :: OSI Approved :: MIT License",

# Specify the Python versions you support here. In particular, ensure
# that you indicate whether you support Python 2, Python 3 or both.
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 3",
"Operating System :: OS Independent",
],
)

0 comments on commit c7316ba

Please sign in to comment.