Skip to content

Commit 71dcd9e

Browse files
committed
python2/3 compliancy
1 parent 74f5c8a commit 71dcd9e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+393
-280
lines changed

mapnik/__init__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,17 @@ def bootstrap_env():
6161
env = {'ICU_DATA':'/usr/local/share/icu/'}
6262
"""
6363
if os.path.exists(os.path.join(os.path.dirname(__file__),'mapnik_settings.py')):
64-
from mapnik_settings import env
64+
from .mapnik_settings import env
6565
process_keys = os.environ.keys()
6666
for key, value in env.items():
6767
if key not in process_keys:
6868
os.environ[key] = value
6969

7070
bootstrap_env()
7171

72-
from _mapnik import *
72+
from ._mapnik import *
7373

74-
import printing
74+
from . import printing
7575
printing.renderer = render
7676

7777
# The base Boost.Python class
@@ -1048,20 +1048,20 @@ def mapnik_version_from_string(version_string):
10481048
def register_plugins(path=None):
10491049
"""Register plugins located by specified path"""
10501050
if not path:
1051-
if os.environ.has_key('MAPNIK_INPUT_PLUGINS_DIRECTORY'):
1051+
if 'MAPNIK_INPUT_PLUGINS_DIRECTORY' in os.environ:
10521052
path = os.environ.get('MAPNIK_INPUT_PLUGINS_DIRECTORY')
10531053
else:
1054-
from paths import inputpluginspath
1054+
from .paths import inputpluginspath
10551055
path = inputpluginspath
10561056
DatasourceCache.register_datasources(path)
10571057

10581058
def register_fonts(path=None,valid_extensions=['.ttf','.otf','.ttc','.pfa','.pfb','.ttc','.dfont','.woff']):
10591059
"""Recursively register fonts using path argument as base directory"""
10601060
if not path:
1061-
if os.environ.has_key('MAPNIK_FONT_DIRECTORY'):
1061+
if 'MAPNIK_FONT_DIRECTORY' in os.environ:
10621062
path = os.environ.get('MAPNIK_FONT_DIRECTORY')
10631063
else:
1064-
from paths import fontscollectionpath
1064+
from .paths import fontscollectionpath
10651065
path = fontscollectionpath
10661066
for dirpath, _, filenames in os.walk(path):
10671067
for filename in filenames:

mapnik/printing.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
see the documentation of mapnik.printing.PDFPrinter() for options
1616
1717
"""
18-
from __future__ import absolute_import
18+
from __future__ import absolute_import, print_function
1919

2020
from . import render, Map, Box2d, Layer, Feature, Projection, Coord, Style, Geometry
2121
import math
@@ -393,7 +393,7 @@ def add_geospatial_pdf_header(self,m,filename,epsg=None,wkt=None):
393393
o=pyPdf.PdfFileWriter()
394394

395395
# preserve OCProperties at document root if we have one
396-
if i.trailer['/Root'].has_key(pyPdf.generic.NameObject('/OCProperties')):
396+
if pyPdf.generic.NameObject('/OCProperties') in i.trailer['/Root']:
397397
o._root.getObject()[pyPdf.generic.NameObject('/OCProperties')] = i.trailer['/Root'].getObject()[pyPdf.generic.NameObject('/OCProperties')]
398398

