Ejemplo n.º 1
0
    def parse(cls, data: ParseData) -> 'PakObject':
        props = data.info  # type: Property

        if data.is_override:
            # Override doesn't have a name
            group_name = ''
        else:
            group_name = props['Name']

        desc = desc_parse(props, data.id)

        widgets = []  # type: List[Widget]
        multi_widgets = []  # type: List[Widget]

        for wid in props.find_all('Widget'):
            try:
                create_func = WidgetLookup[wid['type']]
            except KeyError:
                LOGGER.warning(
                    'Unknown widget type "{}" in <{}:{}>!',
                    wid['type'],
                    data.pak_id,
                    data.id,
                )
                continue
            try:
                timer_func = WidgetLookupMulti[wid['type']]
            except KeyError:
                timer_func = widget_timer_generic(create_func)

            is_timer = wid.bool('UseTimer')
            use_inf = is_timer and wid.bool('HasInf')
            wid_id = wid['id'].casefold()
            name = wid['Label']
            tooltip = wid['Tooltip', '']
            default = wid.find_key('Default', '')
            values = None  # type: Union[List[Tuple[str, tk.StringVar]], tk.StringVar]

            # Special case - can't be timer, and no values.
            if create_func is widget_item_variant:
                if is_timer:
                    LOGGER.warning("Item Variants can't be timers! ({}.{})",
                                   data.id, wid_id)
                    is_timer = use_inf = False
                # Values remains a dummy None value, we don't use it.
            elif is_timer:
                if default.has_children():
                    defaults = {
                        num: default[num]
                        for num in TIMER_NUM
                        # Exclude infinite if use_inf is False.
                        if use_inf or num != 'inf'
                    }
                else:
                    # All the same.
                    defaults = dict.fromkeys(TIMER_NUM, default.value)
                values = [
                    (num,
                     tk.StringVar(value=CONFIG.get_val(
                         data.id,
                         '{}_{}'.format(wid_id, num),
                         defaults[num],
                     ),
                                  name='itemconf_{}_{}_{}'.format(
                                      data.id, wid_id, num)))
                    for num in TIMER_NUM
                    # Exclude infinite if use_inf is False.
                    if use_inf or num != 'inf'
                ]
            else:
                if default.has_children():
                    raise ValueError(
                        'Can only have multiple defaults for timered widgets!')
                values = tk.StringVar(
                    value=CONFIG.get_val(
                        data.id,
                        wid_id,
                        default.value,
                    ),
                    name='itemconf_{}_{}'.format(data.id, wid_id),
                )

            (multi_widgets if is_timer else widgets).append(
                Widget(
                    wid_id,
                    name,
                    tooltip,
                    create_func,
                    timer_func,
                    wid,
                    values,
                    is_timer,
                    use_inf,
                ))

        group = cls(
            data.id,
            group_name,
            desc,
            widgets,
            multi_widgets,
        )
        CONFIG_ORDER.append(group)

        # If we are new, write our defaults to config.
        CONFIG.save_check()

        return group
Ejemplo n.º 2
0
    def parse(cls, data: ParseData) -> 'PakObject':
        props = data.info  # type: Property

        if data.is_override:
            # Override doesn't have a name
            group_name = ''
        else:
            group_name = props['Name']

        desc = desc_parse(props, data.id)

        widgets = []  # type: List[Widget]
        multi_widgets = []  # type: List[Widget]

        for wid in props.find_all('Widget'):
            try:
                create_func = WidgetLookup[wid['type']]
            except KeyError:
                LOGGER.warning(
                    'Unknown widget type "{}" in <{}:{}>!',
                    wid['type'],
                    data.pak_id,
                    data.id,
                )
                continue
            try:
                timer_func = WidgetLookupMulti[wid['type']]
            except KeyError:
                timer_func = widget_timer_generic(create_func)

            is_timer = wid.bool('UseTimer')
            use_inf = is_timer and wid.bool('HasInf')
            wid_id = wid['id'].casefold()
            name = wid['Label']
            tooltip = wid['Tooltip', '']
            default = wid.find_key('Default', '')
            values = None  # type: Union[List[Tuple[str, tk.StringVar]], tk.StringVar]

            # Special case - can't be timer, and no values.
            if create_func is widget_item_variant:
                if is_timer:
                    LOGGER.warning("Item Variants can't be timers! ({}.{})", data.id, wid_id)
                    is_timer = use_inf = False
                # Values remains a dummy None value, we don't use it.
            elif is_timer:
                if default.has_children():
                    defaults = {
                        num: default[num]
                        for num in TIMER_NUM
                        # Exclude infinite if use_inf is False.
                        if use_inf or num != 'inf'
                    }
                else:
                    # All the same.
                    defaults = dict.fromkeys(TIMER_NUM, default.value)
                values = [
                    (num, tk.StringVar(
                        value=CONFIG.get_val(
                            data.id,
                            '{}_{}'.format(wid_id, num),
                            defaults[num],
                        ),
                        name='itemconf_{}_{}_{}'.format(data.id, wid_id, num)
                    ))
                    for num in TIMER_NUM
                    # Exclude infinite if use_inf is False.
                    if use_inf or num != 'inf'
                ]
            else:
                if default.has_children():
                    raise ValueError(
                        'Can only have multiple defaults for timered widgets!'
                    )
                values = tk.StringVar(
                    value=CONFIG.get_val(
                        data.id,
                        wid_id,
                        default.value,
                    ),
                    name='itemconf_{}_{}'.format(data.id, wid_id),
                )

            (multi_widgets if is_timer else widgets).append(Widget(
                wid_id,
                name,
                tooltip,
                create_func,
                timer_func,
                wid,
                values,
                is_timer,
                use_inf,
            ))

        group = cls(
            data.id,
            group_name,
            desc,
            widgets,
            multi_widgets,
        )
        CONFIG_ORDER.append(group)

        # If we are new, write our defaults to config.
        CONFIG.save_check()

        return group