10000 Jac/lxml (#226) · tableau/document-api-python@aed2a5b · GitHub
[go: up one dir, main page]

Skip to content

Commit aed2a5b

Browse files
authored
Jac/lxml (#226)
* add nested calculations to sample doc * switch from ElementTree to lxml
1 parent af3af22 commit aed2a5b

File tree

11 files changed

+823
-49
lines changed

11 files changed

+823
-49
lines changed

samples/show-fields/nested.tds

Lines changed: 780 additions & 0 deletions
Large diffs are not rendered by default.

samples/show-fields/show_fields.py

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,34 @@
66
############################################################
77
# Step 2) Open the .tds we want to inspect
88
############################################################
9-
sourceTDS = Datasource.from_file('world.tds')
9+
datasources = [Datasource.from_file('world.tds'), Datasource.from_file('nested.tds')]
10+
for sourceTDS in datasources:
1011

11-
############################################################
12-
# Step 3) Print out all of the fields and what type they are
13-
############################################################
14-
print('----------------------------------------------------------')
15-
print('-- Info for our .tds:')
16-
print('-- name:\t{0}'.format(sourceTDS.name))
17-
print('-- version:\t{0}'.format(sourceTDS.version))
18-
print('----------------------------------------------------------')
19-
print('--- {} total fields in this datasource'.format(len(sourceTDS.fields)))
20-
print('----------------------------------------------------------')
21-
for count, field in enumerate(sourceTDS.fields.values()):
22-
print('{:>4}: {} is a {}'.format(count+1, field.name, field.datatype))
23-
blank_line = False
24-
if field.calculation:
25-
print(' the formula is {}'.format(field.calculation))
26-
blank_line = True
27-
if field.default_aggregation:
28-
print(' the default aggregation is {}'.format(field.default_aggregation))
29-
blank_line = True
30-
if field.description:
31-
print(' the description is {}'.format(field.description))
12+
############################################################
13+
# Step 3) Print out all of the fields and what type they are
14+
############################################################
15+
print('----------------------------------------------------------')
16+
print('-- Info for our .tds:')
17+
print('-- name:\t{0}'.format(sourceTDS.name))
18+
print('-- version:\t{0}'.format(sourceTDS.version))
19+
print('----------------------------------------------------------')
20+
print('--- {} total fields in this datasource'.format(len(sourceTDS.fields)))
21+
print('----------------------------------------------------------')
22+
for count, field in enumerate(sourceTDS.fields.values()):
23+
blank_line = False
24+
if field.calculation:
25+
print('{:>4}: field named `{}` is a `{}`'.format(count+1, field.name, field.datatype))
26+
print(' field id, caption, calculation: `{}`, `{}`, `{}`'.format(field.id, field.caption,
27+
field.calculation))
28+
blank_line = True
29+
if field.default_aggregation:
30+
print('{:>4}: `{}` is a `{}`, default aggregation is `{}`'.format(count+1, field.name, field.datatype,
31+
field.default_aggregation))
32+
33+
if field.description:
34+
print('{:>4}: `{}` is a `{}`, description is `{}`'.format(count+1, field.name, field.datatype,
35+
field.description))
3236

33-
if blank_line:
34-
print('')
35-
print('----------------------------------------------------------')
37+
if blank_line:
38+
print('')
39+
print('----------------------------------------------------------')

samples/show_workbook_info/show_workbook_info.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Step 1) Use Datasource object from the Document API
33
############################################################
44
from tableaudocumentapi import Workbook
5-
import xml.etree.ElementTree as ET
5+
from lxml import etree as ET
66

77
############################################################
88
# Step 2) Open the .tds we want to explore

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
packages=['tableaudocumentapi'],
1010
license='MIT',
1111
description='A Python module for working with Tableau files.',
12-
test_suite='test'
12+
test_suite='test',
13+
install_requires=['lxml']
1314
)

tableaudocumentapi/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import xml.etree.ElementTree as ET
1+
from lxml import etree as ET
22
from tableaudocumentapi.dbclass import is_valid_dbclass
33

44

tableaudocumentapi/datasource.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import collections
22
import itertools
3-
import xml.etree.ElementTree as ET
3+
from lxml import etree as ET
44
import xml.sax.saxutils as sax
55
from uuid import uuid4
66

tableaudocumentapi/field.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import functools
2-
import xml.etree.ElementTree as ET
2+
from lxml import etree as ET
33
from xml.dom import minidom
44

55
from tableaudocumentapi.property_decorators import argument_is_one_of
@@ -277,7 +277,7 @@ def add_alias(self, key, value):
277277
# determine whether there already is an aliases-tag
278278
aliases = self._xml.find('aliases')
279279
# and create it if there isn't
280-
if not aliases:
280+
if not aliases: # ignore the FutureWarning, does not apply to our usage
281281
aliases = ET.Element('aliases')
282282
self._xml.append(aliases)
283283

@@ -298,7 +298,7 @@ def aliases(self):
298298
Returns:
299299< 10000 code class="diff-text syntax-highlighted-line">
Key-value mappings of all registered aliases. Dict.
300300
"""
301-
aliases_tag = self._xml.find('aliases') or []
301+
aliases_tag = self._xml.find('aliases') or [] # ignore the FutureWarning, does not apply to our usage
302302
return {a.get('key', 'None'): a.get('value', 'None') for a in list(aliases_tag)}
303303

304304
########################################

tableaudocumentapi/xfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import shutil
44
import tempfile
55
import zipfile
6-
import xml.etree.ElementTree as ET
6+
from lxml import etree as ET
77

88
from distutils.version import LooseVersion as Version
99

test/bvt.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
import unittest
33

4-
import xml.etree.ElementTree as ET
4+
from lxml import etree as ET
55
from test.assets.index import *
66
from tableaudocumentapi import Workbook, Datasource, Connection, ConnectionParser
77
from tableaudocumentapi.xfile import TableauInvalidFileException, TableauVersionNotSupportedException
@@ -160,7 +160,7 @@ def test_save_has_xml_declaration(self):
160160
with open(self.tds_file.name) as f:
161161
first_line = f.readline().strip() # first line should be xml tag
162162
self.assertEqual(
163-
first_line, "<?xml version='1.0' encoding='utf-8'?>")
163+
first_line, "<?xml version='1.0' encoding='UTF-8'?>")
164164

165165

166166
class DatasourceModelV10Tests(unittest.TestCase):
@@ -323,7 +323,7 @@ def test_save_has_xml_declaration(self):
323323
with open(self.workbook_file.name) as f:
324324
first_line = f.readline().strip() # first line should be xml tag
325325
self.assertEqual(
326-
first_line, "<?xml version='1.0' encoding='utf-8'?>")
326+
first_line, "<?xml version='1.0' encoding='UTF-8'?>")
327327

328328

329329
class WorkbookModelV10TWBXTests(unittest.TestCase):

test/test_field_change.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os.path
33

44
from tableaudocumentapi import Datasource
5-
import xml.etree.ElementTree as ET
5+
from lxml import etree as ET
66

77

88
TEST_ASSET_DIR = os.path.join(

test/test_xfile.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,10 @@ def test_save_preserves_namespace_twb(self):
5353
wb.save_as(new_name)
5454
self.assertContainsUserNamespace(new_name)
5555

56-
'''
5756
def demo_bug_ns_not_preserved_if_not_used(self):
5857
filename = TABLEAU_10_TDS
5958
self.assertContainsUserNamespace(filename)
6059
wb = Datasource.from_file(filename)
61-
#wb.save()
6260
new_name = 'saved-as-tds.tds'
6361
wb.save_as(new_name)
64-
self.assertContainsUserNamespace(new_name) <- throws
65-
66-
If there is no namespace in the document when you begin working with it,
67-
none will be added.
68-
If there is a namespace but it *is not used* in the document, it will be stripped
69-
70-
Fix will be something like
71-
https://stackoverflow.com/questions/41937624/elementtree-why-are-my-namespace-declarations-stripped-out
72-
73-
'''
62+
self.assertContainsUserNamespace(new_name)

0 commit comments

Comments
 (0)
0