# Path: magnate/order.py
# class Order:
#     """Information needed to complete a transaction.
# 
#     * :py:attr:`location` The location at which the sale is occuring
#     * :py:attr:`commodity`: the name of the commodity being bought or sold
#     * :py:attr:`price`: the price at which the user is agreeing to buy or sell
#     * :py:attr:`hold_quantity`: The amount of the commodity to place in or
#         take from the ship's hold
#     * :py:attr:`warehouse_quantity`: The amount of the commodity to place in
#         or take from the player's warehouse space at the location
#     * :py:attr:`buy`: if True, the user is asking to buy the commodity.
#         Otherwise, the user is selling the commodity
#     * :py:attr:`status`: Status of the transaction.  May be any one of the EnumTypes
#     """
#     location = attr.ib(validator=attr.validators.instance_of(str))
#     commodity = attr.ib(validator=attr.validators.instance_of(str))
#     price = attr.ib(validator=attr.validators.instance_of(int))
#     hold_quantity = attr.ib(validator=attr.validators.instance_of(int), default=0)
#     warehouse_quantity = attr.ib(validator=attr.validators.instance_of(int), default=0)
#     buy = attr.ib(validator=attr.validators.instance_of(bool), default=True)
#     status = attr.ib(default=OrderStatusType.presale,
#                      validator=partial(enum_validator, OrderStatusType),
#                      convert=partial(enum_converter, OrderStatusType))
# 
# Path: magnate/ui/urwid/abcwidget.py
# class ABCWidget(urwid.WidgetMeta, ABCMeta):
#     """Combine ABCMeta and Widgetmeta together so that we can have a metaclass with both"""
#     pass
# 
# Path: magnate/ui/urwid/message_win.py
# class MessageWindow(urwid.WidgetWrap):
#     _MAX_MESSAGES = 3
#     _MIN_TIME_BETWEEN_MESSAGES = 0.7
#     def __init__(self, pubpen):
#     def height(self):
#     def handle_market_event(self, location, commodity, price, msg):
#     def add_message(self, msg, severity=MsgType.info):
# 
# Path: magnate/ui/urwid/numbers.py
# def format_number(number, max_chars=7):
#     """
#     Format a number in a human readable form.  This adds locale-specific separators.
# 
#     If the number is too long, se scientific notation.
# 
#     :kwarg max_chars: The maximum number of characters a number can take on
#         the screen before it is turned into scientific notation.
#     """
#     formatted_number = locale.format('%d', number, grouping=True)
#     if len(formatted_number) > max_chars:
#         formatted_number = '{:.1E}'.format(number)
# 
#     return formatted_number


