Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jac/lxml #226

Merged
merged 3 commits into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
780 changes: 780 additions & 0 deletions samples/show-fields/nested.tds

Large diffs are not rendered by default.

54 changes: 29 additions & 25 deletions samples/show-fields/show_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,34 @@
############################################################
# Step 2) Open the .tds we want to inspect
############################################################
sourceTDS = Datasource.from_file('world.tds')
datasources = [Datasource.from_file('world.tds'), Datasource.from_file('nested.tds')]
for sourceTDS in datasources:

############################################################
# Step 3) Print out all of the fields and what type they are
############################################################
print('----------------------------------------------------------')
print('-- Info for our .tds:')
print('-- name:\t{0}'.format(sourceTDS.name))
print('-- version:\t{0}'.format(sourceTDS.version))
print('----------------------------------------------------------')
print('--- {} total fields in this datasource'.format(len(sourceTDS.fields)))
print('----------------------------------------------------------')
for count, field in enumerate(sourceTDS.fields.values()):
print('{:>4}: {} is a {}'.format(count+1, field.name, field.datatype))
blank_line = False
if field.calculation:
print(' the formula is {}'.format(field.calculation))
blank_line = True
if field.default_aggregation:
print(' the default aggregation is {}'.format(field.default_aggregation))
blank_line = True
if field.description:
print(' the description is {}'.format(field.description))
############################################################
# Step 3) Print out all of the fields and what type they are
############################################################
print('----------------------------------------------------------')
print('-- Info for our .tds:')
print('-- name:\t{0}'.format(sourceTDS.name))
print('-- version:\t{0}'.format(sourceTDS.version))
print('----------------------------------------------------------')
print('--- {} total fields in this datasource'.format(len(sourceTDS.fields)))
print('----------------------------------------------------------')
for count, field in enumerate(sourceTDS.fields.values()):
blank_line = False
if field.calculation:
print('{:>4}: field named `{}` is a `{}`'.format(count+1, field.name, field.datatype))
print(' field id, caption, calculation: `{}`, `{}`, `{}`'.format(field.id, field.caption,
field.calculation))
blank_line = True
if field.default_aggregation:
print('{:>4}: `{}` is a `{}`, default aggregation is `{}`'.format(count+1, field.name, field.datatype,
field.default_aggregation))

if field.description:
print('{:>4}: `{}` is a `{}`, description is `{}`'.format(count+1, field.name, field.datatype,
field.description))

if blank_line:
print('')
print('----------------------------------------------------------')
if blank_line:
print('')
print('----------------------------------------------------------')
2 changes: 1 addition & 1 deletion samples/show_workbook_info/show_workbook_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Step 1) Use Datasource object from the Document API
############################################################
from tableaudocumentapi import Workbook
import xml.etree.ElementTree as ET
from lxml import etree as ET

############################################################
# Step 2) Open the .tds we want to explore
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
packages=['tableaudocumentapi'],
license='MIT',
description='A Python module for working with Tableau files.',
test_suite='test'
test_suite='test',
install_requires=['lxml']
)
2 changes: 1 addition & 1 deletion tableaudocumentapi/connection.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import xml.etree.ElementTree as ET
from lxml import etree as ET
from tableaudocumentapi.dbclass import is_valid_dbclass


Expand Down
2 changes: 1 addition & 1 deletion tableaudocumentapi/datasource.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import collections
import itertools
import xml.etree.ElementTree as ET
from lxml import etree as ET
import xml.sax.saxutils as sax
from uuid import uuid4

Expand Down
6 changes: 3 additions & 3 deletions tableaudocumentapi/field.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import functools
import xml.etree.ElementTree as ET
from lxml import etree as ET
from xml.dom import minidom

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

Expand All @@ -298,7 +298,7 @@ def aliases(self):
Returns:
Key-value mappings of all registered aliases. Dict.
"""
aliases_tag = self._xml.find('aliases') or []
aliases_tag = self._xml.find('aliases') or [] # ignore the FutureWarning, does not apply to our usage
return {a.get('key', 'None'): a.get('value', 'None') for a in list(aliases_tag)}

########################################
Expand Down
2 changes: 1 addition & 1 deletion tableaudocumentapi/xfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import shutil
import tempfile
import zipfile
import xml.etree.ElementTree as ET
from lxml import etree as ET

from distutils.version import LooseVersion as Version

Expand Down
6 changes: 3 additions & 3 deletions test/bvt.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import unittest

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


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


class WorkbookModelV10TWBXTests(unittest.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion test/test_field_change.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os.path

from tableaudocumentapi import Datasource
import xml.etree.ElementTree as ET
from lxml import etree as ET


TEST_ASSET_DIR = os.path.join(
Expand Down
13 changes: 1 addition & 12 deletions test/test_xfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,10 @@ def test_save_preserves_namespace_twb(self):
wb.save_as(new_name)
self.assertContainsUserNamespace(new_name)

'''
def demo_bug_ns_not_preserved_if_not_used(self):
filename = TABLEAU_10_TDS
self.assertContainsUserNamespace(filename)
wb = Datasource.from_file(filename)
#wb.save()
new_name = 'saved-as-tds.tds'
wb.save_as(new_name)
self.assertContainsUserNamespace(new_name) <- throws

If there is no namespace in the document when you begin working with it,
none will be added.
If there is a namespace but it *is not used* in the document, it will be stripped

Fix will be something like
https://stackoverflow.com/questions/41937624/elementtree-why-are-my-namespace-declarations-stripped-out

'''
self.assertContainsUserNamespace(new_name)