399399
for p in i.pages:
@@ -911,7 +911,7 @@ def render_legend(self,m, page_break=False, ctx=None, collumns=1,width=None, hei
911911
else:
912912
rule_text += str(r.filter)
913913
active_rules = tuple(active_rules)
914-
if added_styles.has_key(active_rules):
914+
if active_rules in added_styles:
915915
continue
916916

917917
added_styles[active_rules] = (f,rule_text)
@@ -1014,7 +1014,7 @@ def render_legend(self,m, page_break=False, ctx=None, collumns=1,width=None, hei
10141014
e=self.write_text(ctx, rule_text, m2pt(cwidth-legend_item_box_size[0]-0.005), 6)
10151015
legend_text_size += e[3]
10161016
ctx.rel_move_to(0,e[3])
1017-
if attribution.has_key(layer_title):
1017+
if layer_title in attribution:
10181018
e=self.write_text(ctx, attribution[layer_title], m2pt(cwidth-legend_item_box_size[0]-0.005), 6, fill_color=(0.5,0.5,0.5))
10191019
legend_text_size += e[3]
10201020

setup.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@
88
import shutil
99
import re
1010

11+
PYTHON3 = sys.version_info[0] == 3
12+
13+
14+
# Utils
15+
def check_output(args):
16+
output = subprocess.check_output(args)
17+
if PYTHON3:
18+
# check_output returns bytes in PYTHON3.
19+
output = output.decode()
20+
return output.rstrip('\n')
21+
22+
1123
cflags = sysconfig.get_config_var('CFLAGS')
1224
sysconfig._config_vars['CFLAGS'] = re.sub(' +', ' ', cflags.replace('-g', '').replace('-Os', '').replace('-arch i386', ''))
1325
opt = sysconfig.get_config_var('OPT')
@@ -35,13 +47,13 @@
3547
boost_thread_lib = os.environ.get("BOOST_THREAD_LIB", 'boost_thread-mt')
3648

3749
try:
38-
linkflags = subprocess.check_output([mapnik_config, '--libs']).rstrip('\n').split(' ')
50+
linkflags = check_output([mapnik_config, '--libs']).split(' ')
3951
lib_path = linkflags[0][2:]
40-
linkflags.extend(subprocess.check_output([mapnik_config, '--ldflags']).rstrip('\n').split(' '))
52+
linkflags.extend(check_output([mapnik_config, '--ldflags']).split(' '))
4153
except:
42-
raise Exception("Failed to find proper linking flags from mapnik config");
54+
raise Exception("Failed to find proper linking flags from mapnik config")
4355

44-
## Dynamically make the mapnik/paths.py file if it doesn't exist.
56+
# Dynamically make the mapnik/paths.py file if it doesn't exist.
4557
if os.path.isfile('mapnik/paths.py'):
4658
create_paths = False
4759
else:
@@ -60,7 +72,7 @@
6072
shutil.copyfile(f, os.path.join('mapnik', base_f))
6173
except shutil.Error:
6274
pass
63-
input_plugin_path = subprocess.check_output([mapnik_config, '--input-plugins']).rstrip('\n')
75+
input_plugin_path = check_output([mapnik_config, '--input-plugins'])
6476
input_plugin_files = os.listdir(input_plugin_path)
6577
input_plugin_files = [os.path.join(input_plugin_path, f) for f in input_plugin_files]
6678
if not os.path.exists(os.path.join('mapnik','plugins','input')):
@@ -70,7 +82,7 @@
7082
shutil.copyfile(f, os.path.join('mapnik', 'plugins', 'input', os.path.basename(f)))
7183
except shutil.Error:
7284
pass
73-
font_path = subprocess.check_output([mapnik_config, '--fonts']).rstrip('\n')
85+
font_path = check_output([mapnik_config, '--fonts'])
7486
font_files = os.listdir(font_path)
7587
font_files = [os.path.join(font_path, f) for f in font_files]
7688
if not os.path.exists(os.path.join('mapnik','plugins','fonts')):
@@ -94,7 +106,7 @@
94106

95107

96108
if not mason_build:
97-
icu_path = subprocess.check_output([mapnik_config, '--icu-data']).rstrip('\n')
109+
icu_path = check_output([mapnik_config, '--icu-data'])
98110
else:
99111
icu_path = 'mason_packages/.link/share/icu/'
100112
if icu_path:
@@ -109,7 +121,7 @@
109121
pass
110122

111123
if not mason_build:
112-
gdal_path = subprocess.check_output([mapnik_config, '--gdal-data']).rstrip('\n')
124+
gdal_path = check_output([mapnik_config, '--gdal-data'])
113125
else:
114126
gdal_path = 'mason_packages/.link/share/gdal/'
115127
if os.path.exists('mason_packages/.link/share/gdal/gdal/'):
@@ -126,7 +138,7 @@
126138
pass
127139

128140
if not mason_build:
129-
proj_path = subprocess.check_output([mapnik_config, '--proj-lib']).rstrip('\n')
141+
proj_path = check_output([mapnik_config, '--proj-lib'])
130142
else:
131143
proj_path = 'mason_packages/.link/share/proj/'
132144
if os.path.exists('mason_packages/.link/share/proj/proj/'):
@@ -142,7 +154,7 @@
142154
except shutil.Error:
143155
pass
144156

145-
extra_comp_args = subprocess.check_output([mapnik_config, '--cflags']).rstrip('\n').split(' ')
157+
extra_comp_args = check_output([mapnik_config, '--cflags']).split(' ')
146158

147159
if sys.platform == 'darwin':
148160
extra_comp_args.append('-mmacosx-version-min=10.8')
@@ -153,9 +165,9 @@
153165
linkflags.append('-Wl,-rpath=$ORIGIN')
154166

155167
if os.environ.get("CC",False) == False:
156-
os.environ["CC"] = subprocess.check_output([mapnik_config, '--cxx']).rstrip('\n')
168+
os.environ["CC"] = check_output([mapnik_config, '--cxx'])
157169
if os.environ.get("CXX",False) == False:
158-
os.environ["CXX"] = subprocess.check_output([mapnik_config, '--cxx']).rstrip('\n')
170+
os.environ["CXX"] = check_output([mapnik_config, '--cxx'])
159171

160172
setup(
161173
name = "mapnik",

test/python_tests/agg_rasterizer_integer_overflow_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# -*- coding: utf-8 -*-
33

44
from nose.tools import eq_
5-
from utilities import run_all
5+
from .utilities import run_all
66
import mapnik
77
import json
88

test/python_tests/box2d_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# -*- coding: utf-8 -*-
33

44
from nose.tools import eq_,assert_true,assert_almost_equal,assert_false
5-
from utilities import run_all
5+
from .utilities import run_all
66
import mapnik
77

88
def test_coord_init():

test/python_tests/buffer_clear_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os, mapnik
22
from nose.tools import eq_
3-
from utilities import execution_path, run_all
3+
from .utilities import execution_path, run_all
44

55
def setup():
66
# All of the paths used are relative, if we run the tests

test/python_tests/cairo_test.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#!/usr/bin/env python
22

3+
from __future__ import print_function
4+
35
import os
46
import shutil
57
import mapnik
68
from nose.tools import eq_
7-
from utilities import execution_path, run_all
9+
from .utilities import execution_path, run_all
810

911
def setup():
1012
# All of the paths used are relative, if we run the tests
@@ -90,7 +92,7 @@ def test_passing_pycairo_context_svg():
9092
draw_neatline(m,context)
9193
surface.finish()
9294
if not os.path.exists(expected_cairo_file) or os.environ.get('UPDATE'):
93-
print 'generated expected cairo surface file %s' % expected_cairo_file
95+
print('generated expected cairo surface file', expected_cairo_file)
9496
shutil.copy(test_cairo_file,expected_cairo_file)
9597
diff = abs(os.stat(expected_cairo_file).st_size-os.stat(test_cairo_file).st_size)
9698
msg = 'diff in size (%s) between actual (%s) and expected(%s)' % (diff,test_cairo_file,'tests/python_tests/'+ expected_cairo_file)
@@ -109,7 +111,7 @@ def test_passing_pycairo_context_pdf():
109111
draw_neatline(m,context)
110112
surface.finish()
111113
if not os.path.exists(expected_cairo_file) or os.environ.get('UPDATE'):
112-
print 'generated expected cairo surface file %s' % expected_cairo_file
114+
print('generated expected cairo surface file', expected_cairo_file)
113115
shutil.copy(test_cairo_file,expected_cairo_file)
114116
diff = abs(os.stat(expected_cairo_file).st_size-os.stat(test_cairo_file).st_size)
115117
msg = 'diff in size (%s) between actual (%s) and expected(%s)' % (diff,test_cairo_file,'tests/python_tests/'+ expected_cairo_file)
@@ -133,14 +135,14 @@ def test_passing_pycairo_context_png():
133135
im.save(reduced_color_image,'png8')
134136
surface.finish()
135137
if not os.path.exists(expected_cairo_file) or os.environ.get('UPDATE'):
136-
print 'generated expected cairo surface file %s' % expected_cairo_file
138+
print('generated expected cairo surface file', expected_cairo_file)
137139
shutil.copy(test_cairo_file,expected_cairo_file)
138140
diff = abs(os.stat(expected_cairo_file).st_size-os.stat(test_cairo_file).st_size)
139141
msg = 'diff in size (%s) between actual (%s) and expected(%s)' % (diff,test_cairo_file,'tests/python_tests/'+ expected_cairo_file)
140142
eq_( diff < 500, True, msg)
141143
os.remove(test_cairo_file)
142144
if not os.path.exists(expected_cairo_file2) or os.environ.get('UPDATE'):
143-
print 'generated expected cairo surface file %s' % expected_cairo_file2
145+
print('generated expected cairo surface file', expected_cairo_file2)
144146
shutil.copy(reduced_color_image,expected_cairo_file2)
145147
diff = abs(os.stat(expected_cairo_file2).st_size-os.stat(reduced_color_image).st_size)
146148
msg = 'diff in size (%s) between actual (%s) and expected(%s)' % (diff,reduced_color_image,'tests/python_tests/'+ expected_cairo_file2)
@@ -159,7 +161,7 @@ def _pycairo_surface(type,sym):
159161
mapnik.render(m, surface)
160162
surface.finish()
161163
if not os.path.exists(expected_cairo_file) or os.environ.get('UPDATE'):
162-
print 'generated expected cairo surface file %s' % expected_cairo_file
164+
print('generated expected cairo surface file', expected_cairo_file)
163165
shutil.copy(test_cairo_file,expected_cairo_file)
164166
diff = abs(os.stat(expected_cairo_file).st_size-os.stat(test_cairo_file).st_size)
165167
msg = 'diff in size (%s) between actual (%s) and expected(%s)' % (diff,test_cairo_file,'tests/python_tests/'+ expected_cairo_file)
@@ -170,7 +172,7 @@ def _pycairo_surface(type,sym):
170172
os.remove(test_cairo_file)
171173
return True
172174
else:
173-
print 'skipping cairo.%s test since surface is not available' % type.upper()
175+
print('skipping cairo.%s test since surface is not available' % type.upper())
174176
return True
175177

176178
def test_pycairo_svg_surface1():

test/python_tests/color_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import os, mapnik
66
from timeit import Timer, time
77
from nose.tools import *
8-
from utilities import execution_path, run_all, get_unique_colors
8+
from .utilities import execution_path, run_all, get_unique_colors
99

1010
def setup():
1111
# All of the paths used are relative, if we run the tests

test/python_tests/compare_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import os, mapnik
55
from nose.tools import *
6-
from utilities import execution_path, run_all
6+
from .utilities import execution_path, run_all
77

88
def setup():
99
# All of the paths used are relative, if we run the tests

test/python_tests/compositing_test.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#encoding: utf8
22

3+
from __future__ import print_function
4+
35
from nose.tools import eq_
46
import os
5-
from utilities import execution_path, run_all
6-
from utilities import get_unique_colors, pixel2channels, side_by_side_image
7+
from .utilities import execution_path, run_all
8+
from .utilities import get_unique_colors, pixel2channels, side_by_side_image
79
import mapnik
810

911
def setup():
@@ -19,7 +21,7 @@ def debug_image(image,step=2):
1921
for y in range(0,image.height(),step):
2022
pixel = image.get_pixel(x,y)
2123
red,green,blue,alpha = pixel2channels(pixel)
22-
print "rgba(%s,%s,%s,%s) at %s,%s" % (red,green,blue,alpha,x,y)
24+
print("rgba(%s,%s,%s,%s) at %s,%s" % (red,green,blue,alpha,x,y))
2325

2426
def replace_style(m, name, style):
2527
m.remove_style(name)
@@ -53,7 +55,7 @@ def validate_pixels_are_not_premultiplied2(image):
5355
#each value of the color channels will never be bigger than that of the alpha channel.
5456
if alpha > 0:
5557
if red > 0 and red > alpha:
56-
print 'red: %s, a: %s' % (red,alpha)
58+
print('red: %s, a: %s' % (red,alpha))
5759
looks_not_multiplied = True
5860
return looks_not_multiplied
5961

@@ -94,7 +96,7 @@ def test_compare_images():
9496
fails.append('%s not validly demultiplied' % (name))
9597
a.save(actual,'png32')
9698
if not os.path.exists(expected) or os.environ.get('UPDATE'):
97-
print 'generating expected test image: %s' % expected
99+
print('generating expected test image: %s' % expected)
98100
a.save(expected,'png32')
99101
expected_im = mapnik.Image.open(expected)
100102
# compare them
@@ -168,7 +170,7 @@ def test_style_level_comp_op():
168170
expected = 'images/style-comp-op/' + name + '.png'
169171
im.save(actual,'png32')
170172
if not os.path.exists(expected) or os.environ.get('UPDATE'):
171-
print 'generating expected test image: %s' % expected
173+
print('generating expected test image: %s' % expected)
172174
im.save(expected,'png32')
173175
expected_im = mapnik.Image.open(expected)
174176
# compare them

0 commit comments

Comments
 (0)