Skip to content

Commit

Permalink
Python 3 related fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
katyukha committed Jul 31, 2015
1 parent 5b89e18 commit cf18127
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 23 deletions.
4 changes: 2 additions & 2 deletions openerp_proxy/orm/cache.py
Expand Up @@ -113,15 +113,15 @@ def prefetch_fields(self, fields):

col_info = self._object.columns_info
for data in self._object.read(list(self), to_prefetch):
for field, value in data.iteritems():
for field, value in data.items():

# Fill related cache
ftype = col_info.get(field, {}).get('type', None)
self.cache_field(data['id'], ftype, field, value)

if related:
# TODO: think how to avoid infinite recursion and double reads
for obj_name, rfields in related.viewitems():
for obj_name, rfields in related.items():
self._root_cache[obj_name].prefetch_fields(rfields)


Expand Down
7 changes: 5 additions & 2 deletions openerp_proxy/orm/object.py
Expand Up @@ -25,6 +25,7 @@ def get_object(proxy, name):


# TODO: think about connecting it to service instead of Proxy
@six.python_2_unicode_compatible
class Object(six.with_metaclass(ObjectType)):
""" Base class for all Objects
Expand Down Expand Up @@ -87,8 +88,10 @@ def wrapper(*args, **kwargs):
return getattr(self, name)

def __str__(self):
return "Object ('%s')" % self.name
__repr__ = __str__
return u"Object ('%s')" % self.name

def __repr__(self):
return str(self)

def __eq__(self, other):
return self.name == other.name and self.proxy == other.proxy
Expand Down
21 changes: 12 additions & 9 deletions openerp_proxy/orm/record.py
Expand Up @@ -39,6 +39,7 @@ def get_record(obj, rid, cache=None, context=None):
return RecordMeta.get_object(obj, rid, cache=cache, context=context)


@six.python_2_unicode_compatible
class Record(six.with_metaclass(RecordMeta, object)):
""" Base class for all Records
Expand Down Expand Up @@ -124,13 +125,10 @@ def _name(self):
data = self._object.name_get(list(lcache), context=self.context)
for _id, name in data:
lcache[_id]['__name_get_result'] = name
return self._data.get('__name_get_result', 'ERROR')

def __unicode__(self):
return u"R(%s, %s)[%s]" % (self._object.name, self.id, ustr(self._name))
return self._data.get('__name_get_result', u'ERROR')

def __str__(self):
return unicode(self).encode('utf-8')
return u"R(%s, %s)[%s]" % (self._object.name, self.id, ustr(self._name))

def __repr__(self):
return str(self)
Expand Down Expand Up @@ -256,6 +254,7 @@ def get_record_list(obj, ids=None, fields=None, cache=None, context=None):
return RecordListMeta.get_object(obj, ids, fields=fields, cache=cache, context=context)


@six.python_2_unicode_compatible
class RecordList(six.with_metaclass(RecordListMeta, collections.MutableSequence)):
"""Class to hold list of records with some extra functionality
Expand Down Expand Up @@ -397,8 +396,10 @@ def __getattr__(self, name):
return res

def __str__(self):
return "RecordList(%s): length=%s" % (self.object.name, self.length)
__repr__ = __str__
return u"RecordList(%s): length=%s" % (self.object.name, self.length)

def __repr__(self):
return str(self)

def refresh(self):
""" Cleanup data caches. next try to get data will cause rereading of it
Expand All @@ -410,13 +411,15 @@ def refresh(self):
record.refresh()
return self

def sort(self, cmp=None, key=None, reverse=False):
def sort(self, *args, **kwargs):
""" sort(cmp=None, key=None, reverse=False) -- inplace sort
cmp(x, y) -> -1, 0, 1
Note, that 'cmp' argument, not available for python 3
:return: self
"""
self._records.sort(cmp=cmp, key=key, reverse=reverse)
self._records.sort(*args, **kwargs)
return self

def copy(self, context=None, new_cache=False):
Expand Down
5 changes: 5 additions & 0 deletions openerp_proxy/tests/__init__.py
@@ -1,3 +1,4 @@
import six
import unittest
from openerp_proxy.utils import AttrDict

Expand Down Expand Up @@ -26,3 +27,7 @@ def setUp(self):
for ext in with_extensions.split(','):
__import__(ext)

if six.PY3:
def assertItemsEqual(self, *args, **kwargs):
return self.assertCountEqual(*args, **kwargs)

29 changes: 19 additions & 10 deletions run_tests.bash
Expand Up @@ -4,13 +4,22 @@ SCRIPT=`readlink -f "$0"`
# Absolute path this script is in, thus /home/user/bin
SCRIPTPATH=`dirname "$SCRIPT"`

(cd $SCRIPTPATH && \
virtualenv venv_test && \
source ./venv_test/bin/activate && \
pip install --upgrade pip setuptools coverage mock pudb ipython && \
python setup.py develop && \
rm -f .coverage && \
coverage run --source openerp_proxy -m unittest -v openerp_proxy.tests.all && \
coverage html -d coverage && \
deactivate && \
rm -rf venv_test)
function test_it {
local py_version=$1;
(cd $SCRIPTPATH && \
virtualenv venv_test -p python${py_version} && \
source ./venv_test/bin/activate && \
pip install --upgrade pip setuptools coverage mock pudb ipython && \
python setup.py develop && \
rm -f .coverage && \
coverage run --source openerp_proxy -m unittest -v openerp_proxy.tests.all && \
coverage html -d coverage && \
deactivate && \
rm -rf venv_test)
}

PY_VERSIONS=${PY_VERSIONS:-"2.7 3.4"};

for version in $PY_VERSIONS; do
test_it $version;
done

0 comments on commit cf18127

Please sign in to comment.