def parse_from_dict(json_dict):
    """
    Given a Unified Uploader message, parse the contents and return a
    MarketHistoryList instance.

    :param dict json_dict: A Unified Uploader message as a dict.
    :rtype: MarketOrderList
    :returns: An instance of MarketOrderList, containing the orders
        within.
    """
    history_columns = json_dict['columns']

    history_list = MarketHistoryList(
        upload_keys=json_dict['uploadKeys'],
        history_generator=json_dict['generator'],
    )

    for rowset in json_dict['rowsets']:
        generated_at = parse_datetime(rowset['generatedAt'])
        region_id = rowset['regionID']
        type_id = rowset['typeID']
        history_list.set_empty_region(region_id, type_id, generated_at)

        for row in rowset['rows']:
            history_kwargs = _columns_to_kwargs(
                SPEC_TO_KWARG_CONVERSION, history_columns, row)
            historical_date = parse_datetime(history_kwargs['historical_date'])

            history_kwargs.update({
                'type_id': type_id,
                'region_id': region_id,
                'historical_date': historical_date,
                'generated_at': generated_at,
            })

            history_list.add_entry(MarketHistoryEntry(**history_kwargs))

    return history_list
def parse_from_dict(json_dict):
    """
    Given a Unified Uploader message, parse the contents and return a
    MarketHistoryList instance.

    :param dict json_dict: A Unified Uploader message as a dict.
    :rtype: MarketOrderList
    :returns: An instance of MarketOrderList, containing the orders
        within.
    """
    history_columns = json_dict['columns']

    history_list = MarketHistoryList(
        upload_keys=json_dict['uploadKeys'],
        history_generator=json_dict['generator'],
    )

    for rowset in json_dict['rowsets']:
        generated_at = parse_datetime(rowset['generatedAt'])
        region_id = rowset['regionID']
        type_id = rowset['typeID']
        history_list.set_empty_region(region_id, type_id, generated_at)

        for row in rowset['rows']:
            history_kwargs = _columns_to_kwargs(
                SPEC_TO_KWARG_CONVERSION, history_columns, row)
            historical_date = parse_datetime(history_kwargs['historical_date'])

            history_kwargs.update({
                'type_id': type_id,
                'region_id': region_id,
                'historical_date': historical_date,
                'generated_at': generated_at,
            })

            history_list.add_entry(MarketHistoryEntry(**history_kwargs))

    return history_list
def serialize_history(doc_dict, region_data):
    """
    Serializes a GetOldPriceHistory cache file's contents.

    :param dict doc_dict: The parsed cache document in dict form.
    :rtype: str
    :returns: The UUDIF serialized JSON message.
    """
    hist_list = MarketHistoryList(
        order_generator=ORDER_GENERATOR,
        upload_keys=UPLOAD_KEYS,
    )
    # timezone-aware datetime.
    generated_at = now_dtime_in_utc()
    region_id = region_data[2]
    type_id = region_data[3]

    for hist_item in doc_dict['lret']:
        #print hist_item
        entry = MarketHistoryEntry(
            type_id=type_id,
            region_id=region_id,
            historical_date=wintime_to_datetime(hist_item['historyDate']),
            num_orders=hist_item['orders'],
            low_price=hist_item['lowPrice'],
            high_price=hist_item['highPrice'],
            average_price=hist_item['avgPrice'],
            total_quantity=hist_item['volume'],
            generated_at=generated_at,
        )
        hist_list.add_entry(entry)

    if len(hist_list) is 0:
        # There were no orders for this item+region combo.
        hist_list.set_empty_region(region_id, type_id, generated_at)

    return encode_to_json(hist_list)
def serialize_history(doc_dict, region_data):
    """
    Serializes a GetOldPriceHistory cache file's contents.

    :param dict doc_dict: The parsed cache document in dict form.
    :rtype: str
    :returns: The UUDIF serialized JSON message.
    """
    hist_list = MarketHistoryList(
        order_generator=ORDER_GENERATOR,
        upload_keys=UPLOAD_KEYS,
    )
    # timezone-aware datetime.
    generated_at = now_dtime_in_utc()
    region_id = region_data[2]
    type_id = region_data[3]

    for hist_item in doc_dict['lret']:
        #print hist_item
        entry = MarketHistoryEntry(
            type_id=type_id,
            region_id=region_id,
            historical_date=wintime_to_datetime(hist_item['historyDate']),
            num_orders=hist_item['orders'],
            low_price=hist_item['lowPrice'],
            high_price=hist_item['highPrice'],
            average_price=hist_item['avgPrice'],
            total_quantity=hist_item['volume'],
            generated_at=generated_at,
        )
        hist_list.add_entry(entry)

    if len(hist_list) is 0:
        # There were no orders for this item+region combo.
        hist_list.set_empty_region(region_id, type_id, generated_at)

    return encode_to_json(hist_list)