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

Python3 support #53

Merged
merged 17 commits into from
Jan 22, 2014
Merged
Show file tree
Hide file tree
Changes from 16 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
51 changes: 44 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,50 @@ language: python

python:
- 2.7
- 3.3

env:
- DJANGO_VERSION=1.4.10
- DJANGO_VERSION=1.5.5
- DJANGO_VERSION=1.6
matrix:
- DJANGO_VERSION=1.4.10 DATABASE=sqlite
- DJANGO_VERSION=1.5.5 DATABASE=postgres
- DJANGO_VERSION=1.5.5 DATABASE=sqlite
- DJANGO_VERSION=1.6 DATABASE=postgres
- DJANGO_VERSION=1.6 DATABASE=sqlite
- DJANGO_VERSION=dev DATABASE=postgres
- DJANGO_VERSION=dev DATABASE=sqlite

matrix:
exclude:
- python: 3.3
env: DJANGO_VERSION=1.4.10 DATABASE=sqlite
- python: 3.3
env: DJANGO_VERSION=1.5.5 DATABASE=sqlite
- python: 3.3
env: DJANGO_VERSION=1.5.5 DATABASE=postgres
- python: 3.3
env: DJANGO_VERSION=1.6 DATABASE=sqlite
- python: 3.3
env: DJANGO_VERSION=dev DATABASE=sqlite

before_install:
- sudo apt-get update

install:
# GeoDjango dependencies
- sudo apt-get install -y python-software-properties
- sudo apt-add-repository -y ppa:ubuntugis/ppa
- sudo apt-get update > /dev/null
- sudo apt-get install -y libgdal-dev libproj-dev libgeos-dev libspatialite-dev
- pip install git+git://github.com/tinio/pysqlite.git@extension-enabled#egg=pysqlite
- if [[ $DATABASE == sqlite ]]; then pip
install git+git://github.com/tinio/pysqlite.git@extension-enabled#egg=pysqlite; fi
- if [[ $DATABASE == postgres ]]; then pip install psycopg2; fi

# This is a dependency of our Django test script
- pip install argparse --use-mirrors
- if [[ $DJANGO_VERSION == dev ]]; then pip
install https://github.com/django/django/tarball/master/django.tar.gz#egg=django; else pip
install -q Django==$DJANGO_VERSION --use-mirrors; fi

- pip install -q Django==$DJANGO_VERSION --use-mirrors

- pip install coverage

Expand All @@ -30,12 +56,23 @@ install:

before_script:
- flake8 --ignore=E501 leaflet
- if [[ $DATABASE == postgres ]]; then psql -c 'create database test_db;' -U postgres; fi
- if [[ $DATABASE == postgres ]]; then psql -c 'CREATE EXTENSION postgis;' -U postgres -d test_db; fi
- if [[ $DATABASE == postgres ]]; then psql -c 'CREATE EXTENSION postgis_topology;' -U postgres -d test_db; fi

script:
- python quicktest.py leaflet
- python quicktest.py leaflet --db=$DATABASE
- node node_modules/django-leaflet-tests/node_modules/mocha-phantomjs/bin/mocha-phantomjs leaflet/tests/index.html

after_success:
- coverage run quicktest.py leaflet
- coverage run quicktest.py leaflet --db=$DATABASE
- pip install coveralls
- coveralls

allow_failures:
- python: 3.3
env: DJANGO_VERSION=dev DATABASE=postgres
- python: 2.7
env: DJANGO_VERSION=dev DATABASE=sqlite
- python: 2.7
env: DJANGO_VERSION=dev DATABASE=postgres
22 changes: 14 additions & 8 deletions leaflet/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# -*- coding: utf8 -*-
import urlparse
from __future__ import unicode_literals

try:
from urllib.parse import urlparse, urljoin
except ImportError:
from urlparse import urlparse, urljoin
import warnings

try:
Expand All @@ -11,10 +16,11 @@
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.utils.translation import ugettext_lazy as _
from django.utils import six


DEFAULT_TILES = [(_('OSM'), 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
u'© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors')]
'© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors')]

LEAFLET_CONFIG = getattr(settings, 'LEAFLET_CONFIG', {})

Expand Down Expand Up @@ -43,7 +49,7 @@


# If TILES is a string, convert to tuple
if isinstance(app_settings.get('TILES'), basestring):
if isinstance(app_settings.get('TILES'), six.string_types):
app_settings['TILES'] = [(_('Background'), app_settings.get('TILES'), '')]

SPATIAL_EXTENT = app_settings.get("SPATIAL_EXTENT")
Expand Down Expand Up @@ -82,7 +88,7 @@


DEFAULT_ZOOM = app_settings['DEFAULT_ZOOM']
if DEFAULT_ZOOM is not None and not (isinstance(DEFAULT_ZOOM, int) and (1 <= DEFAULT_ZOOM <= 24)):
if DEFAULT_ZOOM is not None and not (isinstance(DEFAULT_ZOOM, six.integer_types) and (1 <= DEFAULT_ZOOM <= 24)):
raise ImproperlyConfigured("LEAFLET_CONFIG['DEFAULT_ZOOM'] must be an int between 1 and 24.")


Expand Down Expand Up @@ -130,7 +136,7 @@ def _normalize_plugins_config():
if '__is_normalized__' in PLUGINS: # already normalized
return

listed_plugins = PLUGINS.keys()
listed_plugins = list(PLUGINS.keys())
PLUGINS[PLUGINS_DEFAULT] = OrderedDict()
PLUGINS[PLUGIN_ALL] = OrderedDict()

