예제 #1
0
def test_equality():
    c1 = ReadOnlyCell(None, None, 10, None)
    c2 = ReadOnlyCell(None, None, 10, None)
    assert c1 is not c2
    assert c1 == c2
    c3 = ReadOnlyCell(None, None, 5, None)
    assert c3 != c1
예제 #2
0
    def _get_row(self, element, min_col=1, max_col=None):
        """Return cells from a particular row"""
        col_counter = min_col

        for cell in safe_iterator(element, CELL_TAG):
            coord = cell.get('r')
            column_str, row = coordinate_from_string(coord)
            column = column_index_from_string(column_str)

            if max_col is not None and column > max_col:
                break

            if min_col <= column:
                for gap in range(col_counter, column):
                    # pad row with missing cells
                    yield ReadOnlyCell(self, row, None, None)

                data_type = cell.get('t', 'n')
                style_id = int(cell.get('s', 0))
                formula = cell.findtext(FORMULA_TAG)
                value = cell.find(VALUE_TAG)
                if value is not None:
                    value = value.text
                if formula is not None:
                    if not self.parent.data_only:
                        data_type = Cell.TYPE_FORMULA
                        value = "=%s" % formula

                yield ReadOnlyCell(self, row, column_str, value, data_type,
                                   style_id)
            col_counter = column + 1
        if max_col is not None:
            while col_counter <= max_col:
                yield ReadOnlyCell(self, row, None, None)
                col_counter += 1
예제 #3
0
def test_read_only(dummy_sheet):
    cell = ReadOnlyCell(sheet=dummy_sheet, row=None, column=None, value='1')
    assert cell.value == 1
    with pytest.raises(AttributeError):
        cell.value = 10
    with pytest.raises(AttributeError):
        cell.style = 1
예제 #4
0
def DummyCell(dummy_sheet):

    dummy_sheet.parent._number_formats.add('d-mmm-yy')
    style = StyleArray([0, 0, 0, 164, 0, 0, 0, 0, 0])
    dummy_sheet.parent._cell_styles.add(style)
    cell = ReadOnlyCell(dummy_sheet, None, None, "23596", 'n', 1)
    return cell
    def _get_row(self, row, min_col=1, max_col=None, values_only=False):
        """
        Make sure a row contains always the same number of cells or values
        """
        if not row:
            return ()
        first_col = row[0]['column']
        last_col = row[-1]['column']
        max_col = max_col or last_col
        row_width = max_col + 1 - min_col

        if values_only:
            new_row = [None] * row_width
        else:
            new_row = [EMPTY_CELL] * row_width

        for cell in row:
            counter = cell['column']
            if min_col <= counter <= max_col:
                idx = counter - min_col
                if values_only:
                    new_row[idx] = cell['value']
                else:
                    new_row[idx] = ReadOnlyCell(self, **cell)

        return tuple(new_row)
예제 #6
0
def test_read_only():
    cell = ReadOnlyCell(sheet=None, row=None, column=None, value=1)
    assert cell.value == 1
    with pytest.raises(AttributeError):
        cell.value = 10
    with pytest.raises(AttributeError):
        cell.style_id = 1
예제 #7
0
 def get_cells(self, min_row, min_col, max_row, max_col):
     p = iterparse(self.xml_source, tag=[ROW_TAG], remove_blank_text=True)
     for _event, element in p:
         if element.tag == ROW_TAG:
             row = int(element.get("r"))
             if max_row is not None and row > max_row:
                 break
             if min_row <= row:
                 for cell in safe_iterator(element, CELL_TAG):
                     coord = cell.get('r')
                     column_str, row = coordinate_from_string(coord)
                     column = column_index_from_string(column_str)
                     if max_col is not None and column > max_col:
                         break
                     if min_col <= column:
                         data_type = cell.get('t', 'n')
                         style_id = cell.get('s')
                         formula = cell.findtext(FORMULA_TAG)
                         value = cell.findtext(VALUE_TAG)
                         if formula is not None and not self.parent.data_only:
                             data_type = Cell.TYPE_FORMULA
                             value = "=%s" % formula
                         yield ReadOnlyCell(row, column_str, value, data_type,
                                       style_id)
         if element.tag in (CELL_TAG, VALUE_TAG, FORMULA_TAG):
             # sub-elements of rows should be skipped
             continue
         element.clear()
