from datetime import datetime
from datetime import timedelta
import logging
log = logging.getLogger(__file__)

from ott.utils import json_utils

class Fares(object):

    def __init__(self, fare_url, fare_timeout_mins=1440):
        '''
        '''
        log.info("create an instance of {0}".format(self.__class__.__name__))
        self.fare_url = fare_url
        if fare_timeout_mins:
            self.fare_timeout = fare_timeout_mins
        else:
            self.fare_timeout = 1440

        self.last_update = datetime.now() - timedelta(minutes = (self.fare_timeout+10))
        self.content = []
        self.update()

    def update(self):
        try:
            if self.content is None \
            or len(self.content) < 1 \
            or datetime.now() - self.last_update > timedelta(minutes = self.fare_timeout):
                log.debug("updating the fare content")
                self.last_update = datetime.now()
                c = json_utils.stream_json(self.fare_url)
                if c:
                    self.content = c 
        except Exception, e:
            log.warn("couldn't update the fare content: {}".format(e))
 
    def query(self, fare_type="adult", def_val=None):
        ''' 
        '''
        #import pdb; pdb.set_trace()
        ret_val = def_val
        try:
            self.update()
            for c in self.content:
                if fare_type in c:
                    ret_val = c[fare_type]
                    break
        except:
            log.warn("no fare content for fare_type={0}, using default fare of {1}".format(fare_type, def_val))
        return ret_val


@@ -54,16 +54,51 @@
 medelta%0A
+%0Afrom ott.utils import json_utils%0A%0A
 import l
@@ -143,41 +143,8 @@
 _)%0A%0A
-from ott.utils import json_utils%0A
 %0Acla
@@ -220,35 +220,35 @@
 =1440):%0A        
-'''
+%22%22%22
 %0A        '''%0A   
@@ -232,35 +232,35 @@
     %22%22%22%0A        
-'''
+%22%22%22
 %0A        log.inf
@@ -548,19 +548,17 @@
 (minutes
- = 
+=
 (self.fa
@@ -1046,17 +1046,16 @@
 tent = c
- 
 %0A       
@@ -1071,17 +1071,19 @@
 xception
-,
+ as
  e:%0A    
@@ -1213,19 +1213,19 @@
         
-'''
+%22%22%22
  %0A      
@@ -1230,11 +1230,11 @@
     
-'''
+%22%22%22
 %0A   
@@ -1485,16 +1485,31 @@
   except
+ Exception as e
 :%0A      
@@ -1614,16 +1614,16 @@
 f_val))%0A
+
         
@@ -1637,9 +1637,8 @@
 ret_val%0A
-%0A

fix more py 3.0 incoming code fixedus