예제 #1
0
    def post(self):
        args = self.reqparse.parse_args()

        if any([not args.data, not args.nesting_levels]):
            raise RequirementParameterMissing(args)

        try:
            p = Parser(args.data, args.nesting_levels)
            output = p.parse()
            response = {'data': output, 'message': None, 'status': 'success'}
        except Exception as e:
            raise ApiException(str(e))

        return jsonify(response)
def locate(query):
    """ Set locate() function.

    Receive a string containing user query.
    Use Parser, GmapsApiRequest and MediaWikiApiRequest classes.
    Return a tuple of 8 variables via return_infos() method.

    """
    try:
        # Extract relevant words of user query.
        parser = Parser(query)
        logging.debug("Here are relevant words selected by parser : %s",
                      parser.query_relevant_words)

        if not parser.query_relevant_words:
            raise ParserError("Parser didn't find any relevant word ...")

    except ParserError as error:
        logging.warning("ParserError : %s", error)
        # If no relevant words found, error is True. Neither address,
        # nor summary are returned. End of process.
        return _return_infos(error=True,
                             message=random.choice(PARSER_FAILURE_MESSAGES))

    try:
        # Ask data to Google Maps Geocoding API.
        gmaps_api_request = GmapsApiRequest(parser.query_relevant_words)
        address = gmaps_api_request.address
        lat = gmaps_api_request.lat
        lng = gmaps_api_request.lng
        logging.debug(
            "Here are latitude and longitude returned by GoogleMaps \
API : %s, %s", lat, lng)

    except GmapsApiError as error:
        logging.warning("GmapsApiError : %s", error)
        # If there is no data returned from Google Maps Geocoding API,
        # then error becomes true. Neither address, nor summary are
        # returned. End of process.
        return _return_infos(error=True,
                             message=random.choice(ADDRESS_FAILURE_MESSAGES))

    try:
        # Ask data to MediaWiki API.
        mediawiki_api_request = MediaWikiApiRequest(lat, lng)
        summary = mediawiki_api_request.summary

    except MediaWikiApiError as error:
        logging.warning("MediaWikiError : %s", error)
        # If there is no data returned from MediaWiki API, then only
        # Google Maps data are returned.
        return _return_infos(
            address=address,
            lat=lat,
            lng=lng,
            summary_message=random.choice(SUMMARY_FAILURE_MESSAGES))

    # If Parser, GmapsApiRequest & MediaWikiApiRequest return data, then
    # all data are returned.
    return _return_infos(address=address, lat=lat, lng=lng, summary=summary)
예제 #3
0
 def setUp(self):
     self.data = [{
         "country": "US",
         "city": "Boston",
         "currency": "USD",
         "amount": 100
     }, {
         "country": "FR",
         "city": "Paris",
         "currency": "EUR",
         "amount": 20
     }, {
         "country": "FR",
         "city": "Lyon",
         "currency": "EUR",
         "amount": 11.4
     }]
     self.nesting_levels = ['currency', 'country', 'city']
     self.parser = Parser(self.data, self.nesting_levels)
    def test_parser(self):
        """ Set test_parser() method.

        Test that relevant words are extracted from the initial query.

        """
        parser = Parser("Salut GrandPy ! Est-ce que tu connais, par hasard, \
l'adresse du centre équestre Cantegril ?")
        assert parser.query_relevant_words == [
            "centre", "équestre", "Cantegril"
        ]
예제 #5
0
class ParserTestCase(unittest.TestCase):
    def setUp(self):
        self.data = [{
            "country": "US",
            "city": "Boston",
            "currency": "USD",
            "amount": 100
        }, {
            "country": "FR",
            "city": "Paris",
            "currency": "EUR",
            "amount": 20
        }, {
            "country": "FR",
            "city": "Lyon",
            "currency": "EUR",
            "amount": 11.4
        }]
        self.nesting_levels = ['currency', 'country', 'city']
        self.parser = Parser(self.data, self.nesting_levels)

    def test_valid_parse(self):
        output = self.parser.parse()
        expected_output = {
            'USD': {
                'US': {
                    'Boston': [{
                        'amount': 100
                    }]
                }
            },
            'EUR': {
                'FR': {
                    'Paris': [{
                        'amount': 20
                    }],
                    'Lyon': [{
                        'amount': 11.4
                    }]
                }
            }
        }
        self.assertEqual(output, expected_output)

    def test_invalid_nesting_levels(self):
        invalid_levels = ['city', 'a']
        with self.assertRaises(ValueError):
            Parser(self.data, invalid_levels)
예제 #6
0
 def test_invalid_nesting_levels(self):
     invalid_levels = ['city', 'a']
     with self.assertRaises(ValueError):
         Parser(self.data, invalid_levels)
예제 #7
0
def main(data, nesting_levels):
    p = Parser(data, nesting_levels)
    output = p.parse()
    print(json.dumps(output, indent=2, sort_keys=True))