# Path: prosoar/waypoints/welt2000_reader.py
# def parse_welt2000_waypoints(lines, bounds=None):
#     waypoint_list = WaypointList()
# 
#     for line in lines:
#         wp = __parse_line(line, bounds)
#         if wp:
#             waypoint_list.append(wp)
# 
#     return waypoint_list
# 
# Path: prosoar/waypoints/seeyou_writer.py
# def write_seeyou_waypoints(waypoints, path):
#     if not isinstance(waypoints, WaypointList):
#         raise TypeError("WaypointList expected")
# 
#     with open(path, "w") as f:
#         f.write(
#             'name,code,country,lat,lon,elev,style,rwdir,rwlen,freq,desc\r\n')
#         for waypoint in waypoints:
#             f.write(__compose_line(waypoint) + '\r\n')
# 
#     return path
# 
# Path: prosoar/filelist.py
# class FileList:
#     def __init__(self):
#         self.__list = []
# 
#     def __iter__(self):
#         return iter(self.__list)
# 
#     def clear(self):
#         self.__list = []
# 
#     def extend(self, list):
#         if not isinstance(list, FileList):
#             raise TypeError
#         self.__list.extend(list)
# 
#     def add(self, file, compress):
#         self.__list.append((file, compress))


import os
import subprocess
from prosoar.waypoints.welt2000_reader import parse_welt2000_waypoints
from prosoar.waypoints.seeyou_writer import write_seeyou_waypoints
from prosoar.filelist import FileList
def __get_database_file(dir_data):
    path = os.path.join(dir_data, 'welt2000', 'WELT2000.TXT')

    # Create Welt2000 data folder if necessary
    if not os.path.exists(os.path.dirname(path)):
        os.makedirs(os.path.dirname(path))

    # Download the current file
    # (only if server file is newer than local file)
    url = 'http://www.segelflug.de/vereine/welt2000/download/WELT2000.TXT'
    subprocess.check_call(['wget', '-N', '-P', os.path.dirname(path), url])

    # Check if download succeeded
    if not os.path.exists(path):
        raise RuntimeError('Welt2000 database not found at {}'.format(path))

    # Return path to the Welt2000 file
    return path


def get_database(dir_data, bounds=None):
    # Get Welt2000 file
    path = __get_database_file(dir_data)

    # Parse Welt2000 file
    with open(path, "r") as f:
        # Return parsed WaypointList
        return parse_welt2000_waypoints(f, bounds)



# Path: prosoar/waypoints/welt2000_reader.py
# def parse_welt2000_waypoints(lines, bounds=None):
#     waypoint_list = WaypointList()
# 
#     for line in lines:
#         wp = __parse_line(line, bounds)
#         if wp:
#             waypoint_list.append(wp)
# 
#     return waypoint_list
# 
# Path: prosoar/waypoints/seeyou_writer.py
# def write_seeyou_waypoints(waypoints, path):
#     if not isinstance(waypoints, WaypointList):
#         raise TypeError("WaypointList expected")
# 
#     with open(path, "w") as f:
#         f.write(
#             'name,code,country,lat,lon,elev,style,rwdir,rwlen,freq,desc\r\n')
#         for waypoint in waypoints:
#             f.write(__compose_line(waypoint) + '\r\n')
# 
#     return path
# 
# Path: prosoar/filelist.py
# class FileList:
#     def __init__(self):
#         self.__list = []
# 
#     def __iter__(self):
#         return iter(self.__list)
# 
#     def clear(self):
#         self.__list = []
# 
#     def extend(self, list):
#         if not isinstance(list, FileList):
#             raise TypeError
#         self.__list.extend(list)
# 
#     def add(self, file, compress):
#         self.__list.append((file, compress))


# Path: prosoar/waypoints/welt2000.py
import os
import subprocess
from prosoar.waypoints.welt2000_reader import parse_welt2000_waypoints
from prosoar.waypoints.seeyou_writer import write_seeyou_waypoints
from prosoar.filelist import FileList

def __get_database_file(dir_data):
    path = os.path.join(dir_data, 'welt2000', 'WELT2000.TXT')

    # Create Welt2000 data folder if necessary
    if not os.path.exists(os.path.dirname(path)):
        os.makedirs(os.path.dirname(path))

    # Download the current file
    # (only if server file is newer than local file)
    url = 'http://www.segelflug.de/vereine/welt2000/download/WELT2000.TXT'
    subprocess.check_call(['wget', '-N', '-P', os.path.dirname(path), url])

    # Check if download succeeded
    if not os.path.exists(path):
        raise RuntimeError('Welt2000 database not found at {}'.format(path))

    # Return path to the Welt2000 file
    return path


def get_database(dir_data, bounds=None):
    # Get Welt2000 file
    path = __get_database_file(dir_data)

    # Parse Welt2000 file
    with open(path, "r") as f:
        # Return parsed WaypointList
        return parse_welt2000_waypoints(f, bounds)



def __create_waypoint_file(database, dir_temp):