示例#1
0
    def read_json(self, json_file):
        '''
        read the data from a json file to create the movie list
        keyword arguments:
        json_file (string) the path to the json file
        '''
        def parse_params(json_element):
            '''
            returns an array of objects to compose a media with ease
            keywords arguments:
            json_element (Object) -- a python object converted from a json
            element

            attributes:
            title    (string)  -- the media title
            summary  (string)  -- the media summary
            url      (string)  -- the media url
            featured (boolean) -- if the media is featured or not.
            Default false
            '''
            def evaluate(param, else_value=None):
                '''
                check if the given param has value to return or returns an
                optional value if it has not

                keyword arguments:
                param      (string)  -- the attribute name from the jsn_element
                else_value (variant) -- the value to be used if the attribute
                has no value. defautl None
                '''
                if param not in json_element:
                    return else_value

                return json_element[param]

            title = evaluate('title')
            summary = evaluate('description')
            url = evaluate('url')
            featured = evaluate('featured', False)
            return title, url, summary, featured

        json_file = json.load(open(json_file, 'r'))
        for json_nodes in json_file:
            movie = Movie(json_nodes['title'],
                          json_nodes['sinopse'], json_nodes['year'])
            if 'trailers' in json_nodes:
                for trailer in json_nodes['trailers']:
                    attr = parse_params(trailer)
                    movie.add_trailer(attr[0], attr[1], attr[3])
            if 'posters' in json_nodes:
                for poster in json_nodes['posters']:
                    attr = parse_params(poster)
                    movie.add_poster(attr[0], attr[1], attr[2])
            self.add(movie)