<commit_before>import dropbox
import tarfile
import os
import sys
from tempfile import TemporaryFile
from lib.chunked_uploader import ChunkedProgressUploader


class BackupFile(object):

    def __init__(self, Name, write):
        restricted_characters = '/\\?%*:|"<>'
        Name = ''.join(filter(lambda x: x not in restricted_characters, Name))

        self.__write = write
        self.__dropbox_client = None
        if os.path.exists('.dropbox_access_token'):
            access_file = open('.dropbox_access_token', 'r')
            access_token = access_file.read()
            self.__dropbox_client = dropbox.client.DropboxClient(access_token)

        if write:
            if self.__dropbox_client:
                self.__tmp_file = TemporaryFile()
                self.__file_name = '/%s.tar.xz' % (Name,)

                self.__file = tarfile.open(mode='w:xz', fileobj=self.__tmp_file, encoding='utf-8')
            else:
                self.__file = tarfile.open(os.path.join('.', 'backups', Name + '.tar.xz'), 'w:xz', encoding='utf-8')
        else:
            if self.__dropbox_client:
                remote_file = self.__dropbox_client.get_file('/%s.tar.xz' % (Name,))
                self.__tmp_file = TemporaryFile()
                self.__tmp_file.write(remote_file.read())
                self.__tmp_file.seek(0)

                self.__file = tarfile.open(mode='r:xz', fileobj=self.__tmp_file, encoding='utf-8')
            else:
                self.__file = tarfile.open(os.path.join('.', 'backups', Name + '.tar.xz'), 'r:xz', encoding='utf-8')

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.__file.close()

        self.__tmp_file.seek(0)
        if self.__write and self.__dropbox_client:
            sys.stdout.write(' uploading to Dropbox...')
            sys.stdout.flush()

            size = os.fstat(self.__tmp_file.fileno()).st_size
            uploader = ChunkedProgressUploader(self.__dropbox_client, self.__tmp_file, size)
<commit_msg>Added initial value for uploading progress<commit_after>import dropbox
import tarfile
import os
import sys
from tempfile import TemporaryFile
from lib.chunked_uploader import ChunkedProgressUploader


class BackupFile(object):

    def __init__(self, Name, write):
        restricted_characters = '/\\?%*:|"<>'
        Name = ''.join(filter(lambda x: x not in restricted_characters, Name))

        self.__write = write
        self.__dropbox_client = None
        if os.path.exists('.dropbox_access_token'):
            access_file = open('.dropbox_access_token', 'r')
            access_token = access_file.read()
            self.__dropbox_client = dropbox.client.DropboxClient(access_token)

        if write:
            if self.__dropbox_client:
                self.__tmp_file = TemporaryFile()
                self.__file_name = '/%s.tar.xz' % (Name,)

                self.__file = tarfile.open(mode='w:xz', fileobj=self.__tmp_file, encoding='utf-8')
            else:
                self.__file = tarfile.open(os.path.join('.', 'backups', Name + '.tar.xz'), 'w:xz', encoding='utf-8')
        else:
            if self.__dropbox_client:
                remote_file = self.__dropbox_client.get_file('/%s.tar.xz' % (Name,))
                self.__tmp_file = TemporaryFile()
                self.__tmp_file.write(remote_file.read())
                self.__tmp_file.seek(0)

                self.__file = tarfile.open(mode='r:xz', fileobj=self.__tmp_file, encoding='utf-8')
            else:
                self.__file = tarfile.open(os.path.join('.', 'backups', Name + '.tar.xz'), 'r:xz', encoding='utf-8')

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.__file.close()

        self.__tmp_file.seek(0)
        if self.__write and self.__dropbox_client:
            sys.stdout.write(' uploading to Dropbox... (0%)')
            sys.stdout.flush()

            size = os.fstat(self.__tmp_file.fileno()).st_size
            uploader = ChunkedProgressUploader(self.__dropbox_client, self.__tmp_file, size)

{'fraction_non_alphanumeric': 0.12907146329606223, 'fraction_numerical': 0.0031599416626154593, 'mean_word_length': 2.331983805668016, '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': '4375504', 'n_tokens_mistral': 1288, 'n_tokens_neox': 1226, 'n_words': 232}