Skip to content

Commit

Permalink
Merge pull request #20 from pyexcel/dev
Browse files Browse the repository at this point in the history
release 0.6.0
  • Loading branch information
chfw authored Oct 8, 2020
2 parents 0384838 + da7180e commit 2393904
Show file tree
Hide file tree
Showing 19 changed files with 114 additions and 181 deletions.
11 changes: 0 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ python:

stages:
- lint
- moban
- test


Expand All @@ -23,16 +22,6 @@ stages:
stage: lint
script: make lint

.moban: &moban
python: 3.6
env:
- MINREQ=0
stage: moban
install: pip install moban gitfs2 pypifs moban-jinja2-github moban-ansible
script:
- moban
- git diff --exit-code

jobs:
include:
- *moban
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Change log
================================================================================

0.6.0 - 8.10.2020
--------------------------------------------------------------------------------

**added**

#. new style reader and writer plugins. works with pyexcel-io v0.6.2

0.5.3 - 27.11.2018
--------------------------------------------------------------------------------

Expand Down
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2015-2019 by Onni Software Ltd. and its contributors
Copyright (c) 2015-2020 by Onni Software Ltd. and its contributors
All rights reserved.

Redistribution and use in source and binary forms of the software as well
Expand All @@ -13,7 +13,7 @@ that the following conditions are met:
and/or other materials provided with the distribution.

* Neither the name of 'pyexcel-ods3' nor the names of the contributors
may be used to endorse or promote products derived from this software
may not be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE AND DOCUMENTATION IS PROVIDED BY THE COPYRIGHT HOLDERS AND
Expand Down
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ pyexcel-ods3 - Let you focus on data, instead of ods format
.. image:: https://img.shields.io/gitter/room/gitterHQ/gitter.svg
:target: https://gitter.im/pyexcel/Lobby

.. image:: https://img.shields.io/static/v1?label=continuous%20templating&message=%E6%A8%A1%E7%89%88%E6%9B%B4%E6%96%B0&color=blue&style=flat-square
:target: https://moban.readthedocs.io/en/latest/#at-scale-continous-templating-for-open-source-projects

.. image:: https://img.shields.io/static/v1?label=coding%20style&message=black&color=black&style=flat-square
:target: https://github.com/psf/black

**pyexcel-ods3** is a tiny wrapper library to read, manipulate and write data in ods
format. You are likely to use `pyexcel <https://github.com/pyexcel/pyexcel>`__ together
Expand Down
6 changes: 6 additions & 0 deletions changelog.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
name: pyexcel-ods3
organisation: pyexcel
releases:
- changes:
- action: added
details:
- 'new style reader and writer plugins. works with pyexcel-io v0.6.2'
date: 8.10.2020
version: 0.6.0
- changes:
- action: added
details:
Expand Down
4 changes: 2 additions & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
# -- Project information -----------------------------------------------------

project = 'pyexcel-ods3'
copyright = '2015-2019 Onni Software Ltd.'
copyright = '2015-2020 Onni Software Ltd.'
author = 'chfw'
# The short X.Y version
version = '0.6.0'
# The full version, including alpha/beta/rc tags
release = '0.5.3'
release = '0.6.0'

# -- General configuration ---------------------------------------------------

Expand Down
9 changes: 5 additions & 4 deletions pyexcel-ods3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ name: "pyexcel-ods3"
nick_name: ods3
version: 0.6.0
current_version: 0.6.0
release: 0.5.3
release: 0.6.0
file_type: ods
is_on_conda: true
dependencies:
- pyexcel-io>=0.5.10
- pyexcel-io>=0.6.2
- lxml
- pyexcel-ezodf>=0.3.3
test_dependencies:
- pyexcel
- psutil
- pyexcel-xls
description: |
A wrapper library to read, manipulate and write data in ods format
moban_command: false
description: |-
A wrapper library to read, manipulate and write data in ods format
11 changes: 9 additions & 2 deletions pyexcel_ods3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,22 @@

# this line has to be place above all else
# because of dynamic import
from pyexcel_io.plugins import IOPluginInfoChain
from pyexcel_io.plugins import IOPluginInfoChain, IOPluginInfoChainV2