예제 #8
0
  def _parse_row_internal(self, row: ReadOnlyRow):
    """
    Parses whole row and returns parsed instance.
    """
    instance = {}

    required_columns = self.get_required_columns()

    for column in self.get_all_columns():
      try:
        self.parse_column(row, column, instance, column in required_columns)
      except XLSImportError as exc:
        column_id = self.column_mappings.get(column)
        exc.column = get_column_letter(column_id + 1)
        exc.column_enum = column
        raise exc

    for column in self.missing_columns:
      parser = self.get_all_columns()[column]
      mock_cell = ReadOnlyCell(None, None, None, None)
      parser.read_column(row, mock_cell, instance)

    self.on_row_read(instance)

    return instance
예제 #9
0
def DummyCell(dummy_sheet):

    dummy_sheet.parent._number_formats.add('d-mmm-yy')
    style = StyleId(numFmtId=164)
    dummy_sheet.parent._cell_styles.add(style)
    cell = ReadOnlyCell(dummy_sheet, None, None, "23596", 'n', 1)
    return cell
예제 #10
0
    def _get_row(self, element, min_col=1, max_col=None):
        """Return cells from a particular row"""
        col_counter = min_col

        for cell in safe_iterator(element, CELL_TAG):
            coordinate = cell.get('r')
            row, column = coordinate_to_tuple(coordinate)

            if max_col is not None and column > max_col:
                break

            if min_col <= column:
                if col_counter < column:
                    for col_counter in range(max(col_counter, min_col), column):
                        # pad row with missing cells
                        yield EMPTY_CELL

                data_type = cell.get('t', 'n')
                style_id = int(cell.get('s', 0))
                formula = cell.findtext(FORMULA_TAG)
                value = cell.find(VALUE_TAG)
                if value is not None:
                    value = value.text
                if formula is not None:
                    if not self.parent.data_only:
                        data_type = 'f'
                        value = "=%s" % formula

                yield ReadOnlyCell(self, row, column,
                                   value, data_type, style_id)
            col_counter = column + 1
        if max_col is not None:
            for _ in range(col_counter, max_col+1):
                yield EMPTY_CELL
def DummyCell(dummy_sheet):
    class DummyNumberFormat:
        format_code = 'd-mmm-yy'

    class DummyStyle(object):
        number_format = 'd-mmm-yy'

    dummy_sheet.parent.shared_styles.add(DummyStyle())
    cell = ReadOnlyCell(dummy_sheet, None, None, "23596", 'n', '1')
    return cell
예제 #12
0
 def test_pad_row_cells(self, ReadOnlyWorksheet):
     row = [
         {'column':4, 'value':4, 'row':2},
         {'column':8, 'value':8, 'row':2},
     ]
     ws = ReadOnlyWorksheet
     cells = ws._get_row(row, min_col=6, max_col=10)
     assert cells == (
         EMPTY_CELL, EMPTY_CELL,
         ReadOnlyCell(ws, 2, 8, 8, 'n', 0),
         EMPTY_CELL, EMPTY_CELL
     )
예제 #13
0
def DummyCell():
    class DummyNumberFormat:

        format_code = 'd-mmm-yy'

    class DummyStyle(object):

        number_format = DummyNumberFormat()

    style_table = {1: DummyStyle()}
    cell = ReadOnlyCell(None, None, "23596", 'n', '1')
    cell.set_style_table(style_table)
    return cell
예제 #14
0
    def _get_row(self, element, min_col=1, max_col=None, row_counter=None):
        """Return cells from a particular row"""
        col_counter = min_col
        data_only = getattr(self.parent, 'data_only', False)

        for cell in safe_iterator(element, CELL_TAG):
            coordinate = cell.get('r')
            if coordinate:
                row, column = coordinate_to_tuple(coordinate)
            else:
                row, column = row_counter, col_counter

            if max_col is not None and column > max_col:
                break

            if min_col <= column:
                if col_counter < column:
                    for col_counter in range(max(col_counter, min_col),
                                             column):
                        # pad row with missing cells
                        yield EMPTY_CELL

                data_type = cell.get('t', 'n')
                style_id = int(cell.get('s', 0))
                value = None

                formula = cell.findtext(FORMULA_TAG)
                if formula is not None and not data_only:
                    data_type = 'f'
                    value = "=%s" % formula

                elif data_type == 'inlineStr':
                    child = cell.find(INLINE_TAG)
                    if child is not None:
                        richtext = Text.from_tree(child)
                        value = richtext.content

                else:
                    value = cell.findtext(VALUE_TAG) or None

                yield ReadOnlyCell(self, row, column, value, data_type,
                                   style_id)
            col_counter = column + 1

        if max_col is not None:
            for _ in range(col_counter, max_col + 1):
                yield EMPTY_CELL
