Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cloudooo
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boris Kocherov
cloudooo
Commits
3bdab08f
Commit
3bdab08f
authored
Sep 14, 2018
by
Jérome Perrin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
converter: sniff csv field delimiter
using python csv module
parent
de321992
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
50 additions
and
1 deletion
+50
-1
cloudooo/handler/ooo/helper/unoconverter.py
cloudooo/handler/ooo/helper/unoconverter.py
+12
-1
cloudooo/handler/ooo/tests/data/csv_semicolon.csv
cloudooo/handler/ooo/tests/data/csv_semicolon.csv
+2
-0
cloudooo/handler/ooo/tests/data/tsv.tsv
cloudooo/handler/ooo/tests/data/tsv.tsv
+2
-0
cloudooo/handler/ooo/tests/testOooServer.py
cloudooo/handler/ooo/tests/testOooServer.py
+34
-0
No files found.
cloudooo/handler/ooo/helper/unoconverter.py
View file @
3bdab08f
...
...
@@ -28,6 +28,8 @@
##############################################################################
import
sys
import
csv
import
codecs
import
helper_util
from
os.path
import
dirname
,
splitext
from
tempfile
import
mktemp
...
...
@@ -149,9 +151,18 @@ class UnoConverter(object):
_
,
extension
=
splitext
(
source_url
)
if
extension
==
'.csv'
:
# https://wiki.openoffice.org/wiki/Documentation/DevGuide/Spreadsheets/Filter_Options
# Try to sniff the csv delimiter
with
codecs
.
open
(
source_url
,
'rb'
,
'utf-8'
,
errors
=
"ignore"
)
as
csvfile
:
try
:
dialect
=
csv
.
Sniffer
().
sniff
(
csvfile
.
read
(
1024
))
delimiter
=
ord
(
dialect
.
delimiter
)
except
csv
.
Error
:
delimiter
=
ord
(
','
)
return
(
self
.
_createProperty
(
"FilterName"
,
"Text - txt - csv (StarCalc)"
),
self
.
_createProperty
(
"FilterOptions"
,
"
44,34,UTF-8"
),
)
self
.
_createProperty
(
"FilterOptions"
,
"
{delimiter},34,UTF-8"
.
format
(
**
locals
())
),
)
return
()
...
...
cloudooo/handler/ooo/tests/data/csv_semicolon.csv
0 → 100644
View file @
3bdab08f
a a,1
b b,2;x
cloudooo/handler/ooo/tests/data/tsv.tsv
0 → 100644
View file @
3bdab08f
a b
1,3 c
\ No newline at end of file
cloudooo/handler/ooo/tests/testOooServer.py
View file @
3bdab08f
...
...
@@ -578,6 +578,7 @@ class TestChapterItemList(TestCase):
class
TestCSVEncoding
(
TestCase
):
"""Cloudoo tries to be "a bit" clever with CSV:
* the supported encoding is UTF-8, but also accepts latin9, for compatibility.
* the fields delimiter is guessed by python csv module.
"""
def
test_decode_ascii
(
self
):
data
=
encodestring
(
open
(
"./data/csv_ascii.csv"
).
read
())
...
...
@@ -608,3 +609,36 @@ class TestCSVEncoding(TestCase):
self
.
assertEqual
(
[
u"Jérome"
,
u"1€"
],
[
x
.
text
for
x
in
tree
.
getroot
().
find
(
'.//tr[1]'
).
iterdescendants
()
if
x
.
text
])
def
test_separator_semicolon
(
self
):
data
=
encodestring
(
open
(
"./data/csv_semicolon.csv"
).
read
())
converted
=
decodestring
(
self
.
proxy
.
convertFile
(
data
,
"csv"
,
"html"
))
parser
=
etree
.
HTMLParser
()
tree
=
etree
.
parse
(
StringIO
(
converted
),
parser
)
self
.
assertEqual
(
[
'a a'
,
'1'
],
[
x
.
text
for
x
in
tree
.
getroot
().
find
(
'.//tr[1]'
).
iterdescendants
()
if
x
.
text
])
self
.
assertEqual
(
[
'b b'
,
'2;x'
],
[
x
.
text
for
x
in
tree
.
getroot
().
find
(
'.//tr[2]'
).
iterdescendants
()
if
x
.
text
])
def
test_separator_tab
(
self
):
data
=
encodestring
(
open
(
"./data/tsv.tsv"
).
read
())
converted
=
decodestring
(
self
.
proxy
.
convertFile
(
data
,
"csv"
,
"html"
))
parser
=
etree
.
HTMLParser
()
tree
=
etree
.
parse
(
StringIO
(
converted
),
parser
)
self
.
assertEqual
(
[
'a'
,
'b'
],
[
x
.
text
for
x
in
tree
.
getroot
().
find
(
'.//tr[1]'
).
iterdescendants
()
if
x
.
text
])
self
.
assertEqual
(
[
'1,3'
,
'c'
],
[
x
.
text
for
x
in
tree
.
getroot
().
find
(
'.//tr[2]'
).
iterdescendants
()
if
x
.
text
])
def
test_empty_csv
(
self
):
data
=
encodestring
(
""
)
converted
=
decodestring
(
self
.
proxy
.
convertFile
(
data
,
"csv"
,
"html"
))
parser
=
etree
.
HTMLParser
()
tree
=
etree
.
parse
(
StringIO
(
converted
),
parser
)
self
.
assertEqual
(
[],
[
x
.
text
for
x
in
tree
.
getroot
().
findall
(
'.//td'
)])
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment