<commit_before>import os
import argparse
import shlex
import subprocess
import shutil
import sys

# Sanity check to ensure that Python is present on the target system.
if shutil.which('ffmpeg') is None:
    print('This program requires ffmpeg to operate')
    sys.exit(1)


class pyfftrim:
    def __init__(self, name=None, depth=1, postfix='_trimmed'):
        """
        Instantiates the pyfftrim object. If called with the appropriate parameters, this may also populate the list
        in preparation for parsing.

        :param name: The file or directory we want to add to the list of items to parse.
        :param depth: If name is a directory, this specifies how many sub-directories we are willing to traverse.
        """
        self.files = list()
        self.postfix = postfix

        if name is None:
            return

        if os.path.isfile(name):
            self.files = [name]

        elif os.path.isdir(name):
            self.add_dir(name, depth)

        else:
            raise FileNotFoundError

    def add_dir(self, path, depth=1):
        """
        Recursively adds all files in the specified directory to the list of files to be processed.

        :param path: The path of the directory that we wish to add.
        :param depth: The number of directories deep that we traverse. By default, we don't traverse into any
                      sub-directories. Use caution when specifying this parameter.
        :return: This method does not return a value.
        """
        for entry in os.listdir(path):
            if os.path.isfile(entry):
                self.files.append(entry)

            # We are dealing with the directory.
            else:
                if depth > 1:
                    self.add_dir(entry, depth-1)

    def add_file(self, name):
        """
        Simple appends a single file to the list.

        :param name: The name of the file to be added to the list.
        :return: Returns True if the file was successfully added or false if it failed.
        """
        if os.path.isfile(name):
            self.files.append(name)
            return True

        return False

    @staticmethod
    def _get_duration(path):
        """
        Gets the duration of a given file using FFPROBE.

        :param path: The path of the file to get the duration of.
        :return: Returns an integer representation of the length of a video in seconds.
        """
        # Sanity checks
        if not os.path.isfile(path):
            raise FileNotFoundError

        # Properly format the command and its arguments
        cmd = f'ffprobe -show_entries format=duration -v quiet -of csv="p=0" {shlex.quote(path)}'
        subprocess_args = shlex.split(cmd)

        # Run the command, get the output, and decode it as ASCII<commit_msg>Added sanity checks and reworked error handling.<commit_after>import os
import argparse
import shlex
import subprocess
import shutil
import sys

# Sanity check to ensure that Python is present on the target system.
if shutil.which('ffmpeg') is None:
    print('This program requires ffmpeg to operate')
    sys.exit(1)


class pyfftrim:
    def __init__(self, name=None, depth=1, postfix='_trimmed'):
        """
        Instantiates the pyfftrim object. If called with the appropriate parameters, this may also populate the list
        in preparation for parsing.

        :param name: The file or directory we want to add to the list of items to parse.
        :param depth: If name is a directory, this specifies how many sub-directories we are willing to traverse.
        """
        # Sanity checks
        if name is None:
            return

        if depth < 1:
            raise RuntimeError

        if postfix == '':
            raise RuntimeError

        self.files = list()
        self.postfix = postfix

        if os.path.isfile(name):
            self.files = [name]

        elif os.path.isdir(name):
            self.add_dir(name, depth)

        else:
            raise FileNotFoundError

    def add_dir(self, path, depth=1):
        """
        Recursively adds all files in the specified directory to the list of files to be processed.

        :param path: The path of the directory that we wish to add.
        :param depth: The number of directories deep that we traverse. By default, we don't traverse into any
                      sub-directories. Use caution when specifying this parameter.
        :return: Returns True if the directory was added or false if it were not.
        """
        # Sanity checks
        if depth < 1:
            return False

        if not os.path.isdir(path):
            return False

        for entry in os.listdir(path):
            if os.path.isfile(entry):
                self.files.append(entry)

            # We are dealing with a sub-directory.
            else:
                if depth > 1:
                    self.add_dir(entry, depth-1)

        return True

    def add_file(self, name):
        """
        Simple appends a single file to the list.

        :param name: The name of the file to be added to the list.
        :return: Returns True if the file was successfully added or false if it failed.
        """
        if not os.path.isfile(name):
            return False

        self.files.append(name)
        return True

    @staticmethod
    def _get_duration(path):
        """
        Gets the duration of a given file using FFPROBE.

        :param path: The path of the file to get the duration of.
        :return: Returns an integer representation of the length of a video in seconds.
        """
        # Sanity checks
        if not os.path.isfile(path):
            raise FileNotFoundError

        # Properly format the command and its arguments
        cmd = f'ffprobe -show_entries format=duration -v quiet -of csv="p=0" {shlex.quote(path)}'
        subprocess_args = shlex.split(cmd)

        # Run the command, get the output, and decode it as ASCII
{'fraction_non_alphanumeric': 0.06869973190348526, 'fraction_numerical': 0.0023458445040214475, 'mean_word_length': 1.9964859437751004, 'pattern_counts': {'":': 0, '<': 5, '<?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': '4170543', 'n_tokens_mistral': 1613, 'n_tokens_neox': 1537, 'n_words': 671}