Skip to content

Commit

Permalink
code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
chfw committed Sep 25, 2015
1 parent 80c9657 commit 85b3b6a
Showing 1 changed file with 35 additions and 20 deletions.
55 changes: 35 additions & 20 deletions pyexcel_ods/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
# limitations under the License.

# Thanks to grt for the fixes
import sys
import datetime
from odf.table import TableRow, TableCell, Table
from odf.text import P
from odf.namespaces import OFFICENS
from odf.opendocument import OpenDocumentSpreadsheet, load
from pyexcel_io import (
SheetReaderBase,
BookReader,
Expand All @@ -34,13 +39,6 @@
load_data as read_data,
store_data as write_data
)
import odf.opendocument
from odf.table import TableRow, TableCell, Table
from odf.text import P
from odf.namespaces import OFFICENS
from odf.opendocument import OpenDocumentSpreadsheet
import sys
PY2 = sys.version_info[0] == 2


def float_value(value):
Expand All @@ -49,19 +47,27 @@ def float_value(value):


def date_value(value):
ret = "invalid"
try:
# catch strptime exceptions only
if len(value) == 10:
ret = datetime.datetime.strptime(value, "%Y-%m-%d")
ret = datetime.datetime.strptime(
value,
"%Y-%m-%d")
ret = ret.date()
elif len(value) == 19:
ret = datetime.datetime.strptime(value, "%Y-%m-%dT%H:%M:%S")
ret = datetime.datetime.strptime(
value,
"%Y-%m-%dT%H:%M:%S")
elif len(value) > 19:
ret = datetime.datetime.strptime(value[0:26], "%Y-%m-%dT%H:%M:%S.%f")
else:
raise Exception("Bad date value %s" % value)
return ret
ret = datetime.datetime.strptime(
value[0:26],
"%Y-%m-%dT%H:%M:%S.%f")
except:
pass
if ret == "invalid":
raise Exception("Bad date value %s" % value)
return ret


def ods_date_value(value):
Expand Down Expand Up @@ -104,6 +110,7 @@ def ods_bool_value(value):
"currency": float
}


ODS_WRITE_FORMAT_COVERSION = {
float: "float",
int: "float",
Expand Down Expand Up @@ -205,15 +212,16 @@ def get_sheet(self, native_sheet):
return ODSSheet(native_sheet)

def load_from_memory(self, file_content, **keywords):
return odf.opendocument.load(file_content)
return load(file_content)

def load_from_file(self, filename, **keywords):
return odf.opendocument.load(filename)
return load(filename)

def sheet_iterator(self):
if self.sheet_name is not None:
tables = self.native_book.spreadsheet.getElementsByType(Table)
rets = [table for table in tables if table.getAttribute('name') == self.sheet_name]
rets = [table for table in tables
if table.getAttribute('name') == self.sheet_name]
if len(rets) == 0:
raise ValueError("%s cannot be found" % self.sheet_name)
else:
Expand All @@ -224,8 +232,8 @@ def sheet_iterator(self):
if self.sheet_index < length:
return [tables[self.sheet_index]]
else:
raise IndexError("Index %d of out bound %d" %(self.sheet_index,
length))
raise IndexError("Index %d of out bound %d" % (
self.sheet_index, length))
else:
return self.native_book.spreadsheet.getElementsByType(Table)

Expand Down Expand Up @@ -293,19 +301,26 @@ def close(self):
"""
self.native_book.write(self.file)

# Register ods reader and writer
READERS["ods"] = ODSBook
WRITERS["ods"] = ODSWriter


def save_data(afile, data, file_type=None, **keywords):
"""
Save all data to a file
"""
if isstream(afile) and file_type is None:
file_type='ods'
file_type = 'ods'
write_data(afile, data, file_type=file_type, **keywords)


def get_data(afile, file_type=None, **keywords):
"""
Retrieve all data from incoming file
"""
if isstream(afile) and file_type is None:
file_type='ods'
file_type = 'ods'
return read_data(afile, file_type=file_type, **keywords)


Expand Down

0 comments on commit 85b3b6a

Please sign in to comment.