예제 #15
0
    def _get_row(self, row, min_col=1, max_col=None, values_only=False):
        """
        Make sure a row contains always the same number of cells or values
        """
        if not row and not max_col:  # in case someone wants to force rows where there aren't any
            return ()

        max_col = max_col or row[-1]['column']
        row_width = max_col + 1 - min_col

        new_row = [EMPTY_CELL] * row_width
        if values_only:
            new_row = [None] * row_width

        for cell in row:
            counter = cell['column']
            if min_col <= counter <= max_col:
                idx = counter - min_col  # position in list of cells returned
                new_row[idx] = values_only and cell['value'] or ReadOnlyCell(
                    self, **cell)

        return tuple(new_row)
예제 #16
0
def test_bool(value, expected):
    cell = ReadOnlyCell(None, None, value, 'b')
    assert cell.value is expected
예제 #17
0
def test_base_date():
    ReadOnlyCell.set_base_date(2415018.5)
    cell = ReadOnlyCell(None, None, 10, 'n')
    assert cell.base_date == 2415018.5
예제 #18
0
def test_inline_String(dummy_sheet):
    cell = ReadOnlyCell(dummy_sheet, None, None, "Hello World!", 'inlineStr')
    assert cell.value == "Hello World!"
예제 #19
0
def test_numeric(dummy_sheet, value, expected):
    cell = ReadOnlyCell(dummy_sheet, None, None, value, 'n')
    v = cell.value
    assert v == expected
    assert hasattr(v, 'is_integer') == hasattr(expected, 'is_integer'),\
           "Expected {0}, {1}".format(type(expected), type(v))
예제 #20
0
def test_coordinate(dummy_sheet):
    cell = ReadOnlyCell(dummy_sheet, 1, "A", 10, None)
    assert cell.coordinate == "A1"
    cell = ReadOnlyCell(dummy_sheet, None, None, 1, None)
    with pytest.raises(AttributeError):
        cell.coordinate
예제 #21
0
def test_bool(dummy_sheet, value, expected):
    cell = ReadOnlyCell(dummy_sheet, None, None, value, 'b')
    assert cell.value is expected
예제 #22
0
def test_base_date(dummy_sheet):
    cell = ReadOnlyCell(dummy_sheet, None, None, 10, 'n')
    assert cell.base_date == 2415018.5
예제 #23
0
def test_string_table(dummy_sheet):
    cell = ReadOnlyCell(dummy_sheet, None, None, 0, 's')
    assert cell.shared_strings == ['Hello world']
    assert cell.value == 'Hello world'
예제 #24
0
def test_ctor(dummy_sheet):
    cell = ReadOnlyCell(dummy_sheet, None, None, 10, 'n')
    assert cell.value == 10
예제 #25
0
def test_string_table():
    ReadOnlyCell.set_string_table({1: 'Hello world'})
    cell = ReadOnlyCell(None, None, 1, 's')
    assert cell.string_table == {1: 'Hello world'}
    assert cell.value == 'Hello world'
예제 #26
0
def test_numeric():
    cell = ReadOnlyCell(None, None, "24555", 'n')
    assert cell.value == 24555
    cell = ReadOnlyCell(None, None, None, 'n')
    assert cell.value is None
예제 #27
0
def test_ctor():
    cell = ReadOnlyCell(None, None, 10, 'n')
    assert cell.value == 10
예제 #28
0
def test_read_only():
    cell = ReadOnlyCell(None, None, 1, None)
    with pytest.raises(AttributeError):
        cell.value = 10
    with pytest.raises(AttributeError):
        cell.style_id = 1
예제 #29
0
def test_coordinate(dummy_sheet):
    cell = ReadOnlyCell(dummy_sheet, 1, 1, 10, None)
    assert cell.coordinate == "A1"
예제 #30
0
def test_style_table():
    ReadOnlyCell.set_style_table({})
    cell = ReadOnlyCell(None, None, 10, 'n')
    assert cell.style_table == {}