Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
32 changes: 9 additions & 23 deletions MAVProxy/mavproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,7 @@
import threading
import time
import traceback

try:
reload
except NameError:
try:
from importlib import reload
except ImportError:
from imp import reload
from importlib import reload

from pymavlink import mavutil

Expand Down Expand Up @@ -592,13 +585,11 @@ def generate_kwargs(args):


def get_exception_stacktrace(e):
if sys.version_info[0] >= 3:
ret = "%s\n" % e
ret += ''.join(traceback.format_exception(type(e),
e,
tb=e.__traceback__))
return ret
return traceback.format_exc(e)
ret = "%s\n" % e
ret += ''.join(traceback.format_exception(type(e),
e,
tb=e.__traceback__))
return ret


def cmd_module(args):
Expand Down Expand Up @@ -767,10 +758,7 @@ def process_stdin(line):
line += '\r'
for c in line:
time.sleep(0.01)
if sys.version_info.major >= 3:
mpstate.master().write(bytes(c, "ascii"))
else:
mpstate.master().write(c)
mpstate.master().write(bytes(c, "ascii"))
return

if not line:
Expand Down Expand Up @@ -854,10 +842,8 @@ def process_master(m):
if mpstate.system == 'Windows':
# strip nsh ansi codes
s = s.replace("\033[K", "")
if sys.version_info.major >= 3:
sys.stdout.write(str(s, "ascii", "ignore"))
else:
sys.stdout.write(str(s))

sys.stdout.write(str(s, "ascii", "ignore"))
sys.stdout.flush()
return

Expand Down
7 changes: 3 additions & 4 deletions MAVProxy/modules/lib/MacOS/backend_wx.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@
import traceback, pdb
_DEBUG_lvls = {1 : 'Low ', 2 : 'Med ', 3 : 'High', 4 : 'Error' }

if sys.version_info[0] >= 3:
warnings.warn(
"The wx and wxagg backends have not been tested with Python 3.x",
ImportWarning)
warnings.warn(
"The wx and wxagg backends have not been tested with Python 3.x",
ImportWarning)

missingwx = "Matplotlib backend_wx and backend_wxagg require wxPython >=2.8"
missingwxversion = ("Matplotlib backend_wx and backend_wxagg "
Expand Down
5 changes: 1 addition & 4 deletions MAVProxy/modules/lib/grapher.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,7 @@ def __init__(self, flightmode_colourmap=None):
self.title = None
self.grid = False
self.xlim_t = None
if sys.version_info[0] >= 3:
self.text_types = frozenset([str,])
else:
self.text_types = frozenset([unicode, str])
self.text_types = frozenset([str,])
self.max_message_rate = 0

def set_max_message_rate(self, rate_hz):
Expand Down
7 changes: 1 addition & 6 deletions MAVProxy/modules/lib/mission_item_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@
from MAVProxy.modules.lib.mp_menu import MPMenuItem
from MAVProxy.modules.lib.mp_menu import MPMenuSubMenu

try:
# py2
from StringIO import StringIO as SIO
except ImportError:
# py3
from io import BytesIO as SIO
from io import BytesIO as SIO


class MissionItemProtocolModule(mp_module.MPModule):
Expand Down
10 changes: 2 additions & 8 deletions MAVProxy/modules/lib/mp_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,8 @@ def dot_mavproxy(name=None):

def download_url(url):
'''download a URL and return the content'''
if sys.version_info.major < 3:
from urllib2 import urlopen as url_open
from urllib2 import URLError as url_error
else:
from urllib.request import urlopen as url_open
from urllib.error import URLError as url_error
from urllib.request import urlopen as url_open
from urllib.error import URLError as url_error
try:
resp = url_open(url)
headers = resp.info()
Expand Down Expand Up @@ -365,8 +361,6 @@ def quaternion_to_axis_angle(q):

def null_term(str):
'''null terminate a string for py3'''
if sys.version_info.major < 3:
return str
if isinstance(str, bytes):
str = str.decode("utf-8")
idx = str.find("\0")
Expand Down
16 changes: 6 additions & 10 deletions MAVProxy/modules/lib/ntrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ def __init__(self,
ssl=False,
V2=False,
):
if sys.version_info.major >= 3:
user = bytearray(user, 'ascii')
user = bytearray(user, 'ascii')
self.user = base64.b64encode(user)
self.port = port
self.caster = caster
Expand Down Expand Up @@ -75,9 +74,7 @@ def setPosition(self, lat, lon):
self.lat = lat

def getMountPointString(self):
userstr = self.user
if sys.version_info.major >= 3:
userstr = str(userstr, 'ascii')
userstr = str(self.user, 'ascii')
mountPointString = "GET %s HTTP/1.0\r\nUser-Agent: %s\r\nAuthorization: Basic %s\r\n" % (
self.mountpoint, useragent, userstr)
if self.host or self.V2:
Expand Down Expand Up @@ -131,8 +128,7 @@ def read(self):
self.sent_header = True
time.sleep(0.1)
mps = self.getMountPointString()
if sys.version_info.major >= 3:
mps = bytearray(mps, 'ascii')
mps = bytearray(mps, 'ascii')
try:
self.socket.sendall(mps)
except ssl.SSLWantReadError:
Expand All @@ -150,9 +146,9 @@ def read(self):
return None
self.socket = None
casterResponse = ''
if sys.version_info.major >= 3:
# Ignore non ascii characters in HTTP response
casterResponse = str(casterResponse, 'ascii', 'ignore')

# Ignore non ascii characters in HTTP response
casterResponse = str(casterResponse, 'ascii', 'ignore')
header_lines = casterResponse.split("\r\n")
is_ntrip_rev1 = False
for line in header_lines:
Expand Down
9 changes: 2 additions & 7 deletions MAVProxy/modules/lib/param_ftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,8 @@ def ftp_param_decode(data):
}

