import os
import subprocess
from converters.converter_settings import OSMAXX_CONVERSION_SERVICE


class BBox:
    """
    A pickleable Bounding Box object

    :param west: float, indicating a position in mercator
    :param south: float, indicating a position in mercator
    :param east: float, indicating a position in mercator
    :param north: float, indicating a position in mercator
    :returns nothing
    """
    def __init__(self, west, south, east, north):
        self.west, self.south, self.east, self.north = west, south, east, north


def bbox_action(bbox, output_filename):
    xapi_mirror = OSMAXX_CONVERSION_SERVICE.get('XAPI_MIRROR')

    # Download the region map specified through the given coordinates
    command = "wget -qO- {xapi_mirror}?map?bbox={west},{south},{east},{north}".format(
        xapi_mirror=xapi_mirror,
        west=bbox.west,
        south=bbox.south,
        east=bbox.east,
        north=bbox.north,
    )
    p_wget = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
    command = "osmconvert --out-pbf -o={output_filename} -".format(
        output_filename=output_filename,
    )
    # Allow p_wget to receive a SIGPIPE if p_to_pbf exits.
    p_wget.stdout.close()
    subprocess.check_call(command.split(), stdin=p_wget.stdout)
    return output_filename


GEOMETRY_CLASSES_ACTION = {
    BBox.__name__: bbox_action,
}


def cut_osm_extent(geometry_defintion):
    workdir_osm = OSMAXX_CONVERSION_SERVICE.get('WORKDIR')
    output_filename = os.path.join(workdir_osm, 'excerpt.osm.pbf')
    os.makedirs(workdir_osm, exist_ok=True)

    class_name = geometry_defintion.__class__.__name__
    if class_name in GEOMETRY_CLASSES_ACTION:
        return GEOMETRY_CLASSES_ACTION[class_name](geometry_defintion, output_filename)
    else:
        raise NotImplementedError('Currently, ' + class_name + ' cannot be handled')

__all__ = [
    'BBox',
    'GEOMETRY_CLASSES_ACTION',
    'cut_osm_extent',
]

import os
import subprocess
from converters.converter_settings import OSMAXX_CONVERSION_SERVICE


class BBox:
    """
    A pickleable Bounding Box object

    :param west: float, indicating a position in mercator
    :param south: float, indicating a position in mercator
    :param east: float, indicating a position in mercator
    :param north: float, indicating a position in mercator
    :returns nothing
    """
    def __init__(self, west, south, east, north):
        self.west, self.south, self.east, self.north = west, south, east, north


def bbox_action(bbox, output_filename):
    xapi_mirror = OSMAXX_CONVERSION_SERVICE.get('XAPI_MIRROR')

    # Download the region map specified through the given coordinates
    command = "wget -qO- {xapi_mirror}?map?bbox={west},{south},{east},{north}".format(
        xapi_mirror=xapi_mirror,
        west=bbox.west,
        south=bbox.south,
        east=bbox.east,
        north=bbox.north,
    )
    p_wget = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
    command = "osmconvert --out-pbf -o={output_filename} -".format(
        output_filename=output_filename,
    )
    subprocess.check_call(command.split(), stdin=p_wget.stdout)
    return output_filename


GEOMETRY_CLASSES_ACTION = {
    BBox.__name__: bbox_action,
}


def cut_osm_extent(geometry_defintion):
    workdir_osm = OSMAXX_CONVERSION_SERVICE.get('WORKDIR')
    output_filename = os.path.join(workdir_osm, 'excerpt.osm.pbf')
    os.makedirs(workdir_osm, exist_ok=True)

    class_name = geometry_defintion.__class__.__name__
    if class_name in GEOMETRY_CLASSES_ACTION:
        return GEOMETRY_CLASSES_ACTION[class_name](geometry_defintion, output_filename)
    else:
        raise NotImplementedError('Currently, ' + class_name + ' cannot be handled')

__all__ = [
    'BBox',
    'GEOMETRY_CLASSES_ACTION',
    'cut_osm_extent',
]

fix ValueError: I/O operation on closed file: leave stdin to call open when passing it into check_call