<commit_before>import json
import urllib
import webapp2
import yaml
import config
from datetime import datetime
from base_service import BaseService
from google.appengine.api import urlfetch

class MeruService(BaseService):

    @property
    def name(self):
        return "Meru"

    @property
    def has_api(self):
        return False

    def get_path(self):
        meru_config = config.app_config.get('meru')
        return meru_config.get('data_file')

    """
    Try to match a city from available cities in meru data in the source
    address.
    """
    def find_city(self, mdata, route):
        meru_cities = mdata.keys()
        start_address = route.start_address.lower()

        city = [city for city in meru_cities if city in start_address]
        return (city[0] if city else None)

    """
    File path can be obtained from config. However it is also provided as an
    argument to facilitate testing. Since get_app is not available in unittests or
    maybe there's a better way of doing it.
    """
    def load_data(self):
        stream = open(self.get_path(), 'r')
        return yaml.load(stream)

    """
    AVOID using this method. This method calls the get_fare_by_distance
    to calculate fare. Distance is calculated by method in base service and it's
    an approximation since it doesn't account the variation due to the nature of roads

    Only use this method if you have no other option
    """
    def _get_fare_by_lat_lang(self, src_lat, src_long,
            dst_lat, dst_long):
        approx_distance = self.calculate_distance(src_lat, src_long,
                dst_lat, dst_long)
        result = get_fare_by_distance(approx_distance)
        return result


    """
    Get the rule for given time from the set of rules available
    for meru.
    Input: rules for a service type in a given service
    Example:  rules = (meru -> mumbai -> service_type -> meru_cabs)
    """
    def get_rule_for_given_time(self, rules, time):
        midnight = datetime.strptime('00:00', '%H:%M')
        selected_rule = None
        for rule_index in rules:
            rule = rules.get(rule_index)
            time_from   = datetime.strptime(rule.get('time_from'), '%H:%M')
            time_to     = datetime.strptime(rule.get('time_to'), '%H:%M')
            travel_time = datetime.strptime(datetime.now().strftime("%H:%M"), '%H:%M')
            if (travel_time > time_from and travel_time < time_to):
                selected_rule = rule
                break
        return selected_rule

    def calculate_fare(self, rules, distance, time=datetime.now()):
        self.get_rule_for_given_time(rules, time)

    """
    Returns fare for various service types/vehicle types based on distance
    and city.
    Input: instance of Route class
    Returns: dict containing fair information

    We pass Route object since we need city and distance information
    """
    def _get_fare_by_distance(self, route):
        mdata = self.load_data().get('meru')
        city  = self.find_city(mdata, route)
        if not city:
            return {}

        service_rules_for_city = mdata.get(city).get('service_type')
        distance_in_meters = route.distance

        for service_type in service_rules_for_city:
            self.calculate_fare(service_rules_for_city.get(service_type),
                    distance_in_meters)


    """
    This is a method reserved for future use. Just in case we want to pass<commit_msg>implementing get fare in meru to get the tests passing<commit_after>import json
import urllib
import webapp2
import yaml
import config
from datetime import datetime
from base_service import BaseService
from google.appengine.api import urlfetch

class MeruService(BaseService):

    @property
    def name(self):
        return "Meru"

    @property
    def has_api(self):
        return False

    def get_path(self):
        meru_config = config.app_config.get('meru')
        return meru_config.get('data_file')

    """
    Try to match a city from available cities in meru data in the source
    address.
    """
    def find_city(self, mdata, route):
        meru_cities = mdata.keys()
        start_address = route.start_address.lower()

        city = [city for city in meru_cities if city in start_address]
        return (city[0] if city else None)

    """
    File path can be obtained from config. However it is also provided as an
    argument to facilitate testing. Since get_app is not available in unittests or
    maybe there's a better way of doing it.
    """
    def load_data(self):
        stream = open(self.get_path(), 'r')
        return yaml.load(stream)

    """
    AVOID using this method. This method calls the get_fare_by_distance
    to calculate fare. Distance is calculated by method in base service and it's
    an approximation since it doesn't account the variation due to the nature of roads

    Only use this method if you have no other option
    """
    def _get_fare_by_lat_lang(self, src_lat, src_long,
            dst_lat, dst_long):
        approx_distance = self.calculate_distance(src_lat, src_long,
                dst_lat, dst_long)
        result = get_fare_by_distance(approx_distance)
        return result


    """
    Get the rule for given time from the set of rules available
    for meru.
    Input: rules for a service type in a given service
    Example:  rules = (meru -> mumbai -> service_type -> meru_cabs)
    """
    def get_rule_for_given_time(self, rules, time):
        midnight = datetime.strptime('00:00', '%H:%M')
        selected_rule = None
        for rule_index in rules:
            rule = rules.get(rule_index)
            time_from   = datetime.strptime(rule.get('time_from'), '%H:%M')
            time_to     = datetime.strptime(rule.get('time_to'), '%H:%M')
            travel_time = datetime.strptime(datetime.now().strftime("%H:%M"), '%H:%M')
            if (travel_time > time_from and travel_time < time_to):
                selected_rule = rule
                break
        return selected_rule

    def calculate_fare(self, rules, distance, time=datetime.now()):
        self.get_rule_for_given_time(rules, time)

    """
    Returns fare for various service types/vehicle types based on distance
    and city.
    Input: instance of Route class
    Returns: dict containing fair information

    We pass Route object since we need city and distance information
    """
    def _get_fare_by_distance(self, route):
        mdata = self.load_data().get('meru')
        city  = self.find_city(mdata, route)
        if not city:
            return {}

        service_rules_for_city = mdata.get(city).get('service_type')
        distance_in_meters = route.distance

        for service_type in service_rules_for_city:
            self.calculate_fare(service_rules_for_city.get(service_type),
                    distance_in_meters)
        #TODO(goyalankit) finish this method
        return {}

    def get_fare(self, route):
        return self._get_fare_by_distance(self, route)

    """
    This is a method reserved for future use. Just in case we want to pass
{'fraction_non_alphanumeric': 0.08061339335959482, 'fraction_numerical': 0.0016882386043894203, 'mean_word_length': 2.655012853470437, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '4292250', 'n_tokens_mistral': 2088, 'n_tokens_neox': 2018, 'n_words': 665}