Skip to content

Commit

Permalink
Merged version-605: Supply a versatile version attribute, useful pr…
Browse files Browse the repository at this point in the history
…ogramatically and stringified.

Fixes: #605
Author: thijs, nick
Reviewer: nick, thijs

git-svn-id: https://svn.pyamf.org/pyamf/trunk@3063 2dde4cc4-cf3c-0410-b1a3-a9b8ff274da5
  • Loading branch information
njoyce committed Dec 28, 2009
1 parent 59c3b3a commit 992aedb
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 47 deletions.
18 changes: 18 additions & 0 deletions doc/library/pyamf.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
:mod:`pyamf` --- pyamf
=============================================

.. automodule:: pyamf
:members:
:synopsis: AMF support for Python.

.. data:: version

PyAMF version number.

>>> import pyamf
>>> pyamf.__version__ is pyamf.version
True
>>> pyamf.version
(0, 6, 1, 'rc1')
>>> str(pyamf.version)
'0.6.1rc1'
23 changes: 2 additions & 21 deletions pyamf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import types
import inspect

from pyamf import util
from pyamf import util, versions as v
from pyamf.adapters import register_adapters

try:
Expand All @@ -37,7 +37,7 @@
]

#: PyAMF version number.
__version__ = (0, 6)
__version__ = version = v.Version(0, 6)

#: Class mapping support.
CLASS_CACHE = {}
Expand Down Expand Up @@ -573,17 +573,10 @@ def getEncodableAttributes(self, obj, codec=None):
if self.non_static_encodable_properties:
for attr in self.non_static_encodable_properties:
attrs[attr] = getattr(obj, attr)
<<<<<<< HEAD

if not attrs:
attrs = None

=======

if not attrs:
attrs = None

>>>>>>> master
return attrs

dynamic_props = util.get_properties(obj)
Expand All @@ -606,17 +599,6 @@ def getEncodableAttributes(self, obj, codec=None):
else:
for attr in dynamic_props:
attrs[attr] = getattr(obj, attr)
<<<<<<< HEAD

if self.proxy_attrs is not None and attrs:
for k, v in attrs.copy().iteritems():
if k in self.proxy_attrs:
attrs[k] = self.getProxiedAttribute(k, v)

if not attrs:
attrs = None

=======

if self.proxy_attrs is not None and attrs:
for k, v in attrs.copy().iteritems():
Expand All @@ -626,7 +608,6 @@ def getEncodableAttributes(self, obj, codec=None):
if not attrs:
attrs = None

>>>>>>> master
return attrs

def getDecodableAttributes(self, obj, attrs, codec=None):
Expand Down
1 change: 1 addition & 0 deletions pyamf/adapters/_sqlalchemy_orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def createInstance(self, *args, **kwargs):

return self.mapper.class_manager.new_instance()


def is_class_sa_mapped(klass):
"""
@rtype: C{bool}
Expand Down
5 changes: 2 additions & 3 deletions pyamf/remoting/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
from pyamf import remoting


#: Default user agent is C{PyAMF/x.x.x}.
DEFAULT_USER_AGENT = 'PyAMF/%s' % '.'.join(map(lambda x: str(x),
pyamf.__version__))
#: Default user agent is C{PyAMF/x.x(.x)}.
DEFAULT_USER_AGENT = 'PyAMF/%s' % (pyamf.version,)

HTTP_OK = 200

Expand Down
2 changes: 1 addition & 1 deletion pyamf/remoting/gateway/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
impl = 'Python'

SERVER_NAME = 'PyAMF/%s %s/%s' % (
'.'.join(map(lambda x: str(x), pyamf.__version__)), impl,
pyamf.version, impl,
'.'.join(map(lambda x: str(x), sys.version_info[0:3]))
)

Expand Down
6 changes: 3 additions & 3 deletions pyamf/tests/adapters/test_google.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ def test_get_attributes_saved(self):
def test_get_attributes_expando(self):
attrs = self.alias.getEncodableAttributes(self.jessica_expando)

self.assertEquals(sa, {
self.assertEquals(attrs, {
'name': 'Jessica',
'_key': None,
'type': 'cat',
Expand All @@ -755,7 +755,7 @@ def test_get_attributes_saved_expando(self):

attrs = self.alias.getEncodableAttributes(self.jessica_expando)

self.assertEquals(sa, {
self.assertEquals(attrs, {
'name': 'Jessica',
'_key': str(self.jessica_expando.key()),
'type': 'cat',
Expand All @@ -770,7 +770,7 @@ def test_arbitrary_properties(self):

attrs = self.alias.getEncodableAttributes(self.jessica)

self.assertEquals(sa, {
self.assertEquals(attrs, {
'_key': None,
'type': 'cat',
'name': 'Jessica',
Expand Down
56 changes: 56 additions & 0 deletions pyamf/tests/test_versions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""
Tests for L{pyamf.version}
"""

