Skip to content

Commit

Permalink
Add script to automatically dump babel formats from gpsbabel
Browse files Browse the repository at this point in the history
and update internal list to add a ton of new formats and remove
formats deprecated and removed by gpsbabel
  • Loading branch information
nyalldawson committed Aug 2, 2021
1 parent 9bd5ad4 commit 4b17ef2
Show file tree
Hide file tree
Showing 2 changed files with 250 additions and 26 deletions.
109 changes: 109 additions & 0 deletions scripts/dump_babel_formats.py
@@ -0,0 +1,109 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
***************************************************************************
dump_babel_formats.py
---------------------
Date : July 2021
Copyright : (C) 2021 by Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""

"""
Dumps a list of babel formats for inclusion in QgsBabelFormatRegistry::QgsBabelFormatRegistry()
"""

__author__ = 'Nyall Dawson'
__date__ = 'July 2021'
__copyright__ = '(C) 2021, Nyall Dawson'

import subprocess
import re


def process_lines(lines):
"""
Processes lines from gpsbabel -^3 output
"""
current_line = 0
format_out = {}
while current_line < len(lines):
line = lines[current_line]

fields = line.split('\t')
assert len(fields) >= 5, fields

current_line += 1
html_pages = []
while lines[current_line].startswith('http'):
html_pages.append(lines[current_line])
current_line += 1

while current_line < len(lines) and lines[current_line].startswith('option'):
options = lines[current_line].split('\t')
assert len(options) >= 9, options

name, description, option_type, option_def, option_min, option_max, option_html = options[:7]
# print(name, description, option_type, option_def, option_min, option_max, option_html)

option_http_pages = []
current_line += 1
while current_line < len(lines) and lines[current_line].startswith('http'):
option_http_pages.append(lines[current_line])
current_line += 1

name = fields[2]
description = fields[4]
read_waypoints = fields[1][0] == 'r'
read_tracks = fields[1][2] == 'r'
read_routes = fields[1][4] == 'r'
write_waypoints = fields[1][1] == 'w'
write_tracks = fields[1][3] == 'w'
write_routes = fields[1][5] == 'w'
is_file_format = fields[0] == 'file'
is_device_format = fields[0] == 'serial'
extensions = fields[3].split('/')

if is_file_format and any([read_routes, read_tracks, read_waypoints]):
capabilities = []
if read_waypoints:
capabilities.append('Qgis::BabelFormatCapability::Waypoints')
if read_routes:
capabilities.append('Qgis::BabelFormatCapability::Routes')
if read_tracks:
capabilities.append('Qgis::BabelFormatCapability::Tracks')
format_out[
description] = f"""mImporters[QStringLiteral( "{description}" )] = new QgsBabelSimpleImportFormat( QStringLiteral( "{name}" ), {' | '.join(capabilities)} );"""

for format_name in sorted(format_out.keys(), key=lambda x: x.lower()):
print(format_out[format_name])


CMD = "gpsbabel -^3"
START_FORMATS_RX = re.compile("^file|serial")

result = subprocess.Popen(CMD, shell=True, stdout=subprocess.PIPE)
found_first_line = False
input_lines = []
for input_line in result.stdout.readlines():
input_str = input_line.decode()
if not found_first_line:
if START_FORMATS_RX.match(input_str):
found_first_line = True

else:
continue

if input_str.strip():
input_lines.append(input_str.strip())

process_lines(input_lines)

0 comments on commit 4b17ef2

Please sign in to comment.