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

Use importlib to handle version and package resources, where available #1462

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
17 changes: 14 additions & 3 deletions MAVProxy/mavproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1360,10 +1360,21 @@ def run_startup_scripts():
#version information
if opts.version:
#pkg_resources doesn't work in the windows exe build, so read the version file
version = None
try:
import pkg_resources
version = pkg_resources.require("mavproxy")[0].version
except:
import importlib.metadata
version = importlib.metadata.version("mavproxy")
except ImportError:
pass

if version is None:
try:
import pkg_resources
version = pkg_resources.require("mavproxy")[0].version
except:
pass

if version is None:
start_script = mp_util.dot_mavproxy("version.txt")
f = open(start_script, 'r')
version = f.readline()
Expand Down
41 changes: 38 additions & 3 deletions MAVProxy/tools/MAVExplorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@
from MAVProxy.modules.lib import wxsettings
from MAVProxy.modules.lib.graphdefinition import GraphDefinition
from lxml import objectify
import pkg_resources
try:
import importlib
import importlib.resources
except ImportError:
pass
from builtins import input
import datetime
import matplotlib
Expand Down Expand Up @@ -353,8 +357,28 @@ def load_graphs():
if graphs:
mestate.graphs.extend(graphs)
mestate.console.writeln("Loaded %s" % file)

# also load the built in graphs
load_built_in_graphs()

mestate.graphs = sorted(mestate.graphs, key=lambda g: g.name)

def load_built_in_graphs():
'''load graph definitions from packaged resources'''
try:
dlist = importlib.resources.files("MAVProxy.tools.graphs")
for f in dlist.iterdir():
raw = importlib.resources.files("MAVProxy.tools.graphs").joinpath(f).open('r').read()
graphs = load_graph_xml(raw, None)
if graphs:
mestate.graphs.extend(graphs)
mestate.console.writeln("Loaded %s" % f)
return
except Exception:
pass

try:
import pkg_resources
dlist = pkg_resources.resource_listdir("MAVProxy", "tools/graphs")
for f in dlist:
raw = pkg_resources.resource_stream("MAVProxy", "tools/graphs/%s" % f).read()
Expand All @@ -371,7 +395,6 @@ def load_graphs():
if graphs:
mestate.graphs.extend(graphs)
mestate.console.writeln("Loaded %s" % f)
mestate.graphs = sorted(mestate.graphs, key=lambda g: g.name)

def flightmode_colours():
'''return mapping of flight mode to colours'''
Expand Down Expand Up @@ -1554,9 +1577,21 @@ def progress_bar(pct):

if args.version:
#pkg_resources doesn't work in the windows exe build, so read the version file
version = None
try:
version = pkg_resources.require("mavproxy")[0].version
import importlib.metadata
version = importlib.metadata.version("mavproxy")
except Exception as e:
pass

if version is None:
try:
import pkg_resources
version = pkg_resources.require("mavproxy")[0].version
except Exception:
pass

if version is None:
start_script = mp_util.dot_mavproxy("version.txt")
f = open(start_script, 'r')
version = f.readline()
Expand Down