count = 0

if sys.version_info.major < 3:
pad_byte = chr(0)
last_name = ''
else:
pad_byte = 0
last_name = bytes()
pad_byte = 0
last_name = bytes()

while True:
while len(data) > 0 and data[0] == pad_byte:
Expand Down
16 changes: 8 additions & 8 deletions MAVProxy/modules/lib/rline.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# with a try/except
if platform.system() == 'Darwin':
import gnureadline as readline
elif platform.system() == 'Windows' and sys.version_info >= (3, 0):
elif platform.system() == 'Windows':
# Python 3.8 defaults to Proactor event loop, which doesn't work well
if sys.version_info >= (3, 8):
import asyncio
Expand Down Expand Up @@ -49,7 +49,7 @@ def __init__(self, prompt, mpstate):
'(LOADEDMODULES)' : complete_loadedmodules
}

if platform.system() == 'Windows' and sys.version_info >= (3, 0):
if platform.system() == 'Windows':
# Create key bindings registry with a custom binding for the Tab key that
# displays completions like GNU readline.
self.session = PromptSession()
Expand All @@ -58,13 +58,13 @@ def __init__(self, prompt, mpstate):
def set_prompt(self, prompt):
if prompt != self.prompt:
self.prompt = prompt
if platform.system() != 'Windows' or sys.version_info < (3, 0):
if platform.system() != 'Windows' or sys.version_info < (3, 0): #??

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Finish before merge

sys.stdout.write(prompt)
self.redisplay()

def add_history(self, line):
'''add a line to the history'''
if platform.system() == 'Windows' and sys.version_info >= (3, 0):
if platform.system() == 'Windows':
self.session.history.append_string(line)
else:
readline.add_history(line)
Expand All @@ -84,14 +84,14 @@ def get_prompt(self):
def input(self):
''' get user input'''
ret = ""
if platform.system() == 'Windows' and sys.version_info >= (3, 0):
if platform.system() == 'Windows':
global rline_mpstate
with patch_stdout():
return self.session.prompt(self.get_prompt,completer=rline_mpstate.completor, complete_while_typing=False, complete_style=CompleteStyle.READLINE_LIKE, refresh_interval=0.5)
else:
return input(self.get_prompt())

if platform.system() == 'Windows' and sys.version_info >= (3, 0):
if platform.system() == 'Windows':
# This is the prompt-toolkit setup for Windows
class MAVPromptCompleter(Completer):
'''Completion generator for commands'''
Expand Down Expand Up @@ -273,7 +273,7 @@ def complete_rule(rule, cmd):

# expand the next rule component
expanded = []
if platform.system() == 'Windows' and sys.version_info >= (3, 0):
if platform.system() == 'Windows':
if len(rule_components) >= len(cmd):
expanded = rule_expand(rule_components[len(cmd)-1], cmd[-1])
else:
Expand Down Expand Up @@ -327,7 +327,7 @@ def complete(text, state):
return last_clist[state]

