Skip to content

Commit

Permalink
Remove Python 2 compatible import hacks
Browse files Browse the repository at this point in the history
  • Loading branch information
M4rtinK committed Apr 12, 2020
1 parent c2be694 commit 9e923e8
Show file tree
Hide file tree
Showing 34 changed files with 30 additions and 166 deletions.
2 changes: 0 additions & 2 deletions core/json_dict.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# -*- coding: utf-8 -*-
# Load and store dictionaries as JSON files
from __future__ import with_statement # Python 2.5

import os
import shutil
from threading import RLock
Expand Down
2 changes: 0 additions & 2 deletions core/modrana_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#---------------------------------------------------------------------------
from __future__ import with_statement # Python 2.5 compatibility

import logging
import logging.handlers
import os
Expand Down
1 change: 0 additions & 1 deletion core/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#---------------------------------------------------------------------------
from __future__ import with_statement # for Python 2.5
import os
from core import utils

Expand Down
10 changes: 2 additions & 8 deletions core/poi_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
from core.point import POI
from core.backports.six import u

PYTHON3 = sys.version_info[0] > 2

import logging
log = logging.getLogger("core.poi_db")

Expand Down Expand Up @@ -103,12 +101,8 @@ def _get_poi_db_order(self, poi):
:returns: POI object values tuple for storage in database order
:rtype: tuple
"""
if PYTHON3:
name = poi.name
description = poi.description
else:
name = poi.name.decode("utf-8")
description = poi.description.decode("utf-8")
name = poi.name
description = poi.description

return (
poi.db_index,
Expand Down
2 changes: 0 additions & 2 deletions core/pool.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# -*- coding: utf-8 -*-
# At the moment just a thread pool implementation
from __future__ import with_statement # Python 2.5

import threading

from core import threads
Expand Down
2 changes: 0 additions & 2 deletions core/qrc.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
# On Android with PyOtherSide modRana might be distributed using the qrc
# mechanism and might need a few tweaks applied to work properly.
#
from __future__ import with_statement # for Python 2.5

import logging
import shutil
log = logging.getLogger("core.qrc")
Expand Down
8 changes: 1 addition & 7 deletions core/routing_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@
from core.way import Way
from core.backports import six

try: # Python 2
from urllib import urlencode
from urllib2 import urlopen, Request, HTTPError, URLError
except ImportError: # Python 3
from urllib.request import urlopen, Request
from urllib.parse import urlencode
from urllib.error import HTTPError, URLError
from urllib.request import urlopen

try:
import json
Expand Down
26 changes: 7 additions & 19 deletions core/signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@

from weakref import WeakValueDictionary

import sys

PYTHON3 = sys.version_info[0] > 2


class Signal(object):
def __init__(self):
self.__slots = WeakValueDictionary()
Expand All @@ -21,22 +16,15 @@ def __call__(self, *args, **kargs):
func(self.__slots[key], *args, **kargs)

def connect(self, slot):
if PYTHON3:
key = (slot.__func__, id(slot.__self__))
self.__slots[key] = slot.__self__
else:
key = (slot.im_func, id(slot.im_self))
self.__slots[key] = slot.im_self
key = (slot.__func__, id(slot.__self__))
self.__slots[key] = slot.__self__


def disconnect(self, slot):
if PYTHON3:
key = (slot.__func__, id(slot.__self__))
if key in self.__slots:
self.__slots.pop(key)
else:
key = (slot.im_func, id(slot.im_self))
if key in self.__slots:
self.__slots.pop(key)
key = (slot.__func__, id(slot.__self__))
if key in self.__slots:
self.__slots.pop(key)


def clear(self):
self.__slots.clear()
Expand Down
1 change: 0 additions & 1 deletion core/threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#log = logging.getLogger("modRana")
# until modRana has a proper logging structure,
# use a fake stdout log
from __future__ import with_statement # for python 2.5
import sys

import threading
Expand Down
1 change: 0 additions & 1 deletion core/tile_storage/files_store.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import with_statement
import os
import glob
import shutil
Expand Down
3 changes: 0 additions & 3 deletions core/tile_storage/sqlite_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@
#
# When looking for a tile in the database, the lookup database is checked first and if the coordinates
# are found the corresponding storage database is queried for the actual data.

from __future__ import with_statement

import os
import sqlite3
import glob
Expand Down
13 changes: 1 addition & 12 deletions core/tile_storage/utils.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
"""Various tile handling utility functions"""
import six
from six import b
import sys
import os
import logging

from .tile_types import ID_TO_CLASS_MAP

log = logging.getLogger("tile_storage.utils")

PYTHON3 = sys.version_info[0] > 2

def is_an_image(tile_data):
"""test if the string contains an image
by reading its magic number"""

if PYTHON3: # in Python 3 we directly get bytes
h = tile_data
else: # in Python <3 we get a string
# create a file-like object
f = six.moves.StringIO(tile_data)
# read the header from it
h = f.read(32)
# cleanup
f.close()
h = tile_data

# NOTE: magic numbers taken from imghdr source code

Expand Down
26 changes: 5 additions & 21 deletions core/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
# modRana - shared utility classes and methods
from __future__ import with_statement # for python 2.5
import threading
import os
import sys
Expand All @@ -15,12 +14,7 @@

StringIO = six.moves.cStringIO

PYTHON3 = sys.version_info[0] > 2

if PYTHON3:
from urllib.parse import urlparse as urllib_parse
else:
from urlparse import urlparse as urllib_parse
from urllib.parse import urlparse as urllib_parse

if qrc.is_qrc:
import pyotherside
Expand Down Expand Up @@ -168,26 +162,16 @@ def is_the_string_an_image(s):
:rtype: bool
"""

