<commit_before>import hashlib
import json
import logging
import os
import time
import urllib
import urllib2
from threading import Thread

import pygame

from .screen_objects import (Progressbar, ScreenObjectsManager, TextItem,
                             TouchAndTextItem)
from .input_manager import InputManager

logger = logging.getLogger(__name__)


class MainScreen():
    def __init__(self, size, manager, cache, core, fonts):
        self.core = core
        self.size = size
        self.base_size = self.size[1] / 8
        self.fonts = fonts
        self.manager = manager
        self.track = None
        self.cache = cache
        self.image = None
        self.artists = None
	self.image_now_loaded = False
        self.touch_text_manager = ScreenObjectsManager()
        current_track = self.core.playback.current_track.get()
        if current_track is None:
            self.track_playback_ended(None, None)
        else:
            self.track_started(current_track)

    def update(self, screen, update_all):
        if self.track is not None:
            self.touch_text_manager.get_touch_object(
                "time_progress").set_value(
                self.core.playback.time_position.get() / 1000)
            self.touch_text_manager.get_touch_object("time_progress").set_text(
                time.strftime('%M:%S', time.gmtime(
                    self.core.playback.time_position.get() / 1000)) + "/" +
                time.strftime('%M:%S', time.gmtime(
                    self.track.length / 1000)))
	    if update_all:
		if self.image is not None:
                    screen.blit(self.image, (self.base_size / 2, self.base_size + self.base_size / 2))
		    self.image_now_loaded = False
	    if self.image_now_loaded:
		screen.blit(self.image, (self.base_size / 2, self.base_size + self.base_size / 2))
		self.image_now_loaded = False
	self.touch_text_manager.render(screen)
        return screen

    def track_started(self, track):
        self.image = None
        x = self.base_size * 5
        width = self.size[0] - self.base_size / 2 - x

        # Load all artists
        self.artists = []
        for artist in track.artists:
            self.artists.append(artist)

        # Track name
        label = TextItem(self.fonts['base'], MainScreen.get_track_name(track),
                         (x, self.base_size * 2),
                         (width, self.size[1]))
        self.touch_text_manager.set_object("track_name", label)

        # Album name
        label = TextItem(self.fonts['base'],
                         MainScreen.get_track_album_name(track),
                         (x, self.base_size * 3),
                         (width, self.size[1]))
        self.touch_text_manager.set_object("album_name", label)

        # Artist
        label = TextItem(self.fonts['base'], self.get_artist_string(),
                         (x, self.base_size * 4), (width, self.size[1]))
        self.touch_text_manager.set_object("artist_name", label)

        # Previous track button
        button = TouchAndTextItem(self.fonts['icon'], u"\ue61c",
                                  (0, self.base_size * 6), None)
        self.touch_text_manager.set_touch_object("previous", button)
        size_1 = button.get_right_pos()

        size_2 = self.fonts['icon'].size(u"\ue61d")[0]
        button = TouchAndTextItem(self.fonts['icon'], u"\ue61d",
                                  (self.size[0] - size_2, self.base_size * 6),
                                  None)
        self.touch_text_manager.set_touch_object("next", button)

        # Progress
        progress = Progressbar(self.fonts['base'],
                               time.strftime('%M:%S', time.gmtime(
                                   0)) + "/" + time.strftime('%M:%S',
                                                             time.gmtime(0)),
                               (size_1, self.base_size * 6),
                               (
                               self.size[0] - size_1 - size_2, self.base_size),
                               track.length / 1000, False)
        self.touch_text_manager.set_touch_object("time_progress", progress)

        self.track = track
        if not self.is_image_in_cache():
            thread = Thread(target=self.download_image(0))
            thread.start()
        else:
            self.load_image()

    def get_dirty_area(self):
	return self.touch_text_manager.get_dirty_area()

    def get_artist_string(self):
        artists_string = ''
        for artist in self.artists:
            artists_string += artist.name + ', '
        if len(artists_string) > 2:
            artists_string = artists_string[:-2]
        elif len(artists_string) == 0:
            artists_string = "Unknow Artist"
        return artists_string

    def get_image_file_name(self):
        name = MainScreen.get_track_album_name(
            self.track) + '-' + self.get_artist_string()
        md5name = hashlib.md5(name.encode('utf-8')).hexdigest()
        return md5name

    def get_cover_folder(self):
        if not os.path.isdir(self.cache + "/covers"):
            os.makedirs(self.cache + "/covers")
        return self.cache + "/covers/"

    def is_image_in_cache(self):
        self.get_cover_folder()
        return os.path.isfile(
            self.get_cover_folder() + self.get_image_file_name())<commit_msg>Small changes<commit_after>import hashlib
import json
import logging
import os
import time
import urllib
import urllib2
from threading import Thread

import pygame

from .screen_objects import (Progressbar, ScreenObjectsManager, TextItem,
                             TouchAndTextItem)
