-
Notifications
You must be signed in to change notification settings - Fork 181
Initial attempt at enabling reading the columns from the datasource #45
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
Merged
graysonarts
merged 8 commits into
tableau:development
from
graysonarts:feature-get-fields
Jul 1, 2016
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d3a120b
Initial attempt at enabling reading the columns from the datasource
b86e869
Fixing pep8 errors for EOFEOL
a4cf3b3
Changing to OrderedDict for getting columns
99d6ccd
Add documentation for the various column attributes
bf284d4
rename column to field
d80696c
Fixed #46 encode apostrophes in field names
2de54b1
Enable multilook up for Fields
2deb58d
Rename properties on the field based on feedback given in #45
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,7 @@ | |
|
|
||
| setup( | ||
| name='tableaudocumentapi', | ||
| version='0.1.0-dev', | ||
| version='0.1.0.dev0', | ||
| author='Tableau Software', | ||
| author_email='[email protected]', | ||
| url='https://github.com/tableau/document-api-python', | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| from .field import Field | ||
| from .connection import Connection | ||
| from .datasource import Datasource, ConnectionParser | ||
| from .workbook import Workbook | ||
|
|
||
| __version__ = '0.0.1' | ||
| __VERSION__ = __version__ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import functools | ||
|
|
||
| _ATTRIBUTES = [ | ||
| 'id', # Name of the field as specified in the file, usually surrounded by [ ] | ||
| 'caption', # Name of the field as displayed in Tableau unless an aliases is defined | ||
| 'datatype', # Type of the field within Tableau (string, integer, etc) | ||
| 'role', # Dimension or Measure | ||
| 'type', # three possible values: quantitative, ordinal, or nominal | ||
| 'alias', # Name of the field as displayed in Tableau if the default name isn't wanted | ||
| 'calculation', # If this field is a calculated field, this will be the formula | ||
| ] | ||
|
|
||
| _METADATA_ATTRIBUTES = [ | ||
| 'aggregation', # The type of aggregation on the field (e.g Sum, Avg) | ||
| ] | ||
|
|
||
|
|
||
| def _find_metadata_record(record, attrib): | ||
| element = record.find('.//{}'.format(attrib)) | ||
| if element is None: | ||
| return None | ||
| return element.text | ||
|
|
||
|
|
||
| class Field(object): | ||
| """ Represents a field in a datasource """ | ||
|
|
||
| def __init__(self, xmldata): | ||
| for attrib in _ATTRIBUTES: | ||
| self._apply_attribute(xmldata, attrib, lambda x: xmldata.attrib.get(x, None)) | ||
|
|
||
| # All metadata attributes begin at None | ||
| for attrib in _METADATA_ATTRIBUTES: | ||
| setattr(self, '_{}'.format(attrib), None) | ||
|
|
||
| def apply_metadata(self, metadata_record): | ||
| for attrib in _METADATA_ATTRIBUTES: | ||
| self._apply_attribute(metadata_record, attrib, functools.partial(_find_metadata_record, metadata_record)) | ||
|
|
||
| @classmethod | ||
| def from_xml(cls, xmldata): | ||
| return cls(xmldata) | ||
|
|
||
| def __getattr__(self, item): | ||
| private_name = '_{}'.format(item) | ||
| if item in _ATTRIBUTES or item in _METADATA_ATTRIBUTES: | ||
| return getattr(self, private_name) | ||
| raise AttributeError(item) | ||
|
|
||
| def _apply_attribute(self, xmldata, attrib, default_func): | ||
| if hasattr(self, '_read_{}'.format(attrib)): | ||
| value = getattr(self, '_read_{}'.format(attrib))(xmldata) | ||
| else: | ||
| value = default_func(attrib) | ||
|
|
||
| setattr(self, '_{}'.format(attrib), value) | ||
|
|
||
| @property | ||
| def name(self): | ||
| """ Provides a nice name for the field which is derived from the alias, caption, or the id. | ||
|
|
||
| The name resolves as either the alias if it's defined, or the caption if alias is not defined, | ||
| and finally the id which is the underlying name if neither of the fields exist. """ | ||
| alias = getattr(self, 'alias', None) | ||
| if alias: | ||
| return alias | ||
|
|
||
| caption = getattr(self, 'caption', None) | ||
| if caption: | ||
| return caption | ||
|
|
||
| return self.id | ||
|
|
||
| ###################################### | ||
| # Special Case handling methods for reading the values from the XML | ||
| ###################################### | ||
| @staticmethod | ||
| def _read_id(xmldata): | ||
| # ID is actually the name of the field, but to provide a nice name, we call this ID | ||
| return xmldata.attrib.get('name', None) | ||
|
|
||
| @staticmethod | ||
| def _read_calculation(xmldata): | ||
| # The formula for a calculation is stored in a child element, so we need to pull it out separately. | ||
| calc = xmldata.find('.//calculation') | ||
| if calc is None: | ||
| return None | ||
|
|
||
| return calc.attrib.get('formula', None) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| def _resolve_value(key, value): | ||
| try: | ||
| retval = value.get(key, None) | ||
| if retval is None: | ||
| retval = value.getattr(key, None) | ||
| except AttributeError: | ||
| retval = None | ||
| return retval | ||
|
|
||
|
|
||
| def _build_index(key, d): | ||
| return {_resolve_value(key, v): k | ||
| for k, v in d.items() | ||
| if _resolve_value(key, v) is not None} | ||
|
|
||
|
|
||
| # TODO: Improve this to be more generic | ||
| class MultiLookupDict(dict): | ||
| def __init__(self, args=None): | ||
| if args is None: | ||
| args = {} | ||
| super(MultiLookupDict, self).__init__(args) | ||
| self._indexes = { | ||
| 'alias': {}, | ||
| 'caption': {} | ||
| } | ||
| self._populate_indexes() | ||
|
|
||
| def _populate_indexes(self): | ||
| self._indexes['alias'] = _build_index('alias', self) | ||
| self._indexes['caption'] = _build_index('caption', self) | ||
|
|
||
| def __setitem__(self, key, value): | ||
| alias = _resolve_value('alias', value) | ||
| caption = _resolve_value('caption', value) | ||
| if alias is not None: | ||
| self._indexes['alias'][alias] = key | ||
| if caption is not None: | ||
| self._indexes['caption'][caption] = key | ||
|
|
||
| dict.__setitem__(self, key, value) | ||
|
|
||
| def __getitem__(self, key): | ||
| if key in self._indexes['alias']: | ||
| key = self._indexes['alias'][key] | ||
| elif key in self._indexes['caption']: | ||
| key = self._indexes['caption'][key] | ||
|
|
||
| return dict.__getitem__(self, key) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| from . import bvt | ||
| from . import test_datasource |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| <?xml version='1.0' encoding='utf-8' ?> | ||
| <datasource formatted-name='postgres.1of3kl00aoax5d1a1ejma1397430' inline='true' source-platform='mac' version='9.3' xmlns:user='http://www.tableausoftware.com/xml/user'> | ||
| <connection authentication='username-password' class='postgres' dbname='TestV1' odbc-native-protocol='yes' port='5432' server='postgres91.test.tsi.lan' username='test'> | ||
| <relation name='xy' table='[public].[xy]' type='table' /> | ||
| <metadata-records> | ||
| <metadata-record class='column'> | ||
| <remote-name>a</remote-name> | ||
| <remote-type>130</remote-type> | ||
| <local-name>[a]</local-name> | ||
| <parent-name>[xy]</parent-name> | ||
| <remote-alias>a</remote-alias> | ||
| <ordinal>1</ordinal> | ||
| <local-type>string</local-type> | ||
| <aggregation>Count</aggregation> | ||
| <width>255</width> | ||
| <contains-null>true</contains-null> | ||
| <attributes> | ||
| <attribute datatype='string' name='DebugRemoteType'>"SQL_WVARCHAR"</attribute> | ||
| <attribute datatype='string' name='DebugWireType'>"SQL_C_WCHAR"</attribute> | ||
| <attribute datatype='string' name='TypeIsVarchar'>"true"</attribute> | ||
| </attributes> | ||
| </metadata-record> | ||
| <metadata-record class='column'> | ||
| <remote-name>Today's Date</remote-name> | ||
| <remote-type>130</remote-type> | ||
| <local-name>[Today's Date]</local-name> | ||
| <parent-name>[xy]</parent-name> | ||
| <remote-alias>a</remote-alias> | ||
| <ordinal>1</ordinal> | ||
| <local-type>string</local-type> | ||
| <aggregation>Count</aggregation> | ||
| <width>255</width> | ||
| <contains-null>true</contains-null> | ||
| <attributes> | ||
| <attribute datatype='string' name='DebugRemoteType'>"SQL_WVARCHAR"</attribute> | ||
| <attribute datatype='string' name='DebugWireType'>"SQL_C_WCHAR"</attribute> | ||
| <attribute datatype='string' name='TypeIsVarchar'>"true"</attribute> | ||
| </attributes> | ||
| </metadata-record> | ||
| <metadata-record class='column'> | ||
| <remote-name>x</remote-name> | ||
| <remote-type>3</remote-type> | ||
| <local-name>[x]</local-name> | ||
| <parent-name>[xy]</parent-name> | ||
| <remote-alias>x</remote-alias> | ||
| <ordinal>2</ordinal> | ||
| <local-type>integer</local-type> | ||
| <aggregation>Sum</aggregation> | ||
| <precision>10</precision> | ||
| <contains-null>true</contains-null> | ||
| <attributes> | ||
| <attribute datatype='string' name='DebugRemoteType'>"SQL_INTEGER"</attribute> | ||
| <attribute datatype='string' name='DebugWireType'>"SQL_C_SLONG"</attribute> | ||
| </attributes> | ||
| </metadata-record> | ||
| <metadata-record class='column'> | ||
| <remote-name>y</remote-name> | ||
| <remote-type>3</remote-type> | ||
| <local-name>[y]</local-name> | ||
| <parent-name>[xy]</parent-name> | ||
| <remote-alias>y</remote-alias> | ||
| <ordinal>3</ordinal> | ||
| <local-type>integer</local-type> | ||
| <aggregation>Sum</aggregation> | ||
| <precision>10</precision> | ||
| <contains-null>true</contains-null> | ||
| <attributes> | ||
| <attribute datatype='string' name='DebugRemoteType'>"SQL_INTEGER"</attribute> | ||
| <attribute datatype='string' name='DebugWireType'>"SQL_C_SLONG"</attribute> | ||
| </attributes> | ||
| </metadata-record> | ||
| </metadata-records> | ||
| </connection> | ||
| <aliases enabled='yes' /> | ||
| <column datatype='integer' name='[Number of Records]' role='measure' type='quantitative' user:auto-column='numrec'> | ||
| <calculation class='tableau' formula='1' /> | ||
| </column> | ||
| <column caption='A' datatype='string' name='[a]' role='dimension' type='nominal' /> | ||
| <column caption='Today's Date' datatype='string' name='[Today's Date]' role='dimension' type='nominal' /> | ||
| <column caption='X' datatype='integer' name='[x]' role='measure' type='quantitative' /> | ||
| <column caption='Y' datatype='integer' name='[y]' role='measure' type='quantitative' /> | ||
| <layout dim-ordering='alphabetic' dim-percentage='0.5' measure-ordering='alphabetic' measure-percentage='0.5' show-structure='true' /> | ||
| <semantic-values> | ||
| <semantic-value key='[Country].[Name]' value='"United States"' /> | ||
| </semantic-values> | ||
| </datasource> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import unittest | ||
| import os.path | ||
| import functools | ||
|
|
||
| from tableaudocumentapi import Datasource | ||
|
|
||
| TEST_TDS_FILE = os.path.join( | ||
| os.path.dirname(__file__), | ||
| 'assets', | ||
| 'datasource_test.tds' | ||
| ) | ||
|
|
||
|
|
||
| class DataSourceFields(unittest.TestCase): | ||
| def setUp(self): | ||
| self.ds = Datasource.from_file(TEST_TDS_FILE) | ||
|
|
||
| def test_datasource_returns_correct_fields(self): | ||
| self.assertIsNotNone(self.ds.fields) | ||
| self.assertIsNotNone(self.ds.fields.get('[Number of Records]', None)) | ||
|
|
||
| def test_datasource_returns_calculation_from_fields(self): | ||
| self.assertEqual('1', self.ds.fields['[Number of Records]'].calculation) | ||
|
|
||
| def test_datasource_uses_metadata_record(self): | ||
| self.assertEqual('Sum', self.ds.fields['[x]'].aggregation) | ||
|
|
||
| def test_datasource_column_name_contains_apostrophy(self): | ||
| self.assertIsNotNone(self.ds.fields.get("[Today's Date]", None)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import unittest | ||
| import os.path | ||
| import functools | ||
|
|
||
| from tableaudocumentapi.multilookup_dict import MultiLookupDict | ||
|
|
||
|
|
||
| class MLDTests(unittest.TestCase): | ||
| def setUp(self): | ||
| self.mld = MultiLookupDict({ | ||
| '[foo]': { | ||
| 'alias': 'bar', | ||
| 'caption': 'baz', | ||
| 'value': 1 | ||
| }, | ||
| '[bar]': { | ||
| 'caption': 'foo', | ||
| 'value': 2 | ||
| }, | ||
| '[baz]': { | ||
| 'value': 3 | ||
| } | ||
| }) | ||
|
|
||
| def test_multilookupdict_name_only(self): | ||
| actual = self.mld['[baz]'] | ||
| self.assertEqual(3, actual['value']) | ||
|
|
||
| def test_multilookupdict_alias_overrides_everything(self): | ||
| actual = self.mld['bar'] | ||
| self.assertEqual(1, actual['value']) | ||
|
|
||
| def test_mutlilookupdict_caption_overrides_id(self): | ||
| actual = self.mld['foo'] | ||
| self.assertEqual(2, actual['value']) | ||
|
|
||
| def test_mutlilookupdict_can_still_find_id_even_with_alias(self): | ||
| actual = self.mld['[foo]'] | ||
| self.assertEqual(1, actual['value']) | ||
|
|
||
| def test_mutlilookupdict_can_still_find_caption_even_with_alias(self): | ||
| actual = self.mld['baz'] | ||
| self.assertEqual(1, actual['value']) | ||
|
|
||
| def test_mutlilookupdict_can_still_find_id_even_with_caption(self): | ||
| actual = self.mld['[bar]'] | ||
| self.assertEqual(2, actual['value']) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't address the renaming, I'm working on that next.