# Configure readline for linux/mac systems
if platform.system() != 'Windows' or sys.version_info < (3, 0):
if platform.system() != 'Windows':
readline.set_completer_delims(' \t\n;')
readline.parse_and_bind("tab: complete")
readline.set_completer(complete)
Expand Down
21 changes: 7 additions & 14 deletions MAVProxy/modules/lib/srtm.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@
Edited by CanberraUAV"""

import sys
if sys.version_info.major < 3:
from HTMLParser import HTMLParser
import httplib
else:
from html.parser import HTMLParser
import http.client as httplib
from html.parser import HTMLParser
import http.client as httplib

import re
import pickle
Expand Down Expand Up @@ -160,16 +156,13 @@ def getURIWithRedirect(self, url):
continue
data = r1.read()
conn.close()
if sys.version_info.major < 3:
encoding = r1.headers.get_content_charset()
if encoding is not None:
return data.decode(encoding)
elif ".zip" in url or ".hgt" in url:
return data
else:
encoding = r1.headers.get_content_charset()
if encoding is not None:
return data.decode(encoding)
elif ".zip" in url or ".hgt" in url:
return data
else:
return data.decode('utf-8')
return data.decode('utf-8')
return None

def createFileListHTTP(self):
Expand Down
7 changes: 2 additions & 5 deletions MAVProxy/modules/lib/wavefront.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,9 @@
from __future__ import print_function

import os
try:
import cPickle as pickle
except ImportError:
import pickle
import threading
import pickle
import sys
import threading

class Parser(object):
def __init__(self, filename=None, string='', enable_cache=False):
Expand Down
6 changes: 1 addition & 5 deletions MAVProxy/modules/lib/wxsettings_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,7 @@ def on_load(self, event):
ctrl = self.controls[label]
value = ctrl.GetValue()

# Python 2 compatiblility - alternative is:
# import six
# isinstance(value, six.string_types)
if isinstance(value, str) \
or isinstance(value, str if sys.version_info[0] >= 3 else unicode):
if isinstance(value, str):
ctrl.SetValue(str(setting.value))
else:
ctrl.SetValue(setting.value)
Expand Down
5 changes: 1 addition & 4 deletions MAVProxy/modules/mavproxy_dataflash_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,7 @@ def new_log_filepath(self):
log_cnt = 1

self.lastlog_file = open(ll_filepath, 'w+b')
if sys.version_info[0] >= 3:
self.lastlog_file.write(str.encode(log_cnt.__str__()))
else:
self.lastlog_file.write(log_cnt.__str__())
self.lastlog_file.write(str.encode(log_cnt.__str__()))
self.lastlog_file.close()

return os.path.join(self.dataflash_dir, '%u.BIN' % (log_cnt,))
Expand Down
6 changes: 2 additions & 4 deletions MAVProxy/modules/mavproxy_devop.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ def devop_read(self, args, bustype):
address = int(args[2],base=0)
reg = int(args[3],base=0)
count = int(args[4],base=0)
if sys.version_info.major >= 3:
name = bytearray(name, 'ascii')
name = bytearray(name, 'ascii')
self.master.mav.device_op_read_send(self.target_system,
self.target_component,
self.request_id,
Expand Down Expand Up @@ -82,8 +81,7 @@ def devop_write(self, args, bustype):
bytes = [0]*128
for i in range(count):
bytes[i] = int(args[i],base=0)
if sys.version_info.major >= 3:
name = bytearray(name, 'ascii')
name = bytearray(name, 'ascii')
self.master.mav.device_op_write_send(self.target_system,
self.target_component,
self.request_id,
Expand Down
18 changes: 4 additions & 14 deletions MAVProxy/modules/mavproxy_ftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@
import random
from pymavlink import mavutil

try:
# py2
from StringIO import StringIO as SIO
except ImportError:
# py3
from io import BytesIO as SIO

from io import BytesIO as SIO

from MAVProxy.modules.lib import mp_module
from MAVProxy.modules.lib import mp_settings
Expand Down Expand Up @@ -247,10 +243,7 @@ def handle_list_reply(self, op, m):
continue
self.dir_offset += 1
try:
if sys.version_info.major >= 3:
d = str(d, 'ascii')
else:
d = str(d)
d = str(d, 'ascii')
except Exception:
continue
if d[0] == 'D':
Expand Down Expand Up @@ -335,10 +328,7 @@ def check_read_finished(self):
self.callback = None
elif self.filename == "-":
self.fh.seek(0)
if sys.version_info.major < 3:
print(self.fh.read())
else:
print(self.fh.read().decode('utf-8'))
print(self.fh.read())
else:
print("Wrote %u bytes to %s in %.2fs %.1fkByte/s" % (ofs, self.filename, dt, rate))
self.terminate_session()
Expand Down
Loading