Skip to content

Commit

Permalink
Changed decoding to UTF-8, removed logging code, removed test files
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Paynter authored and Frank Paynter committed Jan 2, 2025
1 parent 0e1083a commit 2abe1a4
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 108 deletions.
18 changes: 3 additions & 15 deletions lib/xcsoar/mapgen/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ def index(self, **params):
return view.render()

name = params["name"].strip()
# cherrypy.log('params: name = %s, mail = %s, detail = %s' % (name, params["mail"], params["level_of_detail"]))


if name == "":
return view.render(error="No map name given!") | HTMLFormFiller(data=params)

Expand All @@ -82,15 +81,6 @@ def index(self, **params):

selection = params["selection"]
waypoint_file = params["waypoint_file"]
# cherrypy.log('waypoint_file = %s, waypoint_filename = %s, selection = %s' % (waypoint_file.file, waypoint_file.filename, selection))
# cherrypy.log('waypoint_file = %s' % waypoint_file.file)

#gfp added to determine 'waypoint_file' type
# cherrypy.log('displaying lines from waypoint_file.file')
# lines = waypoint_file.file.readlines()
# for line in lines:
# cherrypy.log(line)


if selection in ["waypoint", "waypoint_bounds"]:
if not waypoint_file.file or not waypoint_file.filename:
Expand All @@ -110,7 +100,7 @@ def index(self, **params):
)
)

