<commit_before>import sublime
import sublime_plugin
import sys

from os import path
from subprocess import Popen, PIPE
from sublime_plugin import TextCommand, WindowCommand

settings = sublime.load_settings('ApiBlueprint.sublime-settings')

class Text():
        @staticmethod
        def all(view):
                return view.substr(sublime.Region(0, view.size()))
        @staticmethod
        def sel(view):
                text = []
                for region in view.sel():
                        if region.empty():
                                continue
                        text.append(view.substr(region))
                return "".join(text)

        @staticmethod
        def get(view):
                text = Text.sel(view)
                if len(text) > 0:
                        return text
                return Text.all(view)

def run_command(cmd, args = [], source="", cwd = None, env = None):
  if not type(args) is list:
    args = [args]
  
  if sys.platform == "win32":
    proc = Popen([cmd]+args, env=env, cwd=cwd, stdout=PIPE, stdin=PIPE, stderr=PIPE, shell=True)
    stat = proc.communicate(input=source)
  
  else:
    if env is None:
      env = {"PATH": settings.get('binDir', '/usr/local/bin')}

    command = [cmd] + args
    
    proc = Popen(command, env=env, cwd=cwd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
    stat = proc.communicate(input=source)
  
  okay = proc.returncode == 0
  
  #remove leading empty line
  lines = stat[1].split('\n')
  if not lines[0]:
    lines.pop(0)
  sanitized_errout = '\n'.join(lines)

  return {"okay": okay, "out": stat[0], "err": sanitized_errout}

class ParseYamlAstCommand(TextCommand):
  def run(self, edit, **kwargs):
    opt = kwargs["opt"]
    args = opt
    
    source = Text.all(self.view)
    result = run_command ("snowcrash", args=args, source=source)
    
    body = result["err"] + '\n' + result["out"]

    output = self.view.window().new_file()
    output.set_scratch(True)
    output.set_syntax_file('Packages/YAML/YAML.tmLanguage')

    if result["okay"] is True:
      output.insert(edit, 0, body)
    else:<commit_msg>Fixed ST3/Python 3 compatibility issues.<commit_after>import sublime
import sublime_plugin
import sys
import os

from os import path
from subprocess import Popen, PIPE
from sublime_plugin import TextCommand, WindowCommand

settings = sublime.load_settings('ApiBlueprint.sublime-settings')

class Text():
        @staticmethod
        def all(view):
                return view.substr(sublime.Region(0, view.size()))
        @staticmethod
        def sel(view):
                text = []
                for region in view.sel():
                        if region.empty():
                                continue
                        text.append(view.substr(region))
                return "".join(text)

        @staticmethod
        def get(view):
                text = Text.sel(view)
                if len(text) > 0:
                        return text
                return Text.all(view)

def run_command(cmd, args = [], source="", cwd = None, env = None):
  if not type(args) is list:
    args = [args]
  
  if sys.platform == "win32":
    proc = Popen([cmd]+args, env=env, cwd=cwd, stdout=PIPE, stdin=PIPE, stderr=PIPE, shell=True)
    stat = proc.communicate(input=source)
  
  else:
    if env is None:
      env = {"PATH": settings.get('binDir', '/usr/local/bin')}

    command = [cmd] + args
    
    proc = Popen(command, env=env, cwd=cwd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
    stat = proc.communicate(input=source.encode('utf-8'))
  
  okay = proc.returncode == 0
  
  #remove leading empty line
  lines = stat[1].decode('UTF-8').split('\n')
  if not lines[0]:
    lines.pop(0)
  sanitized_errout = '\n'.join(lines)

  return {"okay": okay, "out": stat[0].decode('UTF-8'), "err": sanitized_errout}

class ParseYamlAstCommand(TextCommand):
  def run(self, edit, **kwargs):
    opt = kwargs["opt"]
    args = opt
    
    source = Text.all(self.view)
    result = run_command ("snowcrash", args=args, source=source)
    
    body = result["err"] + '\n' + result["out"]

    output = self.view.window().new_file()
    output.set_scratch(True)
    output.set_syntax_file('Packages/YAML/YAML.tmLanguage')

    if result["okay"] is True:
      output.insert(edit, 0, body)
    else:
{'fraction_non_alphanumeric': 0.13354973336424764, 'fraction_numerical': 0.005796429399489914, 'mean_word_length': 2.372947615324472, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 5, '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': '3046445', 'n_tokens_mistral': 1429, 'n_tokens_neox': 1376, 'n_words': 325}