import unittest

from pyamf import versions


class VersionTestCase(unittest.TestCase):
"""
Tests for L{pyamf.version.get_version}
"""

def test_version(self):
self.assertEquals(versions.get_version((0, 0)), '0.0')
self.assertEquals(versions.get_version((0, 1)), '0.1')
self.assertEquals(versions.get_version((3, 2)), '3.2')
self.assertEquals(versions.get_version((3, 2, 1)), '3.2.1')

self.assertEquals(versions.get_version((3, 2, 1, 'alpha')), '3.2.1alpha')
self.assertEquals(versions.get_version((3, 2, 1, 'alpha', 0)), '3.2.1 pre-alpha')

self.assertEquals(versions.get_version((3, 2, 1, 'final')), '3.2.1final')
self.assertEquals(versions.get_version((3, 2, 1, 'beta', 1234)), '3.2.1beta1234')

def test_class(self):
V = versions.Version

v1 = V(0, 1)

self.assertEquals(v1, (0, 1))
self.assertEquals(str(v1), '0.1')

v2 = V(3, 2, 1, 'final')

self.assertEquals(v2, (3, 2, 1, 'final'))
self.assertEquals(str(v2), '3.2.1final')

self.assertTrue(v2 > v1)


def suite():
suite = unittest.TestSuite()

test_cases = [
VersionTestCase,
]

for tc in test_cases:
suite.addTest(unittest.makeSuite(tc))

return suite

if __name__ == '__main__':
unittest.main(defaultTest='suite')
40 changes: 40 additions & 0 deletions pyamf/versions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
Because there is disparity between Python packaging (and it is being sorted
out ...) we currently provide our own way to get the string of a version tuple.
"""


class Version(tuple):
"""
"""

_version = None

def __new__(cls, *args):
x = tuple.__new__(cls, args)

return x

def __str__(self):
if not self._version:
self._version = get_version(self)

return self._version


def get_version(v):
version = '%s.%s' % (v[0], v[1])

try:
if v[2]:
version = '%s.%s' % (version, v[2])
if v[3:] == ('alpha', 0):
version = '%s pre-alpha' % version
else:
version = '%s%s' % (version, v[3])
if v[3] != 'final':
version = '%s%s' % (version, v[4])
except IndexError:
pass

return version
28 changes: 9 additions & 19 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
from setuptools.command.build_ext import build_ext


if __name__ == '__main__':
base_path = os.path.dirname(os.path.normpath(os.path.abspath(__file__)))

sys.path.insert(0, base_path)


class TestCommand(test.test):
def run_twisted(self):
from twisted.trial import runner
Expand Down Expand Up @@ -45,26 +51,10 @@ def get_version():
@since: 0.4
"""
# we read the file instead of importing it as root sometimes does not
# have the cwd as part of the PYTHONPATH

fn = os.path.join(os.getcwd(), 'pyamf', '__init__.py')
lines = open(fn, 'rt').readlines()

version = None

for l in lines:
# include the ' =' as __version__ is a part of __all__
if l.startswith('__version__ =', ):
x = compile(l, fn, 'single')
eval(x)
version = locals()['__version__']
break

if version is None:
raise RuntimeError('Couldn\'t determine version number')
# since the basedir is set as the first option in sys.path, this works
import pyamf

return '.'.join([str(x) for x in version])
return str(pyamf.version)


def get_cpyamf_extensions():
Expand Down

0 comments on commit 992aedb

Please sign in to comment.