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

Initial attempt at enabling reading the columns from the datasource #45

Merged
merged 8 commits into from
Jul 1, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion tableaudocumentapi/datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from tableaudocumentapi import Connection, xfile
from tableaudocumentapi import Field
from tableaudocumentapi.multilookup_dict import MultiLookupDict
Copy link
Contributor Author

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.



def _mapping_from_xml(root_xml, column_xml):
Expand Down Expand Up @@ -142,4 +143,4 @@ def fields(self):
def _get_all_fields(self):
column_objects = (_mapping_from_xml(self._datasourceTree, xml)
for xml in self._datasourceTree.findall('.//column'))
return collections.OrderedDict([(k, v) for k, v in column_objects])
return MultiLookupDict({k: v for k, v in column_objects})
49 changes: 49 additions & 0 deletions tableaudocumentapi/multilookup_dict.py
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)
47 changes: 47 additions & 0 deletions test/test_multidict.py
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'])