__FILE_TYPE__ = "ods"
IOPluginInfoChain(__name__).add_a_reader(
IOPluginInfoChainV2(__name__).add_a_reader(
relative_plugin_class_path="odsr.ODSBook",
locations=["file", "memory"],
file_types=[__FILE_TYPE__],
stream_type="binary",
).add_a_reader(
relative_plugin_class_path="odsr.ODSBookInContent",
locations=["content"],
file_types=[__FILE_TYPE__],
stream_type="binary",
).add_a_writer(
relative_plugin_class_path="odsw.ODSWriter",
locations=["file", "memory"],
file_types=[__FILE_TYPE__],
stream_type="binary",
)
Expand Down
103 changes: 32 additions & 71 deletions pyexcel_ods3/odsr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,38 @@
ods reader
:copyright: (c) 2015-2017 by Onni Software Ltd. & its contributors
:copyright: (c) 2015-2020 by Onni Software Ltd. & its contributors
:license: New BSD License
"""
import ezodf
from io import BytesIO

import ezodf
import pyexcel_io.service as service
from pyexcel_io.book import BookReader
from pyexcel_io.sheet import SheetReader
from pyexcel_io._compact import OrderedDict
from pyexcel_io.plugin_api import ISheet, IReader, NamedContent


class ODSSheet(SheetReader):
class ODSSheet(ISheet):
"""ODS sheet representation"""

def __init__(self, sheet, auto_detect_int=True, **keywords):
SheetReader.__init__(self, sheet, **keywords)
def __init__(self, sheet, auto_detect_int=True):
self.auto_detect_int = auto_detect_int
self.ods_sheet = sheet

@property
def name(self):
return self._native_sheet.name

def number_of_rows(self):
def row_iterator(self):
"""
Number of rows in the xls sheet
"""
return self._native_sheet.nrows()
return range(self.ods_sheet.nrows())

def number_of_columns(self):
def column_iterator(self, row):
"""
Number of columns in the xls sheet
"""
return self._native_sheet.ncols()
for column in range(self.ods_sheet.ncols()):
yield self.cell_value(row, column)

def cell_value(self, row, column):
cell = self._native_sheet.get_cell((row, column))
cell = self.ods_sheet.get_cell((row, column))
cell_type = cell.value_type
ret = None
if cell_type == "currency":
Expand All @@ -63,63 +59,28 @@ def cell_value(self, row, column):
return ret


class ODSBook(BookReader):
"""read a ods book out"""

def open(self, file_name, **keywords):
"""load ods from file"""
BookReader.open(self, file_name, **keywords)
self._load_from_file()

def open_stream(self, file_stream, **keywords):
"""load ods from file stream"""
BookReader.open_stream(self, file_stream, **keywords)
self._load_from_memory()

def read_sheet_by_name(self, sheet_name):
"""read a named sheet"""
rets = [
sheet
for sheet in self._native_book.sheets
if sheet.name == sheet_name
class ODSBook(IReader):
def __init__(self, file_alike_object, file_type, **keywords):
self.ods_book = ezodf.opendoc(file_alike_object)
self._keywords = keywords
self.content_array = [
NamedContent(sheet.name, sheet) for sheet in self.ods_book.sheets
]
if len(rets) == 0:
raise ValueError("%s cannot be found" % sheet_name)
elif len(rets) == 1:
return self.read_sheet(rets[0])
else:
raise ValueError(
"More than 1 sheet named as %s are found" % sheet_name
)

def read_sheet_by_index(self, sheet_index):
"""read a sheet at an index"""
sheets = self._native_book.sheets
length = len(sheets)
if sheet_index < length:
return self.read_sheet(sheets[sheet_index])
else:
raise IndexError(
"Index %d of out bound %d." % (sheet_index, length)
)

def read_all(self):
"""read all available sheets"""
result = OrderedDict()
for sheet in self._native_book.sheets:
data_dict = self.read_sheet(sheet)
result.update(data_dict)
return result

def read_sheet(self, native_sheet):

def read_sheet(self, native_sheet_index):
native_sheet = self.content_array[native_sheet_index].payload
sheet = ODSSheet(native_sheet, **self._keywords)
return {native_sheet.name: sheet.to_array()}
return sheet

def close(self):
self._native_book = None
self.ods_book = None


def _load_from_file(self):
self._native_book = ezodf.opendoc(self._file_name)
class ODSBookInContent(ODSBook):
"""
Open xlsx as read only mode
"""

def _load_from_memory(self):
self._native_book = ezodf.opendoc(self._file_stream)
def __init__(self, file_content, file_type, **keywords):
io = BytesIO(file_content)
super().__init__(io, file_type, **keywords)
49 changes: 22 additions & 27 deletions pyexcel_ods3/odsw.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,30 @@
ods writer using ezodf
:copyright: (c) 2015-2017 by Onni Software Ltd. & its contributors
:copyright: (c) 2015-2020 by Onni Software Ltd. & its contributors
:license: New BSD License
"""
import types

import ezodf

import pyexcel_io.service as service
from pyexcel_io.book import BookWriter
from pyexcel_io.sheet import SheetWriter
from pyexcel_io.constants import MAX_INTEGER
from pyexcel_io.exceptions import IntegerAccuracyLossError
from pyexcel_io.plugin_api import IWriter, ISheetWriter


class ODSSheetWriter(SheetWriter):
class ODSSheetWriter(ISheetWriter):
"""
ODS sheet writer
"""

def set_sheet_name(self, name):
self._native_sheet = ezodf.Sheet(name)
def __init__(self, ods_book, ods_sheet, sheet_name, **keywords):
self.ods_book = ods_book
self.ods_sheet = ezodf.Sheet(sheet_name)
self.current_row = 0

def set_size(self, size):
self._native_sheet.reset(size=size)
def _set_size(self, size):
self.ods_sheet.reset(size=size)

def write_row(self, array):
"""
Expand All @@ -48,7 +47,7 @@ def write_row(self, array):
elif value_type == "float":
if cell > MAX_INTEGER:
raise IntegerAccuracyLossError("%s is too big" % cell)
self._native_sheet[self.current_row, count].set_value(
self.ods_sheet[self.current_row, count].set_value(
cell, value_type=value_type
)
count += 1
Expand All @@ -62,7 +61,7 @@ def write_array(self, table):
if rows < 1:
return
columns = max([len(row) for row in to_write_data])
self.set_size((rows, columns))
self._set_size((rows, columns))
for row in to_write_data:
self.write_row(row)

Expand All @@ -71,40 +70,36 @@ def close(self):
This call writes file
"""
self._native_book.sheets += self._native_sheet
self.ods_book.sheets += self.ods_sheet


class ODSWriter(BookWriter):
class ODSWriter(IWriter):
"""
open document spreadsheet writer
"""

def __init__(self):
BookWriter.__init__(self)
self._native_book = None

def open(self, file_name, **keywords):
def __init__(
self, file_alike_object, file_type, skip_backup=True, **keywords
):
"""open a file for writing ods"""
BookWriter.open(self, file_name, **keywords)
self._native_book = ezodf.newdoc(
doctype="ods", filename=self._file_alike_object
self.ods_book = ezodf.newdoc(
doctype=file_type, filename=file_alike_object
)

skip_backup_flag = self._keywords.get("skip_backup", True)
if skip_backup_flag:
self._native_book.backup = False
if skip_backup:
self.ods_book.backup = False

def create_sheet(self, name):
"""
write a row into the file
"""
return ODSSheetWriter(self._native_book, None, name)
return ODSSheetWriter(self.ods_book, None, name)

def close(self):
"""
This call writes file
"""
self._native_book.save()
self._native_book = None
self.ods_book.save()
self.ods_book = None
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pyexcel-io>=0.5.10
pyexcel-io>=0.6.2
lxml
pyexcel-ezodf>=0.3.3
2 changes: 0 additions & 2 deletions rnd_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
https://github.com/pyexcel/pyexcel-ezodf/archive/master.zip
https://github.com/pyexcel/pyexcel-io/archive/v0.5.10.zip
Loading

0 comments on commit 2393904

Please sign in to comment.