Expand All @@ -142,7 +148,7 @@ def _normalize_plugins_config():
for resource_type in RESOURCE_TYPE_KEYS:
# normalize the resource URLs
urls = plugin_dict.get(resource_type, None)
if isinstance(urls, (str, unicode)):
if isinstance(urls, (six.binary_type, six.string_types)):
urls = [urls]
elif isinstance(urls, tuple): # force to list
urls = list(urls)
Expand All @@ -153,12 +159,12 @@ def _normalize_plugins_config():

# normalize the URLs - see the docstring for details
for i, url in enumerate(urls):
url_parts = urlparse.urlparse(url)
url_parts = urlparse(url)
if url_parts.scheme or url_parts.path.startswith('/'):
# absolute URL or a URL starting at root
pass
else:
urls[i] = urlparse.urljoin(settings.STATIC_URL, url)
urls[i] = urljoin(settings.STATIC_URL, url)

plugin_dict[resource_type] = urls

Expand Down
3 changes: 3 additions & 0 deletions leaflet/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# -*- coding: utf8 -*-
from __future__ import unicode_literals

from django.contrib.admin import ModelAdmin
from django.contrib.gis.db import models

Expand Down
2 changes: 2 additions & 0 deletions leaflet/forms/fields.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import unicode_literals

import django

if django.VERSION >= (1, 6, 0):
Expand Down
3 changes: 3 additions & 0 deletions leaflet/forms/widgets.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django import forms
try:
from django.contrib.gis.forms.widgets import BaseGeometryWidget
Expand Down
8 changes: 6 additions & 2 deletions leaflet/templatetags/leaflet_tags.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# -*- coding: utf8 -*-
from __future__ import unicode_literals

import json

from django import template
from django.conf import settings
from django.utils import six

from leaflet import (app_settings, SPATIAL_EXTENT, SRID, PLUGINS, PLUGINS_DEFAULT,
PLUGIN_ALL, PLUGIN_FORMS)
Expand Down Expand Up @@ -64,7 +68,7 @@ def leaflet_map(name, callback=None, fitextent=True, creatediv=True, loadevent='
fitextent=fitextent,
center=app_settings['DEFAULT_CENTER'],
zoom=app_settings['DEFAULT_ZOOM'],
layers=[(unicode(label), url, attrs) for (label, url, attrs) in app_settings.get('TILES')],
layers=[(six.text_type(label), url, attrs) for (label, url, attrs) in app_settings.get('TILES')],
attributionprefix=app_settings.get('ATTRIBUTION_PREFIX'),
scale=app_settings.get('SCALE'),
minimap=app_settings.get('MINIMAP'),
Expand Down Expand Up @@ -104,7 +108,7 @@ def _get_plugin_names(plugin_names_from_tag_parameter):
:param pluging_names_parameter:
:return:
"""
if isinstance(plugin_names_from_tag_parameter, (str, unicode)):
if isinstance(plugin_names_from_tag_parameter, (six.binary_type, six.text_type)):
names = plugin_names_from_tag_parameter.split(',')
return [n.strip() for n in names]
else:
Expand Down
2 changes: 1 addition & 1 deletion leaflet/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from tests import * # noqa
from .tests import * # noqa
3 changes: 3 additions & 0 deletions leaflet/tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# -*- coding: utf8 -*-
from __future__ import unicode_literals

import django
from django.test import SimpleTestCase
from django.contrib.gis.db import models as gismodels
Expand Down
45 changes: 31 additions & 14 deletions quicktest.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,64 @@
# -*- coding: utf8 -*-
from __future__ import unicode_literals

import os
import sys
import argparse
from django.conf import settings
import django

class QuickDjangoTest(object):
"""
A quick way to run the Django test suite without a fully-configured project.

Example usage:

>>> QuickDjangoTest('app1', 'app2')
>>> QuickDjangoTest(apps=['app1', 'app2'], db='sqlite')

Based on a script published by Lukasz Dziedzia at:
http://stackoverflow.com/questions/3841725/how-to-launch-tests-for-django-reusable-app
"""
DIRNAME = os.path.dirname(__file__)
INSTALLED_APPS = (
INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.admin',
)
]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why switch to list ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.apps is a list now so I cannot concatenate it to a tuple.


def __init__(self, *args, **kwargs):
self.apps = args
self.apps = kwargs.get('apps', [])
self.database= kwargs.get('db', 'sqlite')
self.run_tests()

def run_tests(self):
"""
Fire up the Django test suite developed for version 1.2
"""
settings.configure(
DATABASES={
if self.database == 'postgres':
databases = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'test_db',
'HOST': '127.0.0.1',
'USER': 'postgres',
'PASSWORD': '',
}
}

else:
databases = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.spatialite',
'NAME': os.path.join(self.DIRNAME, 'database.db'),
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
},
}
settings.configure(
DATABASES=databases,
INSTALLED_APPS=self.INSTALLED_APPS + self.apps,
)
if django.VERSION >= (1, 7, 0):
django.setup()
from django.test.simple import DjangoTestSuiteRunner
failures = DjangoTestSuiteRunner().run_tests(self.apps, verbosity=1)
if failures: # pragma: no cover
Expand All @@ -54,13 +70,14 @@ def run_tests(self):

Example usage:

$ python quicktest.py app1 app2
$ python quicktest.py app1 app2 --db=sqlite

"""
parser = argparse.ArgumentParser(
usage="[args]",
usage="[args] [--db=sqlite]",
description="Run Django tests on the provided applications."
)
parser.add_argument('apps', nargs='+', type=str)
parser.add_argument('--db', nargs='?', type=str, default='sqlite')
args = parser.parse_args()
QuickDjangoTest(*args.apps)
QuickDjangoTest(apps=args.apps, db=args.db )
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing space

3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@
'Environment :: Web Environment',
'Framework :: Django',
'Development Status :: 5 - Production/Stable',
'Programming Language :: Python :: 2.7'],
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.3'],
)