<commit_before>"""
Inserts progress commands in skeinforge gcode
"""

import os
import sys
import re
import Preprocessor

class ProgressPreprocessor(Preprocessor.Preprocessor):
    
    def __init__(self):
        self.command = re.compile('([A-Z]\d+(\.\d+)? )+')
        self.countTotal = 1
        self.countCurrent = 0
    
    def count_commands(self, infh):
        self.countTotal = 1
        self.countCurrent = 0
        for line in infh:
            if self.command.match(line):
                self.countTotal += 1

    def print_progress(self, outfh):
        curPercent = int((self.countCurrent * 100) / self.countTotal)
        lastPercent = int(((self.countCurrent - 1) * 100 ) / self.countTotal)
        if lastPercent != curPercent:
            progressmsg = "M73 P%s (progress (%s%%): %s/%s)\n" % (curPercent, curPercent, self.countCurrent, self.countTotal)
            outfh.write(progressmsg)
    
    def process_file(self, input_path, output_path):
        self.inputs_are_gcode(input_path, output_path)
        with open(input_path, 'r') as infh, open(output_path, 'w') as outfh:
            self.count_commands(infh)
            infh.seek(0)
            for line in infh:
                if self.command.match(line):
                    self.countCurrent += 1
                    self.print_progress(outfh)
                outfh.write(line)

def main():
    ProgressPreprocessor().process_file(sys.argv[1], sys.argv[2])

if __name__=="__main__":
    sys.exit(main())<commit_msg>multiple-clause 'with' statement doesn't work until python 2.7<commit_after>"""
Inserts progress commands in skeinforge gcode
"""

import os
import sys
import re
import Preprocessor

class ProgressPreprocessor(Preprocessor.Preprocessor):
    
    def __init__(self):
        self.command = re.compile('([A-Z]\d+(\.\d+)? )+')
        self.countTotal = 1
        self.countCurrent = 0
    
    def count_commands(self, infh):
        self.countTotal = 1
        self.countCurrent = 0
        for line in infh:
            if self.command.match(line):
                self.countTotal += 1

    def print_progress(self, outfh):
        curPercent = int((self.countCurrent * 100) / self.countTotal)
        lastPercent = int(((self.countCurrent - 1) * 100 ) / self.countTotal)
        if lastPercent != curPercent:
            progressmsg = "M73 P%s (progress (%s%%): %s/%s)\n" % (curPercent, curPercent, self.countCurrent, self.countTotal)
            outfh.write(progressmsg)
    
    def process_file(self, input_path, output_path):
        self.inputs_are_gcode(input_path, output_path)
        with open(input_path, 'r') as infh:
            with open(output_path, 'w') as outfh:
                self.count_commands(infh)
                infh.seek(0)
                for line in infh:
                    if self.command.match(line):
                        self.countCurrent += 1
                        self.print_progress(outfh)
                    outfh.write(line)

def main():
    ProgressPreprocessor().process_file(sys.argv[1], sys.argv[2])

if __name__=="__main__":
    sys.exit(main())
{'fraction_non_alphanumeric': 0.11854005167958656, 'fraction_numerical': 0.01227390180878553, 'mean_word_length': 2.256572029442692, 'pattern_counts': {'":': 2, '<': 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': '5446616', 'n_tokens_mistral': 984, 'n_tokens_neox': 926, 'n_words': 207}