<commit_before>import arrow
import csv
import json
import os
import requests
from requests.auth import HTTPBasicAuth

from .configuracoes import TOGGL_ENDPOINTS, PATH_CACHE


def basic_auth(configuracao):
    return HTTPBasicAuth(configuracao['toggl_api']['token'], 'api_token')


class GerenciadorTarefas:

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

        self.cache = self.ler_cache()

    def pega_tarefa_ativa(self):
        tarefa = self.cache if self.cache else self.pega_tarefa_ativa_toggl()
        try:
            return tarefa['data']['id']
        except TypeError:
            return None

    def finaliza_tarefa(self):
        self._finaliza_tarefa_toggl()
        self._limpa_cache()
        return self.resposta

    def inicie_tarefa(self, endpoint, payload):
        self._inicia_tarefa_toggl(endpoint, payload)
        self._grava_cache()
        return self.resposta

    def ler_cache(self):
        if not os.path.isfile(PATH_CACHE):
            return None
        with open(PATH_CACHE, 'rb') as arquivo_cache:
            stream = arquivo_cache.read()
        return json.loads(stream.decode('utf8'))

    def normaliza_data(self, periodo):
        if periodo:
            datas = str(periodo).split('-')
            data_inicio = arrow.get(datas[0], 'DD/MM/YYYY').to('America/Sao_Paulo')
            data_fim = arrow.get(datas[1], 'DD/MM/YYYY').to('America/Sao_Paulo')
        if not periodo:
            data_inicio = arrow.now().replace(hour=0, minute=0, second=0)
            data_fim = data_inicio.replace(hour=23, minute=59, second=59)
        return data_inicio.isoformat(), data_fim.isoformat()

    def pega_tarefa_ativa_toggl(self):
        url = '{url_base}{endpoint}'.format(url_base=self.configuracao['toggl_api']['url_base'],
                                            endpoint='time_entries/current')
        self.resposta = requests.get(url, auth=basic_auth(self.configuracao))
        return json.loads(self.resposta.content.decode('utf8'))

    def relatorio_tarefas(self, periodo):
        data_inicio, data_fim = self.normaliza_data(periodo)
        self._relatorio_tarefas_toggl(data_inicio, data_fim)
        return json.loads(self.resposta.content.decode('utf8'))

    def _relatorio_tarefas_toggl(self, data_inicio, data_fim):
        endpoint = TOGGL_ENDPOINTS['LISTAR_ENTRADAS_TEMPO'].format(inicio=data_inicio, fim=data_fim)
        url = '{url_base}{endpoint}'.format(url_base=self.configuracao['toggl_api']['url_base'],
                                            endpoint=endpoint)
        self.resposta = requests.get(url, auth=basic_auth(self.configuracao))

    def _finaliza_tarefa_toggl(self):<commit_msg>Corrige relatório por periodo de datas<commit_after>import arrow
import csv
import json
import os
import requests
from requests.auth import HTTPBasicAuth

from .configuracoes import TOGGL_ENDPOINTS, PATH_CACHE


def basic_auth(configuracao):
    return HTTPBasicAuth(configuracao['toggl_api']['token'], 'api_token')


class GerenciadorTarefas:

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

        self.cache = self.ler_cache()

    def pega_tarefa_ativa(self):
        tarefa = self.cache if self.cache else self.pega_tarefa_ativa_toggl()
        try:
            return tarefa['data']['id']
        except TypeError:
            return None

    def finaliza_tarefa(self):
        self._finaliza_tarefa_toggl()
        self._limpa_cache()
        return self.resposta

    def inicie_tarefa(self, endpoint, payload):
        self._inicia_tarefa_toggl(endpoint, payload)
        self._grava_cache()
        return self.resposta

    def ler_cache(self):
        if not os.path.isfile(PATH_CACHE):
            return None
        with open(PATH_CACHE, 'rb') as arquivo_cache:
            stream = arquivo_cache.read()
        return json.loads(stream.decode('utf8'))

    def normaliza_data(self, periodo):
        if periodo:
            datas = str(periodo).split('-')
            data_inicio = arrow.get(datas[0], 'DD/MM/YYYY').to('America/Sao_Paulo').floor('day')
            data_fim = arrow.get(datas[1], 'DD/MM/YYYY').to('America/Sao_Paulo').ceil('day')
        if not periodo:
            data_inicio = arrow.now().replace(hour=0, minute=0, second=0)
            data_fim = data_inicio.replace(hour=23, minute=59, second=59)
        return data_inicio.isoformat(), data_fim.isoformat()

    def pega_tarefa_ativa_toggl(self):
        url = '{url_base}{endpoint}'.format(url_base=self.configuracao['toggl_api']['url_base'],
                                            endpoint='time_entries/current')
        self.resposta = requests.get(url, auth=basic_auth(self.configuracao))
        return json.loads(self.resposta.content.decode('utf8'))

    def relatorio_tarefas(self, periodo):
        data_inicio, data_fim = self.normaliza_data(periodo)
        self._relatorio_tarefas_toggl(data_inicio, data_fim)
        return json.loads(self.resposta.content.decode('utf8'))

    def _relatorio_tarefas_toggl(self, data_inicio, data_fim):
        endpoint = TOGGL_ENDPOINTS['LISTAR_ENTRADAS_TEMPO'].format(inicio=data_inicio, fim=data_fim)
        url = '{url_base}{endpoint}'.format(url_base=self.configuracao['toggl_api']['url_base'],
                                            endpoint=endpoint)
        self.resposta = requests.get(url, auth=basic_auth(self.configuracao))

    def _finaliza_tarefa_toggl(self):
{'fraction_non_alphanumeric': 0.11208791208791209, 'fraction_numerical': 0.005128205128205128, 'mean_word_length': 3.009544787077827, '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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5479076', 'n_tokens_mistral': 1841, 'n_tokens_neox': 1818, 'n_words': 286}