# #241212 better way to write this boolean expression (filename already forced to lowercase)
#241212 better way to write this boolean expression (filename already forced to lowercase)
if not filename.endswith(".dat") and not filename.endswith(".cup"):
raise RuntimeError(
"Waypoint file {} has an unsupported format.".format(
Expand All @@ -125,7 +115,6 @@ def index(self, **params):
"waypoints.cup" if filename.endswith(".cup") else "waypoints.dat"
)

cherrypy.log(f'in server.py: {filename} bounds: left = {desc.bounds.left:.3f}, right: {desc.bounds.right:.3f}, top: {desc.bounds.top:.3f}, bot {desc.bounds.bottom:.3f}')
return view.render(error=f"left: {desc.bounds.left:.3f}, right: {desc.bounds.right:.3f}, top: {desc.bounds.top:.3f}, bot {desc.bounds.bottom:.3f}")| HTMLFormFiller(data=params)

except:
Expand Down Expand Up @@ -165,8 +154,7 @@ def index(self, **params):

if desc.waypoint_file:
waypoint_file.file.seek(0)
cherrypy.log("In the 'desc.waypoint_file' routine")


f = open(job.file_path(desc.waypoint_file), "w")
try:
shutil.copyfileobj(fsrc=waypoint_file.file, fdst=f, length=1024 * 64)
Expand Down
1 change: 0 additions & 1 deletion lib/xcsoar/mapgen/server/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def wrapper(*args, **kwargs):


def render(*args, **kwargs):
# cherrypy.log(f'In render(): args = {args}, kwargs = {kwargs}')
"""Function to render the given data to the template specified via the
``@output`` decorator.
"""
Expand Down
21 changes: 1 addition & 20 deletions lib/xcsoar/mapgen/waypoints/parser.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,13 @@
# -*- coding: utf-8 -*-
from xcsoar.mapgen.waypoints.seeyou_reader import parse_seeyou_waypoints
from xcsoar.mapgen.waypoints.winpilot_reader import parse_winpilot_waypoints
import cherrypy

#cherrypy.log(data.decode('utf-8'))

def parse_waypoint_file(filename, file=None):
# cherrypy.log('in parse_waypoint_file: filename = %s' % filename)
lines = 0 #gfp added so 'lines' object stays in scope
if not file:
cherrypy.log('in parse_waypoint_file: if not file block with filename = %s' % filename)

else:
# # #241207 gfp bugfix: parser fcns need 'lines' (list of lines) vs 'file'
lines = file.readlines()

# cherrypy.log('in parse_waypoint_file: %s lines read from %s' %(lines.count, filename))
cherrypy.log('in parse_waypoint_file: %s lines read from %s' %(len(lines), filename))
# wpnum = 0
# for line in lines:
# wpnum+=1
# decoded_line = line.decode('ISO-8859-2')
# cherrypy.log('line%s: %s' % (wpnum, decoded_line))
lines = file.readlines()

if filename.lower().endswith(".xcw") or filename.lower().endswith(".dat"):
return parse_winpilot_waypoints(lines)
elif filename.lower().endswith(".cup"):
# cherrypy.log('in parse_waypoint_file filename.lower().endswith(".cup"): filename = %s' % filename)
return parse_seeyou_waypoints(lines)#241207 gfp bugfix:
else:
raise RuntimeError(
Expand Down
42 changes: 3 additions & 39 deletions lib/xcsoar/mapgen/waypoints/seeyou_reader.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
from xcsoar.mapgen.waypoints.waypoint import Waypoint
from xcsoar.mapgen.waypoints.list import WaypointList
import cherrypy



Expand Down Expand Up @@ -37,7 +36,6 @@ def __next__(self):


def __parse_altitude(str):
# cherrypy.log(f'in parse_altitude({str})')
str = str.lower()
if str.endswith("ft") or str.endswith("f"):
str = str.rstrip("ft")
Expand Down Expand Up @@ -78,57 +76,30 @@ def __parse_length(str):

def parse_seeyou_waypoints(lines, bounds=None):
waypoint_list = WaypointList()
# cherrypy.log('in parse_seeyou_waypoints function:')

#gfp 241210: modified to wait for header line before processing
#gfp 241210: added 'ISO-8859-2' decoding for correct cherrypy logging display

#gfp 241208 added to print out all lines in selected .CUP file
# wpnum = 0
# for byteline in lines:
# wpnum = wpnum + 1
# line = byteline.decode('ISO-8859-2')

header = 'name,code,country,lat,lon,elev,style,rwdir,rwlen,freq,desc'

wpnum = 0
for byteline in lines:
wpnum = wpnum + 1
line = byteline.decode('ISO-8859-2') #gfp 241210: added 'ISO-8859-2' decoding for correct cherrypy logging display
line = line.strip()

# cherrypy.log('in for loop: wpnum = %s line = %s' %(wpnum, line))
# cherrypy.log(f'for loop row {wpnum}: {line}')
line = byteline.decode('UTF-8')

#check for blank lines or comments
if line == "" or line.startswith("*"):
continue

if header in line:
# cherrypy.log(f'header line found at row {wpnum}: {line}')
continue #skip to next line (first waypoint line)

if line == "-----Related Tasks-----":
break

# cherrypy.log('in for loop before line = __CSVLine(line): wpnum = %s' %wpnum)

fields = []
CSVline = __CSVLine(line)
# cherrypy.log(f'row {wpnum}: line = __CSVLine(line) ->> {line}')


while CSVline.has_next():
fields.append(next(CSVline))


#display fields for this line
# cherrypy.log('extracted fields for line = %s' %wpnum)
# idx = 0
# for field in fields:
# cherrypy.log(f' field[{idx}] = {field}')
# idx += 1


if len(fields) < 6:
continue

Expand All @@ -146,10 +117,7 @@ def parse_seeyou_waypoints(lines, bounds=None):
wp.altitude = __parse_altitude(fields[5])
wp.name = fields[0].strip()
wp.country_code = fields[2].strip()

# cherrypy.log('waypoint %s: name = %s' %(wpnum, wp.name))



if len(fields) > 6 and len(fields[6]) > 0:
wp.cup_type = int(fields[6])

Expand All @@ -165,11 +133,7 @@ def parse_seeyou_waypoints(lines, bounds=None):
if len(fields) > 10 and len(fields[10]) > 0:
wp.comment = fields[10].strip()

# cherrypy.log(f'waypoint {wpnum}: {wp.name}, {wp.lat:.3f}, {wp.lon:.3f}')

#gfp print out current 'bounds' params
# cherrypy.log(f'bounds = {bounds}')

waypoint_list.append(wp)


Expand Down
35 changes: 2 additions & 33 deletions lib/xcsoar/mapgen/waypoints/winpilot_reader.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
# -*- coding: utf-8 -*-
from xcsoar.mapgen.waypoints.waypoint import Waypoint
from xcsoar.mapgen.waypoints.list import WaypointList
import cherrypy

def __parse_altitude(str):
# cherrypy.log(f'parse_altitude({str})')
str = str.lower()
if str.endswith("ft") or str.endswith("f"):
str = str.rstrip("ft")
return int(str) * 0.3048
else:
str = str.rstrip("m")
# cherrypy.log(f'parse_altitude:str = {str})')
float_alt = float(str)
# cherrypy.log(f'parse_altitude:float_alt = {float_alt})')
int_alt = int(float_alt)
# cherrypy.log(f'parse_altitude:int_alt = {int_alt})')

return int(int_alt)

Expand All @@ -27,77 +22,51 @@ def __parse_altitude(str):
# dd:mm.dd (for example: 36:15.33N)
# dd:mm.ddd (for example: 36:15.333N)
def __parse_coordinate(str):
# cherrypy.log(f'winpilot parse_coordinate({str})')

str = str.lower()
negative = str.endswith("s") or str.endswith("w")
str = str.rstrip("sw") if negative else str.rstrip("ne")

# cherrypy.log(f'parse_coordinate before str.split: str = {str}')

strsplit = str.split(":")
# cherrypy.log(f'parse_coordinate after str.split into {len(strsplit)} elements')
if len(strsplit) < 2:
return None

if len(strsplit) == 2:
# cherrypy.log(f'parse_coordinate in 2 element block')
# degrees + minutes / 60
a = int(strsplit[0]) + float(strsplit[1]) / 60
# cherrypy.log(f'parse_coordinate in 2 element block: a = {a}')

if len(strsplit) == 3:
# cherrypy.log(f'parse_coordinate in 3 element block')
# degrees + minutes / 60 + seconds / 3600
a = int(str[0]) + float(str[1]) / 60 + float(str[2]) / 3600
# cherrypy.log(f'parse_coordinate in 3 element block: a = {a}')

if negative:
a *= -1

# cherrypy.log(f'parse_coordinate just before return with a = {a}')

return a


def parse_winpilot_waypoints(lines):
# cherrypy.log('in parse_winpilot_waypoints function:')

waypoint_list = WaypointList()
wpnum = 0
for byteline in lines:
wpnum += 1
# cherrypy.log(f'winpilot line {wpnum}: {byteline}')

line = byteline.decode('ISO-8859-2') #gfp 241210: added 'ISO-8859-2' decoding for correct cherrypy logging display
line = byteline.decode('UTF-8')
line = line.strip()
if line == "" or line.startswith("*"):
continue
# cherrypy.log(f'winpilot line {wpnum}: {line}')


fields = line.split(",")
# cherrypy.log(f'winpilot line {wpnum}: fields = {fields}')
# cherrypy.log(f'winpilot line {wpnum}: line splits into {len(fields)} fields')
if len(fields) < 6:
continue



# fieldnum = 0
# for field in fields:
# cherrypy.log(f'field {fieldnum} = {field}')
# fieldnum += 1

wp = Waypoint()
wp.lat = __parse_coordinate(fields[1])
wp.lon = __parse_coordinate(fields[2])
wp.altitude = __parse_altitude(fields[3])
wp.name = fields[5].strip()

# cherrypy.log(f'waypoint {wpnum}: {wp.name}, {wp.lat:.3f}, {wp.lon:.3f}')

waypoint_list.append(wp)



return waypoint_list

0 comments on commit 2abe1a4

Please sign in to comment.