Exemplo n.º 1
0
def filter_brands_by_code(root, con_log, code):
    """Remove brands with brand code different than code.

    Removes BrandPrograms and adds an error message if there are
    no brands left.
    """
    parent_map = get_parent_map(root)
    brands = []
    brands_removed = 0
    for brand in root.findall(anywhere('Brand')):
        brand_code = brand.findtext(add_xmlns('Code'))
        if brand_code != code:
            parent_map[brand].remove(brand)
            brands_removed += 1
        else:
            brands.append(brand)
    if not brands:
        for program in root.findall(anywhere('BrandProgram')):
            parent_map[program].remove(program)
        mkt_response = root.find(anywhere('MarketResponse'))
        # TODO Update when BS team specify error message for no matching brands
        message = ET.SubElement(mkt_response,
                                'Message',
                                messageCode='Warning',
                                failCode='3001')
        message.text = 'No programs found'
    output = 'Filtered out {:d} non-{:s} brands, {:d} left'.format(
        brands_removed, code, len(brands))
    con_log(log_category='plg_out', text=output)
    return root
Exemplo n.º 2
0
def add_fake_t166_item(root, con_log, program, brand, ancillaries, container,
                       item):
    """Add Table 166 info to brand in given program"""
    program_node = root.find(
        anywhere('BrandProgram[@programID="{}"]'.format(program)))
    if program_node is None:
        con_log(log_category='plg_out',
                text='Program {} not found in BS response'.format(program))
        return root

    brand_node = None
    for bnode in program_node.findall(anywhere('Brand')):
        code = bnode.find(anywhere('Code'))
        if code.text == brand:
            brand_node = bnode
            break

    if brand_node is None:
        con_log(log_category='plg_out',
                text='Brand {} not found in program {}'.format(brand, program))
        return root

    create_table_166_node(brand_node, ancillaries, container, item)
    output = 'Found program {} and brand {}, inserting ancillaries {}'.format(
        program, brand, ancillaries)
    con_log(log_category='plg_out', text=output)
    return root
Exemplo n.º 3
0
def find_brands(root, brand_code=None):
    """ Returns all Brand nodes. If brand_code is set
         - returns only the brands with given Brand Code """
    if brand_code is None:
        return root.findall(anywhere('Brand'))
    else:
        # [tag=smth] is an undocumented (or rater not clearly documented) feature
        # of ET but it works as expected
        return root.findall(
            anywhere('Brand[{:s}="{:s}"]'.format(add_xmlns('Code'),
                                                 brand_code)))
Exemplo n.º 4
0
def add_fake_MarketDataSource(root, con_log, data_source, market_id=None):
    """ Add a fake dataSource attribute to all ProgramIdList elements"""
    updated_element_count = 0
    if market_id:
        search_pattern = 'MarketResponse[@marketID="{:s}"]/'.format(market_id)
        search_pattern += add_xmlns('ProgramIdList')
        search_pattern = anywhere(search_pattern)
    else:
        search_pattern = anywhere('ProgramIdList')
    for program_list in root.findall(search_pattern):
        program_list.set('dataSource', data_source)
        updated_element_count += 1
    output = 'Set dataSource for market {:s} to ' \
             '"{:s}" in {:d} elements'.format(market_id or '*',
                                              data_source,
                                              updated_element_count)
    con_log(log_category='plg_out', text=output)
    return root
Exemplo n.º 5
0
def add_fake_BrandProgramDataSource(root,
                                    con_log,
                                    data_source,
                                    program_id=None):
    """ Add a fake dataSource attribute to all BrandProgram elements"""
    updated_element_count = 0
    if program_id:
        search_pattern = 'BrandProgram[@programID="{:s}"]'.format(program_id)
        search_pattern = anywhere(search_pattern)
    else:
        search_pattern = anywhere('BrandProgram')
    for program_list in root.findall(search_pattern):
        program_list.set('dataSource', data_source)
        updated_element_count += 1
    output = 'Set dataSource for BrandProgram {:s} to ' \
             '"{:s}" in {:d} elements'.format(program_id or '*',
                                              data_source,
                                              updated_element_count)
    con_log(log_category='plg_out', text=output)
    return root
Exemplo n.º 6
0
def extract_brand_code(root):
    """Get brand code and remove it from root.

    Modifies root by removing the BrandCode element.
    Returns brand code or None if BrandCode element wasn't found.

    TODO: This is only valid for a single market. Pricing can send
    multiple markets in a single request.
    """
    bc_elem = root.find(anywhere('BrandCode'))
    if bc_elem is not None:
        parent_map = get_parent_map(root)
        parent_map[bc_elem].remove(bc_elem)
        return bc_elem.text
Exemplo n.º 7
0
def add_fake_t186_itemno(root, con_log, offset):
    """Add an itemno to each brand.

    The value of itemno = Brand/Tier + offset.
    """
    updated_element_count = 0
    for brand in root.findall(anywhere('Brand')):
        tier = brand.findtext(add_xmlns('Tier'))
        if tier:
            itemno = int(tier) + offset
            itemno_elem = ET.SubElement(brand, 'CarrierFlightItemNum')
            itemno_elem.text = str(itemno)
        updated_element_count += 1
    output = 'Added itemno to {:d} elements'.format(updated_element_count)
    con_log(log_category='plg_out', text=output)
    return root
Exemplo n.º 8
0
def find_brand_codes(root):
    return root.findall(anywhere('Brand/{}'.format(add_xmlns('Code'))))