# Path: OGRgeoConverter/tools/shell.py
# class ShellCommand:
# 
#     '''
#     Class to build and execute Shell Commands
#     '''
# 
#     def __init__(self):
#         self.__command = ''
#         self.__parameter_list = ShellParameterList()
#         self.__result_output = ''
#         self.__result_error = ''
#         self.__commandtimeoutsecs = 600
# 
#     def set_command(self, command):
#         self.__command = command
# 
#     def set_timeout(self, timeout=None):
#         self.__commandtimeoutsecs = timeout
# 
#     def get_command(self):
#         return self.__command
# 
#     def get_full_command_string(self):
#         parameter_string = self.__parameter_list.get_parameters_string()
#         if parameter_string != '':
#             argument_string = ' ' + parameter_string
#         return self.__command + argument_string
# 
#     def add_parameter(
#             self,
#             parameter_name,
#             parameter_value,
#             prefix='-',
#             value_quotation_marks=True):
#         self.__parameter_list.add_parameter(
#             parameter_name,
#             parameter_value,
#             prefix,
#             value_quotation_marks)
# 
#     def add_parameter_object(self, shell_parameter):
#         self.__parameter_list.add_parameter(
#             shell_parameter.parameter_name,
#             shell_parameter.parameter_value,
#             shell_parameter.prefix,
#             shell_parameter.value_quotation_marks)
# 
#     def execute(self):
#         command = self.get_full_command_string()
#         try:
#             args = shlex.split(command)
#             p = Popen(args, stdout=PIPE, stderr=PIPE)
#             stdout, stderr = p.communicate(timeout=self.__commandtimeoutsecs)
#         except TimeoutExpired as e:
#             # The child process is not killed if the timeout expires
#             p.kill()
#             stdout, stderr = p.communicate()
#             if not stderr:
#                 stderr = e
#         except Exception as e:
#             stdout = ''
#             stderr = e
# 
#         encoding = locale.getdefaultlocale()[1]
#         if encoding is None:
#             encoding = 'utf-8'
#         if stdout is not None:
#             self.__result_output = stdout.decode(encoding) if hasattr(stdout, 'decode') else stdout
#         if stderr is not None:
#             self.__result_error = stderr.decode(encoding) if hasattr(stderr, 'decode') else stderr
# 
#     def get_result_output(self):
#         return self.__result_output
# 
#     def get_result_error(self):
#         return self.__result_error
# 
#     def has_error(self):
#         return self.__result_error != ''


