<commit_before>import io
import os
import time
import subprocess
import re
import cherrypy
import calendar
import datetime
from pydispatch import dispatcher

class FFMPEGTranscoderSubprocessTool(cherrypy.Tool):
    """
    This tool makes sure the ffmpeg subprocess is ended
    properly when a request is cancelled
    """
    def __init__(self):
        cherrypy.Tool.__init__(self, 'on_end_request',
                               self.end, priority=20)

    def end(self):
        if (hasattr(cherrypy.request, 'ffmpegtranscoder_subprocess') and
            cherrypy.request.ffmpegtranscoder_subprocess is not None):

            p = cherrypy.request.ffmpegtranscoder_subprocess
            p.stdout.read()
            p.wait()

        track = cherrypy.request.transcoding_track
        dispatcher.send(signal='end_transcoding', sender=track)


class Transcoder:
    def transcode(self):
        raise NotImplementedError()

    @staticmethod
    def outputs():
        raise NotImplementedError()

class FFMPEGTranscoder(Transcoder):

    FNULL = open('/dev/null', 'w')

    def __init__(self, track):
        self.track = track

    def __enter__(self):
        filename = self.track.paths[0].path

        ext = os.path.splitext(os.path.basename(filename))[1].lower()[1:]

        artist = self.track.artist.name
        album = self.track.album.name
        title = self.track.name
        track_number = self.track.number if self.track.number is not None else 0<commit_msg>only dispatch end_transcoding event if track is not None<commit_after>import io
import os
import time
import subprocess
import re
import cherrypy
import calendar
import datetime
from pydispatch import dispatcher

class FFMPEGTranscoderSubprocessTool(cherrypy.Tool):
    """
    This tool makes sure the ffmpeg subprocess is ended
    properly when a request is cancelled
    """
    def __init__(self):
        cherrypy.Tool.__init__(self, 'on_end_request',
                               self.end, priority=20)

    def end(self):
        if (hasattr(cherrypy.request, 'ffmpegtranscoder_subprocess') and
            cherrypy.request.ffmpegtranscoder_subprocess is not None):

            p = cherrypy.request.ffmpegtranscoder_subprocess
            p.stdout.read()
            p.wait()

        if (hasattr(cherrypy.request, 'transcoding_track') and
            cherrypy.request.transcoding_track is not None):

            track = cherrypy.request.transcoding_track
            dispatcher.send(signal='end_transcoding', sender=track)


class Transcoder:
    def transcode(self):
        raise NotImplementedError()

    @staticmethod
    def outputs():
        raise NotImplementedError()

class FFMPEGTranscoder(Transcoder):

    FNULL = open('/dev/null', 'w')

    def __init__(self, track):
        self.track = track

    def __enter__(self):
        filename = self.track.paths[0].path

        ext = os.path.splitext(os.path.basename(filename))[1].lower()[1:]

        artist = self.track.artist.name
        album = self.track.album.name
        title = self.track.name
        track_number = self.track.number if self.track.number is not None else 0
{'fraction_non_alphanumeric': 0.08943348185868873, 'fraction_numerical': 0.0038192234245703373, 'mean_word_length': 2.737217598097503, 'pattern_counts': {'":': 0, '<': 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': '1837887', 'n_tokens_mistral': 948, 'n_tokens_neox': 911, 'n_words': 221}