<commit_before>import flask
import dropbox


class DropboxSession(dropbox.session.DropboxSession):

    def link(self, force=False, url=None):
        # Only build a new request token if we don't have one or are forced to
        # do so.
        if force or not self.request_token:
            print 'obtaining request token'
            self.obtain_request_token()
        else:
            try:
                print 'obtaining access token'
                self.obtain_access_token()
            except dropbox.rest.ErrorResponse, exception:
                if exception.status == 401:
                    #self.unlink()
                    pass
                else:
                    raise

        return self.build_authorize_url(
            self.request_token, url or flask.request.base_url)

    def obtain_request_token(self):
        '''
        This updates the DropboxSession so we have to notify Flask that our
        session has changed.
        '''
        print 'building request token'
        request_token = dropbox.session.DropboxSession.obtain_request_token(
            self)

        flask.session['request_token'] = self.request_token.key
        flask.session['request_token_secret'] = self.request_token.secret
        return request_token

    def obtain_access_token(self, request_token=None):
        print 'building access token'
        '''
        This updates the DropboxSession so we have to notify Flask that our
        session has changed.
        '''
        access_token = dropbox.session.DropboxSession.obtain_access_token(
            self, request_token)

        flask.session['access_token'] = self.token.key
        flask.session['access_token_secret'] = self.token.secret
        return access_token

    def unlink(self):
        self.request_token = None
        flask.session['request_token'] = None
        flask.session['request_token_secret'] = None
        flask.session['access_token'] = None
        flask.session['access_token_secret'] = None
        return dropbox.session.DropboxSession.unlink(self)

    def __init__(self):
        from main import app
        dropbox.session.DropboxSession.__init__(
            self,
            consumer_key=app.config['APP_KEY'],
            consumer_secret=app.config['APP_SECRET'],
            access_type=app.config['ACCESS_TYPE'],
        )

        data = flask.session

        if data.get('request_token') and data.get('request_token_secret'):
            # Set the request token if available
            self.set_request_token(
                request_token=data['request_token'],
                request_token_secret=data['request_token_secret'],
            )

        if data.get('access_token') and data.get('access_token_secret'):
            # Set the access token if available
            self.set_token(
                access_token=data['access_token'],
                access_token_secret=data['access_token_secret'],
            )
<commit_msg>added serializable dropbox sessions for celery<commit_after>import flask
import dropbox


class DropboxSession(dropbox.session.DropboxSession):

    def __init__(self, session=None):
        from main import app
        dropbox.session.DropboxSession.__init__(
            self,
            consumer_key=app.config['APP_KEY'],
            consumer_secret=app.config['APP_SECRET'],
            access_type=app.config['ACCESS_TYPE'],
        )

        session = self.session = session or flask.session
        if session.get('request_token') and session.get(
                'request_token_secret'):
            # Set the request token if available
            self.set_request_token(
                request_token=session['request_token'],
                request_token_secret=session['request_token_secret'],
            )

        if session.get('access_token') and session.get('access_token_secret'):
            # Set the access token if available
            self.set_token(
                access_token=session['access_token'],
                access_token_secret=session['access_token_secret'],
            )

    def link(self, force=False, url=None):
        # Only build a new request token if we don't have one or are forced to
        # do so.
        if force or not self.request_token:
            self.obtain_request_token()
        else:
            try:
                self.obtain_access_token()
            except dropbox.rest.ErrorResponse, exception:
                if exception.status == 401:
                    #self.unlink()
                    pass
                else:
                    raise

        return self.build_authorize_url(
            self.request_token, url or flask.request.base_url)

    def obtain_request_token(self):
        '''
        This updates the DropboxSession so we have to notify Flask that our
        session has changed.
        '''
        request_token = dropbox.session.DropboxSession.obtain_request_token(
            self)

        self.session['request_token'] = self.request_token.key
        self.session['request_token_secret'] = self.request_token.secret
        return request_token

    def obtain_access_token(self, request_token=None):
        '''
        This updates the DropboxSession so we have to notify Flask that our
        session has changed.
        '''
        access_token = dropbox.session.DropboxSession.obtain_access_token(
            self, request_token)

        self.session['access_token'] = self.token.key
        self.session['access_token_secret'] = self.token.secret
        return access_token

    def unlink(self):
        self.request_token = None
        self.session['request_token'] = None
        self.session['request_token_secret'] = None
        self.session['access_token'] = None
        self.session['access_token_secret'] = None
        return dropbox.session.DropboxSession.unlink(self)

{'fraction_non_alphanumeric': 0.08248125426039536, 'fraction_numerical': 0.0010224948875255625, 'mean_word_length': 2.111876988335101, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3503681', 'n_tokens_mistral': 1569, 'n_tokens_neox': 1506, 'n_words': 344}