Ejemplo n.º 1
0
def _get_card_name(lines: List[str], active_filename: str) -> Optional[str]:
    """
    Returns the name of the card defined by the provided lines

    Parameters
    ----------
    lines : list[str]
        the lines of the card

    Returns
    -------
    card_name : str
        the name of the card

    """
    card_name = lines[0][:8].rstrip('\t, ').split(',')[0].split('\t')[0].strip(
        '*\t ')
    if len(card_name) == 0:
        return None
    if ' ' in card_name or len(card_name) == 0:
        msg = 'card_name=%r\nline=%r in filename=%r is invalid' \
              % (card_name, lines[0], active_filename)
        print(msg)
        raise CardParseSyntaxError(msg)
    return card_name.upper()
Ejemplo n.º 2
0
def expand_tabs(line: str) -> str:
    """expands the tabs; breaks if you mix commas and tabs"""
    line = line.expandtabs()
    if ',' in line:
        line = line.replace('\t', '')
        msg = 'tabs and commas in the same line are not supported...\nline=%r' % line
        raise CardParseSyntaxError(msg)
    return line
Ejemplo n.º 3
0
def to_fields_long(card_lines, card_name):
    # type: (List[str], str) -> List[str]
    """
    Converts a series of lines in a card into string versions of the field.
    Handles large, small, and CSV formatted cards.

    Doesn't consider Nastran's rule about 72 character width fields,
    which is nice when you have poorly formatted BDFs.

    Parameters
    ----------
    lines : List[str]
        the lines of the BDF card object
    card_name : str
        the card_name -> 'GRID'

    Returns
    -------
    fields : List[str]
        the string formatted fields of the card

    .. warning:: this function is used by the reader and isn't intended
                 to be called by a separate process

    .. code-block:: python

      >>> card_lines = ['GRID,1,,1.0,2.0,3.0']
      >>> card_name = 'GRID'
      >>> fields = to_fields_long(lines, card_name)
      >>> fields
      ['GRID', '1', '', '1.0', '2.0', '3.0']
    """
    fields = []  # type: List[str]

    if card_name == 'MONPNT1':
        return _to_fields_mntpnt1(card_lines)

    # first line
    line = card_lines.pop(0)
    if '=' in line:
        msg = 'card_name=%r\nequal signs are not supported...line=%r' % (card_name, line)
        raise CardParseSyntaxError(msg)

    if '\t' in line:
        line = line.expandtabs()
        if ',' in line:
            msg = 'tabs and commas in the same line are not supported...line=%r' % line
            raise CardParseSyntaxError(msg)

    if '*' in line:  # large field
        if ',' in line:  # csv
            new_fields = line.split(',')[:5]
            for i in range(5 - len(new_fields)):
                new_fields.append('')
        else:  # standard
            new_fields = [line[0:8], line[8:24], line[24:40], line[40:56],
                          line[56:72]]
        fields += new_fields
        assert len(fields) == 5, fields
    else:  # small field
        if ',' in line:  # csv
            new_fields = line.split(',')[:9]
            for i in range(9 - len(new_fields)):
                new_fields.append('')
        else:  # standard
            new_fields = [line[0:8], line[8:16], line[16:24], line[24:32],
                          line[32:40], line[40:48], line[48:56], line[56:64],
                          line[64:72]]
        fields += new_fields
        assert len(fields) == 9, fields

    for j, line in enumerate(card_lines): # continuation lines
        if '=' in line and card_name != 'EIGRL':
            msg = 'card_name=%r\nequal signs are not supported...line=%r' % (card_name, line)
            raise CardParseSyntaxError(msg)
        if '\t' in line:
            line = line.expandtabs()
            if ',' in line:
                msg = 'tabs and commas in the same line are not supported...line=%r' % line
                raise CardParseSyntaxError(msg)

        if '*' in line:  # large field
            if ',' in line:  # csv
                new_fields = line.split(',')[1:5]
                for i in range(4 - len(new_fields)):
                    new_fields.append('')
            else:  # standard
                new_fields = [line[8:24], line[24:40], line[40:56], line[56:72]]
            assert len(new_fields) == 4, new_fields
        else:  # small field
            if ',' in line:  # csv
                new_fields = line.split(',')[1:9]
                for i in range(8 - len(new_fields)):
                    new_fields.append('')
            else:  # standard
                new_fields = [line[8:16], line[16:24], line[24:32],
                              line[32:40], line[40:48], line[48:56],
                              line[56:64], line[64:72]]
            if len(new_fields) != 8:
                nfields = len(new_fields)
                msg = 'nFields=%s new_fields=%s' % (nfields, new_fields)
                raise RuntimeError(msg)

        fields += new_fields
    return fields #[field.strip() for field in fields]