from .input_manager import InputManager

logger = logging.getLogger(__name__)


class MainScreen():
    def __init__(self, size, manager, cache, core, fonts):
        self.core = core
        self.size = size
        self.base_size = self.size[1] / 8
        self.fonts = fonts
        self.manager = manager
        self.track = None
        self.cache = cache
        self.image = None
        self.artists = None
	self.dirty_area = []
	self.image_now_loaded = False
        self.touch_text_manager = ScreenObjectsManager()
        current_track = self.core.playback.current_track.get()
        if current_track is None:
            self.track_playback_ended(None, None)
        else:
            self.track_started(current_track)

    def update(self, screen, update_all):
        if self.track is not None:
            self.touch_text_manager.get_touch_object(
                "time_progress").set_value(
                self.core.playback.time_position.get() / 1000)
            #self.touch_text_manager.get_touch_object("time_progress").set_text(
             #   time.strftime('%M:%S', time.gmtime(
            #        self.core.playback.time_position.get() / 1000)) + "/" +
            #    time.strftime('%M:%S', time.gmtime(
             #       self.track.length / 1000)))
	    if update_all:
		if self.image is not None:
                    screen.blit(self.image, (self.base_size / 2, self.base_size + self.base_size / 2))
		    self.image_now_loaded = False
	    if self.image_now_loaded:
		self.dirty_area.append(self.image.get_rect())
		screen.blit(self.image, (self.base_size / 2, self.base_size + self.base_size / 2))
		self.image_now_loaded = False
	self.touch_text_manager.render(screen)
        return screen

    def track_started(self, track):
        self.image = None
        x = self.base_size * 5
        width = self.size[0] - self.base_size / 2 - x

        # Load all artists
        self.artists = []
        for artist in track.artists:
            self.artists.append(artist)

        # Track name
        label = TextItem(self.fonts['base'], MainScreen.get_track_name(track),
                         (x, self.base_size * 2),
                         (width, self.size[1]))
        self.touch_text_manager.set_object("track_name", label)

        # Album name
        label = TextItem(self.fonts['base'],
                         MainScreen.get_track_album_name(track),
                         (x, self.base_size * 3),
                         (width, self.size[1]))
        self.touch_text_manager.set_object("album_name", label)

        # Artist
        label = TextItem(self.fonts['base'], self.get_artist_string(),
                         (x, self.base_size * 4), (width, self.size[1]))
        self.touch_text_manager.set_object("artist_name", label)

        # Previous track button
        button = TouchAndTextItem(self.fonts['icon'], u"\ue61c",
                                  (0, self.base_size * 6), None)
        self.touch_text_manager.set_touch_object("previous", button)
        size_1 = button.get_right_pos()

        size_2 = self.fonts['icon'].size(u"\ue61d")[0]
        button = TouchAndTextItem(self.fonts['icon'], u"\ue61d",
                                  (self.size[0] - size_2, self.base_size * 6),
                                  None)
        self.touch_text_manager.set_touch_object("next", button)

        # Progress
        progress = Progressbar(self.fonts['base'],time.strftime('%M:%S', time.gmtime(0)) + "/" + time.strftime('%M:%S',time.gmtime(0)),
                               (size_1, self.base_size * 6),
                               (
                               self.size[0] - size_1 - size_2, self.base_size),
                               track.length / 1000, False)
        self.touch_text_manager.set_touch_object("time_progress", progress)

        self.track = track
        if not self.is_image_in_cache():
            thread = Thread(target=self.download_image(0))
            thread.start()
        else:
            self.load_image()

    def get_dirty_area(self):
	dirty = self.touch_text_manager.get_dirty_area() + self.dirty_area
	self.dirty_area = []
	return dirty

    def get_artist_string(self):
        artists_string = ''
        for artist in self.artists:
            artists_string += artist.name + ', '
        if len(artists_string) > 2:
            artists_string = artists_string[:-2]
        elif len(artists_string) == 0:
            artists_string = "Unknow Artist"
        return artists_string

    def get_image_file_name(self):
        name = MainScreen.get_track_album_name(
            self.track) + '-' + self.get_artist_string()
        md5name = hashlib.md5(name.encode('utf-8')).hexdigest()
        return md5name

    def get_cover_folder(self):
        if not os.path.isdir(self.cache + "/covers"):
            os.makedirs(self.cache + "/covers")
        return self.cache + "/covers/"

    def is_image_in_cache(self):
        self.get_cover_folder()
        return os.path.isfile(
            self.get_cover_folder() + self.get_image_file_name())
{'fraction_non_alphanumeric': 0.11051062243756989, 'fraction_numerical': 0.011367871785314947, 'mean_word_length': 1.9389375684556407, 'pattern_counts': {'":': 0, '<': 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': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1190711', 'n_tokens_mistral': 3331, 'n_tokens_neox': 3154, 'n_words': 587}