Skip to content

Commit 6ab20f7

Browse files
committed
Merge pull request #17 from mrpollo/api_load_path_context
Api load path context
2 parents cb06ea2 + e92aa89 commit 6ab20f7

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

droneapi/lib/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import threading
33
from pymavlink import mavutil
44

5+
local_path = ''
6+
57
def web_connect(authinfo):
68
"""
79
Connect to the central dronehub server

droneapi/module/api.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import time
23
import threading
34
import traceback
@@ -342,6 +343,8 @@ def __init__(self, mpstate):
342343
self.web = None
343344
self.web_interface = 0
344345
self.web_serverid = None
346+
347+
self.local_path = os.path.dirname(os.getcwd())
345348
print("DroneAPI loaded")
346349

347350
def __on_change(self, *args):
@@ -434,7 +437,8 @@ def send_to_server(self, m):
434437
pass
435438

436439
def thread_remove(self, t):
437-
del self.threads[t.thread_num]
440+
if t.thread_num in self.threads.keys():
441+
del self.threads[t.thread_num]
438442

439443
def thread_add(self, t):
440444
self.threads[t.thread_num] = t
@@ -445,7 +449,8 @@ def cmd_list(self):
445449
print str(t)
446450

447451
def cmd_kill(self, n):
448-
self.threads[n].kill()
452+
if self.threads[n].isAlive():
453+
self.threads[n].kill()
449454

450455
def get_connection(self):
451456
return self.api
@@ -529,10 +534,22 @@ def cmd_api(self, args):
529534
# Just kill the youngest
530535
self.cmd_kill(max(self.threads.keys))
531536
elif args[0] == "start":
532-
if len(args) != 2:
533-
print("usage: api load <filename>")
537+
if len(args) < 2:
538+
print("usage: api start <filename> <local_path:optional>")
534539
return
535-
g = { "local_connect" : self.get_connection }
540+
541+
g = {
542+
"local_connect" : self.get_connection,
543+
"local_path": self.local_path
544+
}
545+
546+
# define local_path in target file
547+
# this helps to add context to apps
548+
# that need to load external resources
549+
# w/o relative paths
550+
if len(args) == 3:
551+
g["local_path"] = args[2]
552+
536553
APIThread(self, lambda: execfile(args[1], g), args[1])
537554
else:
538555
print("Invalid api subcommand")

0 commit comments

Comments
 (0)