Skip to content

Commit

Permalink
Merge pull request #1119 from mlyle/mpl-pythoniterateduavoparsingbull…
Browse files Browse the repository at this point in the history
…shit

python-api: parse xml files iteratively
  • Loading branch information
tracernz authored Jul 15, 2016
2 parents 08b498a + 6757684 commit 9630278
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 27 deletions.
2 changes: 1 addition & 1 deletion python/dronin/uavo.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def make_class(collection, xml_file, update_globals=True):

from lxml import etree

tree = etree.parse(xml_file)
tree = etree.fromstring(xml_file)

subs = {
'tree' : tree,
Expand Down
61 changes: 35 additions & 26 deletions python/dronin/uavo_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,45 @@ def get_settings_objects(self):

return objs

def from_file_contents(self, content_list):
some_processed = True

# There are dependencies here
while some_processed:
some_processed = False

unprocessed = []

# Build up the UAV objects from the xml definitions
for contents in content_list:
try:
u = uavo.make_class(self, contents)

# add this uavo definition to our dictionary
self.update([('{0:08x}'.format(u._id), u)])

some_processed = True
except Exception:
unprocessed.append(contents)

content_list = unprocessed

if len(content_list):
raise Exception("Unable to parse some uavo files")

def from_tar_file(self, t):
# Get the file members
f_members = []
content_list = []

for f_info in t.getmembers():
if not f_info.isfile():
continue
# Make sure the shared definitions are parsed first
if op.basename(f_info.name).lower().find('shared') > 0:
f_members.insert(0, f_info)
else:
f_members.append(f_info)

# Build up the UAV objects from the xml definitions
for f_info in f_members:
if f_info.name.count("oplinksettings") > 0:
continue

f = t.extractfile(f_info)
content_list.append(f.read())
f.close()

u = uavo.make_class(self, f)

# add this uavo definition to our dictionary
self.update([('{0:08x}'.format(u._id), u)])
self.from_file_contents(content_list)

def from_tar_bytes(self, contents):
from cStringIO import StringIO
Expand Down Expand Up @@ -103,17 +119,10 @@ def from_uavo_xml_path(self, path):
import os
import glob

# Make sure the shared definitions are parsed first
file_names = []
for file_name in glob.glob(os.path.join(path, '*.xml')):
if op.basename(file_name).lower().find('shared') > 0:
file_names.insert(0, file_name)
else:
file_names.append(file_name)
content_list = []

for file_name in file_names:
for file_name in glob.glob(os.path.join(path, '*.xml')):
with open(file_name, 'rU') as f:
u = uavo.make_class(self, f)
content_list.append(f.read())

# add this uavo definition to our dictionary
self.update([('{0:08x}'.format(u._id), u)])
self.from_file_contents(content_list)

0 comments on commit 9630278

Please sign in to comment.