<commit_before>__author__ = 'alexisgallepe'

from . import Piece
import bitstring
import logging
from threading import Thread
from pubsub import pub


class PiecesManager(Thread):
    def __init__(self, torrent):
        Thread.__init__(self)
        self.torrent = torrent
        self.piecesCompleted = False

        self.numberOfPieces = torrent.numberOfPieces
        self.bitfield = bitstring.BitArray(self.numberOfPieces)
        self.pieces = self.generate_pieces()
        self.files = self.get_files()

        for file in self.files:
            idPiece = file['idPiece']
            self.pieces[idPiece].files.append(file)

        # Create events
        pub.subscribe(self.receive_block_piece, 'PiecesManager.Piece')
        pub.subscribe(self.update_bit_field, 'PiecesManager.PieceCompleted')

    def check_percent_finished(self):
        b = 0
        for i in range(self.numberOfPieces):
            for j in range(self.pieces[i].num_blocks):
                if self.pieces[i].blocks[j][0] == "Full":
                    b += len(self.pieces[i].blocks[j][2])
        return b

    def check_disk_pieces(self):
        for piece in self.pieces:
            piece.isCompleteOnDisk()  # this should set all the finished bools on the finished pieces

    def update_bit_field(self, pieceIndex):
        self.bitfield[pieceIndex] = 1

    def receive_block_piece(self,piece):
        piece_index, piece_offset, piece_data = piece
        self.pieces[int(piece_index)].setBlock(piece_offset, piece_data)

    def generate_pieces(self):
        pieces = []
        for i in range(self.numberOfPieces):
            start = i * 20
            end = start + 20

            if i == (self.numberOfPieces-1):
                pieceLength = self.torrent.totalLength - (self.numberOfPieces-1) * self.torrent.pieceLength
                pieces.append(Piece.Piece(i, pieceLength, self.torrent.pieces[start:end]))
            else:
                pieces.append(Piece.Piece(i, self.torrent.pieceLength, self.torrent.pieces[start:end]))<commit_msg>check_percent_finished operates on a piece level<commit_after>__author__ = 'alexisgallepe'

from . import Piece
import bitstring
import logging
from threading import Thread
from pubsub import pub


class PiecesManager(Thread):
    def __init__(self, torrent):
        Thread.__init__(self)
        self.torrent = torrent
        self.piecesCompleted = False

        self.numberOfPieces = torrent.numberOfPieces
        self.bitfield = bitstring.BitArray(self.numberOfPieces)
        self.pieces = self.generate_pieces()
        self.files = self.get_files()

        for file in self.files:
            idPiece = file['idPiece']
            self.pieces[idPiece].files.append(file)

        # Create events
        pub.subscribe(self.receive_block_piece, 'PiecesManager.Piece')
        pub.subscribe(self.update_bit_field, 'PiecesManager.PieceCompleted')

    def check_percent_finished(self):
        b = 0
        for i in range(self.numberOfPieces):
            if self.pieces[i].finished:
                b += self.pieces[i].pieceSize
        return b


    def check_disk_pieces(self):
        for piece in self.pieces:
            piece.isCompleteOnDisk()  # this should set all the finished bools on the finished pieces

    def update_bit_field(self, pieceIndex):
        self.bitfield[pieceIndex] = 1

    def receive_block_piece(self,piece):
        piece_index, piece_offset, piece_data = piece
        self.pieces[int(piece_index)].setBlock(piece_offset, piece_data)

    def generate_pieces(self):
        pieces = []
        for i in range(self.numberOfPieces):
            start = i * 20
            end = start + 20

            if i == (self.numberOfPieces-1):
                pieceLength = self.torrent.totalLength - (self.numberOfPieces-1) * self.torrent.pieceLength
                pieces.append(Piece.Piece(i, pieceLength, self.torrent.pieces[start:end]))
            else:
                pieces.append(Piece.Piece(i, self.torrent.pieceLength, self.torrent.pieces[start:end]))
{'fraction_non_alphanumeric': 0.096168108776267, 'fraction_numerical': 0.004449938195302843, 'mean_word_length': 2.5398075240594924, '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': '7567788', 'n_tokens_mistral': 1268, 'n_tokens_neox': 1186, 'n_words': 249}