<commit_before>import sys
import os
import re
from PIL import Image

def get_image_size(instructions):
    return (len(instructions), 1) # TODO: output dimension parameters

def hex2rgb(strhex):
    r = int(strhex[:2], 16)
    g = int(strhex[2:4], 16)
    b = int(strhex[4:], 16)
    return (r, g, b)

def set_image_data(instructions, image):
    for i in range(len(instructions)):
        image[i, 0] = hex2rgb(instructions[i]) # TODO: output dimension param
    return image

# get ir filename and additional arguments
flags = []
for arg in sys.argv:
    if arg[0] == "-":
        flags.append(arg) # TODO: handle flags
    else:
        irpath = arg

# open ir file and get instructions
with open(irpath) as f:
    instructions = f.readlines()

# remove whitespace, newline, and comments
for i in range(len(instructions)):
    instructions[i] = instructions[i].strip("\n")
    instructions[i] = re.sub(ur'( )|(;.*$)', '', instructions[i])

instructions = [x for x in instructions if x != ""]
print instructions

# create Rainbow image
image = Image.new("RGB", get_image_size(instructions), "black")
pix = image.load()
pix = set_image_data(instructions, pix)

# write image file
# TODO: output name param
impath = os.path.splitext(irpath)[0]+'.bmp'
print impath
#image.save(impath)<commit_msg>output directory arg with argparse<commit_after>import sys
import os
import re
import argparse
from PIL import Image

def get_image_size(instructions):
    return (len(instructions), 1) # TODO: output dimension parameters

def hex2rgb(strhex):
    r = int(strhex[:2], 16)
    g = int(strhex[2:4], 16)
    b = int(strhex[4:], 16)
    return (r, g, b)

def set_image_data(instructions, image):
    for i in range(len(instructions)):
        image[i, 0] = hex2rgb(instructions[i]) # TODO: output dimension param
    return image

# get ir filename and additional arguments
parser = argparse.ArgumentParser(description='Compiler for the Rainbow Intermediate Representation.')
parser.add_argument('rirpath', type=str, help='rir file to be compiled')
parser.add_argument('--output', '-o', help='destination file for compiled Rainbow script')

args = parser.parse_args()

# open ir file and get instructions
with open(args.rirpath) as f:
    instructions = f.readlines()

# remove whitespace, newline, and comments
for i in range(len(instructions)):
    instructions[i] = instructions[i].strip("\n")
    instructions[i] = re.sub(ur'( )|(;.*$)', '', instructions[i])

instructions = [x for x in instructions if x != ""]

# create Rainbow image
image = Image.new("RGB", get_image_size(instructions), "black")
pix = image.load()
pix = set_image_data(instructions, pix)

# write image file
if args.output is not None:
    impath = args.output
else:
    impath = os.path.splitext(args.rirpath)[0]+'.bmp'

image.save(impath)
{'fraction_non_alphanumeric': 0.13399857448325017, 'fraction_numerical': 0.011047754811119031, 'mean_word_length': 4.439922480620155, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1241243', 'n_tokens_mistral': 925, 'n_tokens_neox': 907, 'n_words': 275}