if PYTHON3: # in Python 3 we directly get bytes
h = s
else: # in Python <3 we get a string
# create a file-like object
f = StringIO(s)
# read the header from it
h = f.read(32)
# cleanup
f.close()

# NOTE: magic numbers taken from imghdr source code

# as most tiles are PNGs, check for PNG first
if h[:8] == b("\211PNG\r\n\032\n"):
if s[:8] == b("\211PNG\r\n\032\n"):
return True
elif h[6:10] in (b('JFIF'), b('Exif')): # JPEG in JFIF or Exif format
elif s[6:10] in (b('JFIF'), b('Exif')): # JPEG in JFIF or Exif format
return True
elif h[:6] in (b('GIF87a'), b('GIF89a')): # GIF ('87 and '89 variants)
elif s[:6] in (b('GIF87a'), b('GIF89a')): # GIF ('87 and '89 variants)
return True
elif h[:2] in (b('MM'), b('II'), b('BM')): # tiff or BMP
elif s[:2] in (b('MM'), b('II'), b('BM')): # tiff or BMP
return True
else: # probably not an image file
return False
Expand Down
1 change: 0 additions & 1 deletion core/way.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""a modRana class representing an unified tracklog or route"""
# -*- coding: utf-8 -*-
from __future__ import with_statement # for python 2.5
import csv
import os
import threading
Expand Down
14 changes: 1 addition & 13 deletions modules/device_modules/base_device_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@
from modules.base_module import RanaModule
from core.signal import Signal

import sys
import os
import logging
PYTHON3 = sys.version_info[0] > 2

class DeviceModule(RanaModule):
"""A modRana device module"""
Expand Down Expand Up @@ -288,16 +285,7 @@ def connectivity_status(self):
False - disconnected from the Internet
"""
connected = constants.OFFLINE
# open the /proc/net/route file
#with open('/proc/net/route', 'rc') as f:

openMode = "r"
if not PYTHON3:
openMode = "rc"
# TODO: check if this is still needed on Python 2.5
# on the N900