Ejemplo n.º 4
0
def to_fields(card_lines: List[str], card_name: str) -> List[str]:
    """
    Converts a series of lines in a card into string versions of the field.
    Handles large, small, and CSV formatted cards.

    Parameters
    ----------
    lines : List[str]
        the lines of the BDF card object
    card_name : str
        the card_name -> 'GRID'

    Returns
    -------
    fields : List[str]
        the string formatted fields of the card

    .. warning:: this function is used by the reader and isn't intended
                 to be called by a separate process

    .. code-block:: python

      >>> card_lines = ['GRID,1,,1.0,2.0,3.0']
      >>> card_name = 'GRID'
      >>> fields = to_fields(lines, card_name)
      >>> fields
      ['GRID', '1', '', '1.0', '2.0', '3.0']

    """
    fields = []  # type: List[str]

    if card_name == 'MONPNT1':
        return _to_fields_mntpnt1(card_lines)

    # first line
    line = card_lines[0]
    if '=' in line:
        msg = 'card_name=%r\nequal signs are not supported...line=%r' % (
            card_name, line)
        raise CardParseSyntaxError(msg)

    if '\t' in line:
        line = expand_tabs(line)

    if '*' in line:  # large field
        if ',' in line:  # csv
            new_fields = line.split(',')[:5]
            for unused_i in range(5 - len(new_fields)):
                new_fields.append('')
        else:  # standard
            new_fields = [
                line[0:8], line[8:24], line[24:40], line[40:56], line[56:72]
            ]
        fields += new_fields
        assert len(fields) == 5, fields
    else:  # small field
        if ',' in line:  # csv
            new_fields = line.split(',')[:9]
            for unused_i in range(9 - len(new_fields)):
                new_fields.append('')
        else:  # standard
            new_fields = [
                line[0:8], line[8:16], line[16:24], line[24:32], line[32:40],
                line[40:48], line[48:56], line[56:64], line[64:72]
            ]
        fields += new_fields
        assert len(fields) == 9, fields

    for line in card_lines[1:]:  # continuation lines
        if '=' in line and card_name != 'EIGRL':
            msg = 'card_name=%r\nequal signs are not supported...\nline=%r' % (
                card_name, line)
            raise CardParseSyntaxError(msg)
        if '\t' in line:
            line = expand_tabs(line)

        if '*' in line:  # large field
            if ',' in line:  # csv
                new_fields = line.split(',')[1:5]
                for unused_i in range(4 - len(new_fields)):
                    new_fields.append('')
            else:  # standard
                new_fields = [
                    line[8:24], line[24:40], line[40:56], line[56:72]
                ]
            assert len(new_fields) == 4, new_fields
        else:  # small field
            if ',' in line:  # csv
                new_fields = line.split(',')[1:9]
                for unused_i in range(8 - len(new_fields)):
                    new_fields.append('')
            else:  # standard
                new_fields = [
                    line[8:16], line[16:24], line[24:32], line[32:40],
                    line[40:48], line[48:56], line[56:64], line[64:72]
                ]
            if len(new_fields) != 8:
                nfields = len(new_fields)
                msg = 'nfields=%s new_fields=%s' % (nfields, new_fields)
                raise RuntimeError(msg)

        fields += new_fields
    return fields