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

WIP: Refactor set item #19

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
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
106 changes: 53 additions & 53 deletions genologics/descriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@

from genologics.constants import nsmap

try:
from urllib.parse import urlsplit, urlparse, parse_qs, urlunparse
except ImportError:
from urlparse import urlsplit, urlparse, parse_qs, urlunparse

from decimal import Decimal
import datetime
import time
Expand Down Expand Up @@ -156,10 +151,7 @@ class UdfDictionary(object):
"Dictionary-like container of UDFs, optionally within a UDT."

def _is_string(self, value):
try:
return isinstance(value, basestring)
except:
return isinstance(value, str)
return isinstance(value, str)

def __init__(self, instance, *args, **kwargs):
self.instance = instance
Expand Down Expand Up @@ -236,56 +228,64 @@ def __contains__(self, key):
def __getitem__(self, key):
return self._lookup[key]

def _validate_and_parse(self, vtype, value):
"""
Validates that the `value` is valid for vtype and returns a valid string representation
of the value that can be accepted by the API.
"""
if value is None:
value = ''
elif vtype == 'string' or vtype == 'str':
if not self._is_string(value):
raise TypeError('String UDF requires a string')
elif vtype == 'text':
if not self._is_string(value):
raise TypeError('Text UDF requires a string')
elif vtype == 'numeric':
if not isinstance(value, (int, float, Decimal)) and value != '':
raise TypeError('Numeric UDF requires int or float value')
else:
value = str(value)
elif vtype == 'boolean':
if not isinstance(value, bool):
raise TypeError('Boolean UDF requires bool value')
value = 'true' if value else 'false'
elif vtype == 'date':
if not isinstance(value, datetime.date): # Too restrictive?
raise TypeError('Date UDF requires datetime.date value')
value = str(value)
elif vtype == 'uri':
if not self._is_string(value):
raise TypeError('URI UDF requires a string')
value = str(value)
elif not isinstance(value, str):
if not self._is_string(value):
value = str(value).encode('UTF-8')
else:
raise NotImplementedError("UDF type '%s'" % vtype)

return value

def __setitem__(self, key, value):
self._lookup[key] = value

found_node = None

for node in self._elems:
if node.attrib['name'] != key: continue
vtype = node.attrib['type'].lower()
if node.attrib['name'] != key:
continue
found_node = node

if value is None:
value=''
elif vtype == 'string':
if not self._is_string(value):
raise TypeError('String UDF requires str or unicode value')
elif vtype == 'str':
if not self._is_string(value):
raise TypeError('String UDF requires str or unicode value')
elif vtype == 'text':
if not self._is_string(value):
raise TypeError('Text UDF requires str or unicode value')
elif vtype == 'numeric':
if not isinstance(value, (int, float, Decimal)) and value != '':
raise TypeError('Numeric UDF requires int or float value')
else:
value = str(value)
elif vtype == 'boolean':
if not isinstance(value, bool):
raise TypeError('Boolean UDF requires bool value')
value = value and 'true' or 'false'
elif vtype == 'date':
if not isinstance(value, datetime.date): # Too restrictive?
raise TypeError('Date UDF requires datetime.date value')
value = str(value)
elif vtype == 'uri':
if not self._is_string(value):
raise TypeError('URI UDF requires str or punycode (unicode) value')
value = str(value)
else:
raise NotImplemented("UDF type '%s'" % vtype)
if value is None:
node.text = ''
else:
if not isinstance(value, str):
if not self._is_string(value):
value = str(value).encode('UTF-8')
node.text = value
break
else: # Create new entry; heuristics for type
if found_node:
vtype = found_node.attrib['type'].lower()
found_node.text = self._validate_and_parse(vtype, value)
else:
# Create new entry; heuristics for type
if self._is_string(value):
vtype = '\n' in value and 'Text' or 'String'
vtype = 'Text' if '\n' in value else 'String'
elif isinstance(value, bool):
vtype = 'Boolean'
value = value and 'true' or 'false'
value = 'true' if value else 'false'
elif isinstance(value, (int, float, Decimal)):
vtype = 'Numeric'
value = str(value)
Expand All @@ -309,7 +309,7 @@ def __setitem__(self, key, value):

elem.text = value

#update the internal elements and lookup with new values
# update the internal elements and lookup with new values
self._update_elems()
self._prepare_lookup()

Expand Down