with open('/proc/net/route', openMode) as f:
with open('/proc/net/route', "r") as f:
for line in f:
# the line is delimited by tabulators
lineSplit = line.split('\t')
Expand Down
1 change: 0 additions & 1 deletion modules/gui_modules/gui_qt5/gui_qt5.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#---------------------------------------------------------------------------
from __future__ import with_statement
import os
import re

Expand Down
1 change: 0 additions & 1 deletion modules/mod_cron.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#---------------------------------------------------------------------------
from __future__ import with_statement # for python 2.5
from modules.base_module import RanaModule
import threading

Expand Down
6 changes: 1 addition & 5 deletions modules/mod_loadTracklogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@
import math
import os
import glob

try:
import cPickle as pickle # Python 2
except ImportError:
import pickle # Python 3
import pickle
import shutil
from time import clock
from time import gmtime, strftime
Expand Down
1 change: 0 additions & 1 deletion modules/mod_location/gps_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#---------------------------------------------------------------------------
from __future__ import with_statement # for python 2.5
import threading
import socket
from time import sleep
Expand Down
1 change: 0 additions & 1 deletion modules/mod_location/mod_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#---------------------------------------------------------------------------
from __future__ import with_statement # for python 2.5
from modules.base_module import RanaModule
from time import *
from core import gs
Expand Down
5 changes: 0 additions & 5 deletions modules/mod_mapData/mod_mapData.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,11 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#---------------------------------------------------------------------------
from __future__ import with_statement # for python 2.5
from modules.base_module import RanaModule
from time import clock
import time
import os
import copy
from core import geo
from core import utils
from core import tiles
from core import constants
from core.tilenames import *
import threading
from .pools import BatchSizeCheckPool
Expand Down
1 change: 0 additions & 1 deletion modules/mod_mapData/pools.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
# Tile checking & batch download pools
from __future__ import with_statement

import threading
import time
Expand Down
1 change: 0 additions & 1 deletion modules/mod_mapTiles/mod_mapTiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#---------------------------------------------------------------------------
from __future__ import with_statement # for python 2.5
from modules.base_module import RanaModule
import threading
import time
Expand Down
5 changes: 1 addition & 4 deletions modules/mod_mapTiles/tile_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
# * overwrite support - we want to download tiles for
# some layers even if they are already stored locally
# (traffic, weather overlay, etc.)

from __future__ import with_statement

from urllib.error import HTTPError, URLError
from urllib.error import URLError

import threading
import time
Expand Down
12 changes: 2 additions & 10 deletions modules/mod_onlineServices/geonames.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
"""multi source geocoding"""
import sys
import traceback

try: # Python 2
from urllib import urlencode
from urllib2 import urlopen, Request, build_opener, HTTPError, URLError
except ImportError: # Python 3
from urllib.request import urlopen, Request, build_opener
from urllib.parse import urlencode
from urllib.error import HTTPError, URLError
from urllib.request import urlopen, Request, build_opener
from urllib.parse import urlencode

import logging
log = logging.getLogger("mod.onlineServices.geonames")
Expand Down
11 changes: 2 additions & 9 deletions modules/mod_onlineServices/offline_providers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# -*- coding: utf-8 -*-
# Online geodata providers
import time
import re
from core import constants
from core.point import Point
from core import requirements
Expand All @@ -13,13 +11,8 @@

from core.providers import POIProvider, DummyController

try: # Python 2
from urllib import urlencode
from urllib2 import urlopen, Request, HTTPError, URLError
except ImportError: # Python 3
from urllib.request import urlopen, Request
from urllib.parse import urlencode
from urllib.error import HTTPError, URLError
from urllib.request import urlopen
from urllib.parse import urlencode

OSM_SCOUT_SERVER_POI_SEARCH_URL = "http://localhost:8553/v1/search?"
OSM_SCOUT_SERVER_LOCAL_SEARCH_URL = "http://localhost:8553/v1/guide?"
Expand Down
Loading

0 comments on commit 9e923e8

Please sign in to comment.