# Path: openaddr/sample.py
# def sample_geojson(stream, max_features):
#     ''' Read a stream of input GeoJSON and return a string with a limited feature count.
#     '''
#     features = list()
# 
#     for feature in stream_geojson(stream):
#         if len(features) == max_features:
#             break
# 
#         features.append(feature)
# 
#     geojson = dict(type='FeatureCollection', features=features)
#     return json.dumps(geojson)
# 
# def stream_geojson(stream):
#     '''
#     '''
#     data = ijson.parse(stream)
# 
#     for (prefix1, event1, value1) in data:
#         if event1 != 'start_map':
#             # A root GeoJSON object is a map.
#             raise ValueError((prefix1, event1, value1))
# 
#         for (prefix2, event2, value2) in data:
#             if event2 == 'map_key' and value2 == 'type':
#                 prefix3, event3, value3 = next(data)
# 
#                 if event3 != 'string' and value3 != 'FeatureCollection':
#                     # We only want GeoJSON feature collections
#                     raise ValueError((prefix3, event3, value3))
# 
#             elif event2 == 'map_key' and value2 == 'features':
#                 prefix4, event4, value4 = next(data)
# 
#                 if event4 != 'start_array':
#                     # We only want lists of features here.
#                     raise ValueError((prefix4, event4, value4))
# 
#                 for (prefix5, event5, value5) in data:
#                     if event5 == 'end_array':
#                         break
# 
#                     # let _build_value() handle the feature.
#                     _data = chain([(prefix5, event5, value5)], data)
#                     feature = _build_value(_data)
#                     yield feature


import logging; _L = logging.getLogger('openaddr.conform')
import os
import errno
import tempfile
import mimetypes
import json
import copy
import csv
import re
from zipfile import ZipFile
from locale import getpreferredencoding
from os.path import splitext
from hashlib import sha1
from uuid import uuid4
from .sample import sample_geojson, stream_geojson
from osgeo import ogr, osr, gdal
        unzipped_base = os.path.join(workdir, UNZIPPED_DIRNAME)
        unzipped_paths = dict([(os.path.relpath(source_path, unzipped_base), source_path)
                               for source_path in source_paths])

        if conform['file'] not in unzipped_paths:
            return []

        csv_path = ExcerptDataTask._make_csv_path(unzipped_paths.get(conform['file']))
        return [csv_path]

    @staticmethod
    def _make_csv_path(csv_path):
        _, csv_ext = os.path.splitext(csv_path.lower())

        if csv_ext != '.csv':
            # Convince OGR it's looking at a CSV file.
            new_path = csv_path + '.csv'
            os.link(csv_path, new_path)
            csv_path = new_path

        return csv_path

    @staticmethod
    def _sample_geojson_file(data_path):
        # Sample a few GeoJSON features to save on memory for large datasets.
        with open(data_path, 'r') as complete_layer:
            temp_dir = os.path.dirname(data_path)
            _, temp_path = tempfile.mkstemp(dir=temp_dir, suffix='.json')

            with open(temp_path, 'w') as temp_file:

# Path: openaddr/sample.py
# def sample_geojson(stream, max_features):
#     ''' Read a stream of input GeoJSON and return a string with a limited feature count.
#     '''
#     features = list()
# 
#     for feature in stream_geojson(stream):
#         if len(features) == max_features:
#             break
# 
#         features.append(feature)
# 
#     geojson = dict(type='FeatureCollection', features=features)
#     return json.dumps(geojson)
# 
# def stream_geojson(stream):
#     '''
#     '''
#     data = ijson.parse(stream)
# 
#     for (prefix1, event1, value1) in data:
#         if event1 != 'start_map':
#             # A root GeoJSON object is a map.
#             raise ValueError((prefix1, event1, value1))
# 
#         for (prefix2, event2, value2) in data:
#             if event2 == 'map_key' and value2 == 'type':
#                 prefix3, event3, value3 = next(data)
# 
#                 if event3 != 'string' and value3 != 'FeatureCollection':
#                     # We only want GeoJSON feature collections
#                     raise ValueError((prefix3, event3, value3))
# 
#             elif event2 == 'map_key' and value2 == 'features':
#                 prefix4, event4, value4 = next(data)
# 
#                 if event4 != 'start_array':
#                     # We only want lists of features here.
#                     raise ValueError((prefix4, event4, value4))
# 
#                 for (prefix5, event5, value5) in data:
#                     if event5 == 'end_array':
#                         break
# 
#                     # let _build_value() handle the feature.
#                     _data = chain([(prefix5, event5, value5)], data)
#                     feature = _build_value(_data)
#                     yield feature


# Path: openaddr/conform.py
import logging; _L = logging.getLogger('openaddr.conform')
import os
import errno
import tempfile
import mimetypes
import json
import copy
import csv
import re
from zipfile import ZipFile
from locale import getpreferredencoding
from os.path import splitext
from hashlib import sha1
from uuid import uuid4
from .sample import sample_geojson, stream_geojson
from osgeo import ogr, osr, gdal

        unzipped_base = os.path.join(workdir, UNZIPPED_DIRNAME)
        unzipped_paths = dict([(os.path.relpath(source_path, unzipped_base), source_path)
                               for source_path in source_paths])

        if conform['file'] not in unzipped_paths:
            return []

        csv_path = ExcerptDataTask._make_csv_path(unzipped_paths.get(conform['file']))
        return [csv_path]

    @staticmethod
    def _make_csv_path(csv_path):
        _, csv_ext = os.path.splitext(csv_path.lower())

        if csv_ext != '.csv':
            # Convince OGR it's looking at a CSV file.
            new_path = csv_path + '.csv'
            os.link(csv_path, new_path)
            csv_path = new_path

        return csv_path

    @staticmethod
    def _sample_geojson_file(data_path):
        # Sample a few GeoJSON features to save on memory for large datasets.
        with open(data_path, 'r') as complete_layer:
            temp_dir = os.path.dirname(data_path)
            _, temp_path = tempfile.mkstemp(dir=temp_dir, suffix='.json')

            with open(temp_path, 'w') as temp_file:

                temp_file.write(sample_geojson(complete_layer, 10))