from abc import abstractmethod
from ...order import Order
from .abcwidget import ABCWidget
from .message_win import MsgType
from .numbers import format_number
import urwid
                    quantity = revert_amount
                self.quantity.set_edit_text('{}'.format(quantity))
        else:
            if quantity > self.max_sell_quantity:
                valid = False
                if revert_amount is None:
                    quantity = self.max_sell_quantity
                else:
                    quantity = revert_amount
                self.quantity.set_edit_text('{}'.format(quantity))

        if self.order is not None:
            total_sale = quantity * self.order.price
            self.sale_info.set_text('Total Sale: ${}'.format(format_number(total_sale)))
        return valid

    def handle_quantity_change(self, edit_widget, old_text):
        """Update total sale as the edit widget is changed"""
        if not old_text:
            old_text = 0
        was_valid = self.validate_quantity(revert_amount=int(old_text))

        if not was_valid:
            if self.buy_button.state:
                msg_args = ('buy', self.max_buy_quantity, self.order.commodity)
            else:
                msg_args = ('sell', self.max_sell_quantity, self.order.commodity)

            self.pubpen.publish('ui.urwid.message',
                                'Cannot {} more than {} {} at this time'.format(*msg_args),

# Path: magnate/order.py
# class Order:
#     """Information needed to complete a transaction.
# 
#     * :py:attr:`location` The location at which the sale is occuring
#     * :py:attr:`commodity`: the name of the commodity being bought or sold
#     * :py:attr:`price`: the price at which the user is agreeing to buy or sell
#     * :py:attr:`hold_quantity`: The amount of the commodity to place in or
#         take from the ship's hold
#     * :py:attr:`warehouse_quantity`: The amount of the commodity to place in
#         or take from the player's warehouse space at the location
#     * :py:attr:`buy`: if True, the user is asking to buy the commodity.
#         Otherwise, the user is selling the commodity
#     * :py:attr:`status`: Status of the transaction.  May be any one of the EnumTypes
#     """
#     location = attr.ib(validator=attr.validators.instance_of(str))
#     commodity = attr.ib(validator=attr.validators.instance_of(str))
#     price = attr.ib(validator=attr.validators.instance_of(int))
#     hold_quantity = attr.ib(validator=attr.validators.instance_of(int), default=0)
#     warehouse_quantity = attr.ib(validator=attr.validators.instance_of(int), default=0)
#     buy = attr.ib(validator=attr.validators.instance_of(bool), default=True)
#     status = attr.ib(default=OrderStatusType.presale,
#                      validator=partial(enum_validator, OrderStatusType),
#                      convert=partial(enum_converter, OrderStatusType))
# 
# Path: magnate/ui/urwid/abcwidget.py
# class ABCWidget(urwid.WidgetMeta, ABCMeta):
#     """Combine ABCMeta and Widgetmeta together so that we can have a metaclass with both"""
#     pass
# 
# Path: magnate/ui/urwid/message_win.py
# class MessageWindow(urwid.WidgetWrap):
#     _MAX_MESSAGES = 3
#     _MIN_TIME_BETWEEN_MESSAGES = 0.7
#     def __init__(self, pubpen):
#     def height(self):
#     def handle_market_event(self, location, commodity, price, msg):
#     def add_message(self, msg, severity=MsgType.info):
# 
# Path: magnate/ui/urwid/numbers.py
# def format_number(number, max_chars=7):
#     """
#     Format a number in a human readable form.  This adds locale-specific separators.
# 
#     If the number is too long, se scientific notation.
# 
#     :kwarg max_chars: The maximum number of characters a number can take on
#         the screen before it is turned into scientific notation.
#     """
#     formatted_number = locale.format('%d', number, grouping=True)
#     if len(formatted_number) > max_chars:
#         formatted_number = '{:.1E}'.format(number)
# 
#     return formatted_number


# Path: magnate/ui/urwid/order_dialog.py
from abc import abstractmethod
from ...order import Order
from .abcwidget import ABCWidget
from .message_win import MsgType
from .numbers import format_number
import urwid

                    quantity = revert_amount
                self.quantity.set_edit_text('{}'.format(quantity))
        else:
            if quantity > self.max_sell_quantity:
                valid = False
                if revert_amount is None:
                    quantity = self.max_sell_quantity
                else:
                    quantity = revert_amount
                self.quantity.set_edit_text('{}'.format(quantity))

        if self.order is not None:
            total_sale = quantity * self.order.price
            self.sale_info.set_text('Total Sale: ${}'.format(format_number(total_sale)))
        return valid

    def handle_quantity_change(self, edit_widget, old_text):
        """Update total sale as the edit widget is changed"""
        if not old_text:
            old_text = 0
        was_valid = self.validate_quantity(revert_amount=int(old_text))

        if not was_valid:
            if self.buy_button.state:
                msg_args = ('buy', self.max_buy_quantity, self.order.commodity)
            else:
                msg_args = ('sell', self.max_sell_quantity, self.order.commodity)

            self.pubpen.publish('ui.urwid.message',
                                'Cannot {} more than {} {} at this time'.format(*msg_args),

                                severity=MsgType.error)