from OGRgeoConverter.tools.shell import ShellCommand
from collections import namedtuple
import re
'''



def get_version_based_format_re():
    """Retrieve regex to match version specific --formats output
    Example output of version 1.x of ogr2ogr:
      -> "ESRI Shapefile" (read/write)
      -> "UK .NTF" (readonly)
    Example output of version 2.x of ogr2ogr:
      ESRI Shapefile -vector- (rw+v): ESRI Shapefile
      UK .NTF -vector- (ro): UK .NTF
    """
    shell_command = ShellCommand()
    shell_command.set_command('ogrinfo')
    shell_command.add_parameter('version', None, '--')
    shell_command.execute()
    version_pattern = re.compile(
        r'^GDAL\s+(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+[^,]*),.*$', re.M)
    re_match = version_pattern.match(shell_command.get_result_output())
    if re_match is not None:
        if re_match.group('major') == '2':
            return re.compile(
                r'^\s+(?P<name>.*) -[^(]+\(((?P<rw>rw)|(?P<r>ro)|(?P<w>w))(?P<otherflags>[^)]*)\):.*$')
    return re.compile(
        r'^\s+->[^"]*"(?P<name>[^"]+)"[^(]+\(((?P<rw>read/write)|(?P<r>readonly)|(?P<w>writeonly))\).*$')


def get_supported_formats():
    format_re = get_version_based_format_re()

# Path: OGRgeoConverter/tools/shell.py
# class ShellCommand:
# 
#     '''
#     Class to build and execute Shell Commands
#     '''
# 
#     def __init__(self):
#         self.__command = ''
#         self.__parameter_list = ShellParameterList()
#         self.__result_output = ''
#         self.__result_error = ''
#         self.__commandtimeoutsecs = 600
# 
#     def set_command(self, command):
#         self.__command = command
# 
#     def set_timeout(self, timeout=None):
#         self.__commandtimeoutsecs = timeout
# 
#     def get_command(self):
#         return self.__command
# 
#     def get_full_command_string(self):
#         parameter_string = self.__parameter_list.get_parameters_string()
#         if parameter_string != '':
#             argument_string = ' ' + parameter_string
#         return self.__command + argument_string
# 
#     def add_parameter(
#             self,
#             parameter_name,
#             parameter_value,
#             prefix='-',
#             value_quotation_marks=True):
#         self.__parameter_list.add_parameter(
#             parameter_name,
#             parameter_value,
#             prefix,
#             value_quotation_marks)
# 
#     def add_parameter_object(self, shell_parameter):
#         self.__parameter_list.add_parameter(
#             shell_parameter.parameter_name,
#             shell_parameter.parameter_value,
#             shell_parameter.prefix,
#             shell_parameter.value_quotation_marks)
# 
#     def execute(self):
#         command = self.get_full_command_string()
#         try:
#             args = shlex.split(command)
#             p = Popen(args, stdout=PIPE, stderr=PIPE)
#             stdout, stderr = p.communicate(timeout=self.__commandtimeoutsecs)
#         except TimeoutExpired as e:
#             # The child process is not killed if the timeout expires
#             p.kill()
#             stdout, stderr = p.communicate()
#             if not stderr:
#                 stderr = e
#         except Exception as e:
#             stdout = ''
#             stderr = e
# 
#         encoding = locale.getdefaultlocale()[1]
#         if encoding is None:
#             encoding = 'utf-8'
#         if stdout is not None:
#             self.__result_output = stdout.decode(encoding) if hasattr(stdout, 'decode') else stdout
#         if stderr is not None:
#             self.__result_error = stderr.decode(encoding) if hasattr(stderr, 'decode') else stderr
# 
#     def get_result_output(self):
#         return self.__result_output
# 
#     def get_result_error(self):
#         return self.__result_error
# 
#     def has_error(self):
#         return self.__result_error != ''


# Path: OGRgeoConverter/tools/ogrinfo.py
from OGRgeoConverter.tools.shell import ShellCommand
from collections import namedtuple
import re

'''



def get_version_based_format_re():
    """Retrieve regex to match version specific --formats output
    Example output of version 1.x of ogr2ogr:
      -> "ESRI Shapefile" (read/write)
      -> "UK .NTF" (readonly)
    Example output of version 2.x of ogr2ogr:
      ESRI Shapefile -vector- (rw+v): ESRI Shapefile
      UK .NTF -vector- (ro): UK .NTF
    """
    shell_command = ShellCommand()
    shell_command.set_command('ogrinfo')
    shell_command.add_parameter('version', None, '--')
    shell_command.execute()
    version_pattern = re.compile(
        r'^GDAL\s+(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+[^,]*),.*$', re.M)
    re_match = version_pattern.match(shell_command.get_result_output())
    if re_match is not None:
        if re_match.group('major') == '2':
            return re.compile(
                r'^\s+(?P<name>.*) -[^(]+\(((?P<rw>rw)|(?P<r>ro)|(?P<w>w))(?P<otherflags>[^)]*)\):.*$')
    return re.compile(
        r'^\s+->[^"]*"(?P<name>[^"]+)"[^(]+\(((?P<rw>read/write)|(?P<r>readonly)|(?P<w>writeonly))\).*$')


def get_supported_formats():
    format_re = get_version_based_format_re()

    shell_command = ShellCommand()