# Path: utils/deferred.py
# class deferred(object):
#     @staticmethod
#     def defer(*args, **kwargs):
#         args1 = args[1:]
#         args[0](*args1, **kwargs)


import session, time, datetime, logging, hashlib, json
import tweepy
import settings, twitter_settings
from collections import defaultdict
from inspect import currentframe, getframeinfo
from tweepy import StreamListener
from tweepy.api import API
from google.appengine.api import users
from google.appengine.ext import ndb
from utils.deferred import deferred
from models import *
from lsh_matrix import *
        """Called once connected to streaming server.
        This will be invoked once a successful response
        is received from the server. Allows the listener
        to perform some work prior to entering the read loop.
        """
        self.tweets = list()
        self.cursor = 0
        dui = ndb.Key(urlsafe = self.duik).get()
        if dui:
            dui.indicate_fetch_begun()
        frameinfo = getframeinfo(currentframe())
        logging.debug('file %s, line %s Twitter connection ready', frameinfo.filename, frameinfo.lineno+1)

    def launch_lsh_calc(self):
        # store tweets and kick off run_lsh

        tw_from = self.cursor
        tw_till = len(self.tweets)
        dui = ndb.Key(urlsafe = self.duik).get()
        dui = dui.extend_tweets(self.tweets[tw_from:tw_till])
        self.cursor = len(self.tweets)

        if not self.matrix:
            Matrix._initialize()
            MatrixRow._initialize()
            self.matrix = Matrix.create(filename = dui.filename(), source = 'tweets', file_key = self.duik)
            if self.matrix:
                dui = dui.set_ds_key(self.matrix.ds_key)
        if self.matrix:
            timestamp = datetime.datetime.utcnow().isoformat()

# Path: utils/deferred.py
# class deferred(object):
#     @staticmethod
#     def defer(*args, **kwargs):
#         args1 = args[1:]
#         args[0](*args1, **kwargs)


# Path: read_tweepy.py
import session, time, datetime, logging, hashlib, json
import tweepy
import settings, twitter_settings
from collections import defaultdict
from inspect import currentframe, getframeinfo
from tweepy import StreamListener
from tweepy.api import API
from google.appengine.api import users
from google.appengine.ext import ndb
from utils.deferred import deferred
from models import *
from lsh_matrix import *

        """Called once connected to streaming server.
        This will be invoked once a successful response
        is received from the server. Allows the listener
        to perform some work prior to entering the read loop.
        """
        self.tweets = list()
        self.cursor = 0
        dui = ndb.Key(urlsafe = self.duik).get()
        if dui:
            dui.indicate_fetch_begun()
        frameinfo = getframeinfo(currentframe())
        logging.debug('file %s, line %s Twitter connection ready', frameinfo.filename, frameinfo.lineno+1)

    def launch_lsh_calc(self):
        # store tweets and kick off run_lsh

        tw_from = self.cursor
        tw_till = len(self.tweets)
        dui = ndb.Key(urlsafe = self.duik).get()
        dui = dui.extend_tweets(self.tweets[tw_from:tw_till])
        self.cursor = len(self.tweets)

        if not self.matrix:
            Matrix._initialize()
            MatrixRow._initialize()
            self.matrix = Matrix.create(filename = dui.filename(), source = 'tweets', file_key = self.duik)
            if self.matrix:
                dui = dui.set_ds_key(self.matrix.ds_key)
        if self.matrix:
            timestamp = datetime.datetime.utcnow().isoformat()

            deferred.defer(run_lsh, self.duik, self.tweets[tw_from:tw_till], self.matrix.ds_key, tw_from, timestamp)