<commit_before>from __future__ import absolute_import

from Crypto.Cipher import AES
from Crypto.PublicKey import RSA
from Crypto.Util.strxor import strxor
from Crypto.Util.number import bytes_to_long, long_to_bytes
from Crypto.Util import Counter
from Crypto.Random import random

from .utils import b64encode, b64decode

import itertools
import weakref
import datetime
import json
import urlparse
import os
import os.path
import requests
import logging

from . import errors
from . import protocol
from . import transport
from . import models
from . import utils
from . import errors
from . import protocol

__all__ = [ 'Session', 'File', 'User' ]

class Session(object):
    def __init__(self, username = None, password = None):
        self.sequence = itertools.count(0)
        self.keystore = models.Keystore()
        self.datastore = models.Datastore()

        # self._poller = gevent.Greenlet(self._poll_server)

        self._reqs_session = requests.Session()
        self._reqs_session.stream = True
        self._reqs_session.params['ssl'] = 1

        if username:
            self.login(username, password)

    def _maxaction():
        doc = "The _maxaction property."
        def fget(self):
            return self.__maxaction
        def fset(self, value):
            self.__maxaction = value
            #if value and not bool(self._poller):
            #   self._poller.start()
            #elif not value and bool(self._poller):
            #   self._poller.kill(block=False)
        def fdel(self):
            del self.__maxaction
            #self._poller.kill(block=False)

        return locals()
    _maxaction = property(**_maxaction())

    @property
    def root(self):
        if self.datastore.root is None:
            self.init_datastore()
        return self.datastore.root

    @property
    def trash(self):
        if self.datastore.trash is None:
            self.init_datastore()
        return self.datastore.trash

    @property
    def inbox(self):
        if self.datastore.inbox is None:
            self.init_datastore()
        return self.datastore.inbox

    def __str__(self):
        if not self.user:
            return self.__repr__()

        return "%s for user '%s'" % (self.__name__, self.user.username)

    def login(self, username, password):
        self.user = User(self)
        self.user.login(username, password)

    def ephemeral(self):
        self.user = User(self)
        self.user.ephemeral()

    def init_datastore(self):
        req = protocol.FilesRequest()
        res = req.send(self)

        self._maxaction = res['maxaction']
        for data in res['files']:
            meta = models.Meta.for_data(data, self)
            self.datastore.add(meta)

    def download(self, func, file, *args, **kwargs):
        if isinstance(file, basestring):
            (handle, cipher_info) = models.File.parse_download_url(file)

            req = protocol.PublicFileDownloadRequest(handle)
            res = req.send(self)

            file = models.File.deserialize(res.as_dict(), handle=handle,
                session=self, cipher_info=cipher_info)

            req = self._reqs_session.get(res['url'], stream=True,
                params={'sid': None, 'ssl': None})

            func(file, file.decrypt_from_stream(req.raw), *args, **kwargs)
        else:
            file.download(func, *args, **kwargs)

    def download_to_file(self, file, handle = None):
        self.download(self._to_file, file, handle)<commit_msg>Fix imports<commit_after>from __future__ import absolute_import

from Crypto.Cipher import AES
from Crypto.PublicKey import RSA
from Crypto.Util.strxor import strxor
from Crypto.Util.number import bytes_to_long, long_to_bytes
from Crypto.Util import Counter
from Crypto.Random import random

from .utils import b64encode, b64decode

import itertools
import weakref
import datetime
import json
import urlparse
import os
import os.path
import requests
import logging

from . import errors
from . import protocol
from . import transport
from . import utils
from . import errors
from . import protocol

__all__ = [ 'Session', 'File', 'User' ]

class Session(object):
    def __init__(self, username = None, password = <PASSWORD>):
        self.sequence = itertools.count(0)
        self.keystore = Keystore()
        self.datastore = Datastore()

        # self._poller = gevent.Greenlet(self._poll_server)

        self._reqs_session = requests.Session()
        self._reqs_session.stream = True
        self._reqs_session.params['ssl'] = 1

        if username:
            self.login(username, password)

    def _maxaction():
        doc = "The _maxaction property."
        def fget(self):
            return self.__maxaction
        def fset(self, value):
            self.__maxaction = value
            #if value and not bool(self._poller):
            #   self._poller.start()
            #elif not value and bool(self._poller):
            #   self._poller.kill(block=False)
        def fdel(self):
            del self.__maxaction
            #self._poller.kill(block=False)

        return locals()
    _maxaction = property(**_maxaction())

    @property
    def root(self):
        if self.datastore.root is None:
            self.init_datastore()
        return self.datastore.root

    @property
    def trash(self):
        if self.datastore.trash is None:
            self.init_datastore()
        return self.datastore.trash

    @property
    def inbox(self):
        if self.datastore.inbox is None:
            self.init_datastore()
        return self.datastore.inbox

    def __str__(self):
        if not self.user:
            return self.__repr__()

        return "%s for user '%s'" % (self.__name__, self.user.username)

    def login(self, username, password):
        self.user = User(self)
        self.user.login(username, password)

    def ephemeral(self):
        self.user = User(self)
        self.user.ephemeral()

    def init_datastore(self):
        req = protocol.FilesRequest()
        res = req.send(self)

        self._maxaction = res['maxaction']
        for data in res['files']:
            meta = Meta.for_data(data, self)
            self.datastore.add(meta)

    def download(self, func, file, *args, **kwargs):
        if isinstance(file, basestring):
            (handle, cipher_info) = File.parse_download_url(file)

            req = protocol.PublicFileDownloadRequest(handle)
            res = req.send(self)

            file = File.deserialize(res.as_dict(), handle=handle,
                session=self, cipher_info=cipher_info)

            req = self._reqs_session.get(res['url'], stream=True,
                params={'sid': None, 'ssl': None})

            func(file, file.decrypt_from_stream(req.raw), *args, **kwargs)
        else:
            file.download(func, *args, **kwargs)

    def download_to_file(self, file, handle = None):
        self.download(self._to_file, file, handle)
{'fraction_non_alphanumeric': 0.1016705069124424, 'fraction_numerical': 0.0017281105990783411, 'mean_word_length': 2.3944281524926687, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '316025', 'n_tokens_mistral': 2092, 'n_tokens_neox': 2022, 'n_words': 509}