# Path: openaddr/conform.py
# import logging; _L = logging.getLogger('openaddr.conform')
# OPENADDR_CSV_SCHEMA = ['LON', 'LAT', 'NUMBER', 'STREET', 'UNIT', 'CITY',
#                        'DISTRICT', 'REGION', 'POSTCODE', 'ID', 'HASH']
# GEOM_FIELDNAME = 'OA:geom'
# X_FIELDNAME, Y_FIELDNAME = 'OA:x', 'OA:y'
# UNZIPPED_DIRNAME = 'unzipped'
# def gdal_error_handler(err_class, err_num, err_msg):
# def mkdirsp(path):
#     def __init__(self, processed, sample, website, license, geometry_type,
#                  address_count, path, elapsed, sharealike_flag,
#                  attribution_flag, attribution_name):
#     def empty():
#     def todict(self):
#     def from_format_string(clz, format_string):
#     def decompress(self, source_paths):
#     def decompress(self, source_paths, workdir, filenames):
# def is_in(path, names):
#     def decompress(self, source_paths, workdir, filenames):
#     def excerpt(self, source_paths, workdir, conform):
#     def _get_known_paths(source_paths, workdir, conform, known_types):
#     def _make_csv_path(csv_path):
#     def _sample_geojson_file(data_path):
#     def _excerpt_csv_file(data_path, encoding, csvsplit):
# def elaborate_filenames(filename):
# def guess_source_encoding(datasource, layer):
# def find_source_path(source_definition, source_paths):
#     def convert(self, source_definition, source_paths, workdir):
# def convert_regexp_replace(replace):
# def normalize_ogr_filename_case(source_path):
# def ogr_source_to_csv(source_definition, source_path, dest_path):
# def csv_source_to_csv(source_definition, source_path, dest_path):
# def geojson_source_to_csv(source_path, dest_path):
# def _transform_to_4326(srs):
# def row_extract_and_reproject(source_definition, source_row):
# def row_function(sd, row, key, fxn):
# def row_transform_and_convert(sd, row):
# def fxn_smash_case(fxn):
# def conform_smash_case(source_definition):
# def row_smash_case(sd, input):
# def row_merge(sd, row, key):
# def row_fxn_join(sd, row, key, fxn):
# def row_fxn_regexp(sd, row, key, fxn):
# def row_fxn_prefixed_number(sd, row, key, fxn):
# def row_fxn_postfixed_street(sd, row, key, fxn):
# def row_fxn_postfixed_unit(sd, row, key, fxn):
# def row_fxn_remove_prefix(sd, row, key, fxn):
# def row_fxn_remove_postfix(sd, row, key, fxn):
# def row_fxn_format(sd, row, key, fxn):
# def row_fxn_chain(sd, row, key, fxn):
# def row_fxn_first_non_empty(sd, row, key, fxn):
# def row_fxn_get_from_array_string(sd, row, key, fxn):
# def row_canonicalize_unit_and_number(sd, row):
# def _round_wgs84_to_7(n):
# def row_round_lat_lon(sd, row):
# def row_calculate_hash(cache_fingerprint, row):
# def row_convert_to_out(sd, row):
# def extract_to_source_csv(source_definition, source_path, extract_path):
# def transform_to_out_csv(source_definition, extract_path, dest_path):
# def conform_cli(source_definition, source_path, dest_path):
# def conform_license(license):
# def conform_attribution(license, attribution):
# def conform_sharealike(license):
# def check_source_tests(raw_source):
# class ConformResult:
# class DecompressionError(Exception):
# class DecompressionTask(object):
# class GuessDecompressTask(DecompressionTask):
# class ZipDecompressTask(DecompressionTask):
# class ExcerptDataTask(object):
# class ConvertToCsvTask(object):


import os
import copy
import json
import csv
import re
import unittest
import tempfile
import shutil
from ..conform import (
    GEOM_FIELDNAME, X_FIELDNAME, Y_FIELDNAME,
    csv_source_to_csv, find_source_path, row_transform_and_convert,
    row_fxn_regexp, row_smash_case, row_round_lat_lon, row_merge,
    row_extract_and_reproject, row_convert_to_out, row_fxn_join, row_fxn_format,
    row_fxn_prefixed_number, row_fxn_postfixed_street,
    row_fxn_postfixed_unit,
    row_fxn_remove_prefix, row_fxn_remove_postfix, row_fxn_chain,
    row_fxn_first_non_empty,
    row_fxn_get_from_array_string,
    row_canonicalize_unit_and_number, conform_smash_case, conform_cli,
    convert_regexp_replace, conform_license,
    conform_attribution, conform_sharealike, normalize_ogr_filename_case,
    OPENADDR_CSV_SCHEMA, is_in, geojson_source_to_csv, check_source_tests
    )
                "field": "ADDRESS"
            },
            "street": {
                "function": "postfixed_street",
                "field": "ADDRESS"
            }
        } }
        d = { "ADDRESS": "123 MAPLE ST" }
        e = copy.deepcopy(d)
        e.update({ "OA:number": "123", "OA:street": "MAPLE ST" })

        d = row_fxn_prefixed_number(c, d, "number", c["conform"]["number"])
        d = row_fxn_postfixed_street(c, d, "street", c["conform"]["street"])
        self.assertEqual(e, d)

        "Regex prefixed_number and postfixed_street - no number"
        c = { "conform": {
            "number": {
                "function": "prefixed_number",
                "field": "ADDRESS"
            },
            "street": {
                "function": "postfixed_street",
                "field": "ADDRESS"
            }
        } }
        d = { "ADDRESS": "MAPLE ST" }
        e = copy.deepcopy(d)
        e.update({ "OA:number": "", "OA:street": "MAPLE ST" })


# Path: openaddr/conform.py
# import logging; _L = logging.getLogger('openaddr.conform')
# OPENADDR_CSV_SCHEMA = ['LON', 'LAT', 'NUMBER', 'STREET', 'UNIT', 'CITY',
#                        'DISTRICT', 'REGION', 'POSTCODE', 'ID', 'HASH']
# GEOM_FIELDNAME = 'OA:geom'
# X_FIELDNAME, Y_FIELDNAME = 'OA:x', 'OA:y'
# UNZIPPED_DIRNAME = 'unzipped'
# def gdal_error_handler(err_class, err_num, err_msg):
# def mkdirsp(path):
#     def __init__(self, processed, sample, website, license, geometry_type,
#                  address_count, path, elapsed, sharealike_flag,
#                  attribution_flag, attribution_name):
#     def empty():
#     def todict(self):
#     def from_format_string(clz, format_string):
#     def decompress(self, source_paths):
#     def decompress(self, source_paths, workdir, filenames):
# def is_in(path, names):
#     def decompress(self, source_paths, workdir, filenames):
#     def excerpt(self, source_paths, workdir, conform):
#     def _get_known_paths(source_paths, workdir, conform, known_types):
#     def _make_csv_path(csv_path):
#     def _sample_geojson_file(data_path):
#     def _excerpt_csv_file(data_path, encoding, csvsplit):
# def elaborate_filenames(filename):
# def guess_source_encoding(datasource, layer):
# def find_source_path(source_definition, source_paths):
#     def convert(self, source_definition, source_paths, workdir):
# def convert_regexp_replace(replace):
# def normalize_ogr_filename_case(source_path):
# def ogr_source_to_csv(source_definition, source_path, dest_path):
# def csv_source_to_csv(source_definition, source_path, dest_path):
# def geojson_source_to_csv(source_path, dest_path):
# def _transform_to_4326(srs):
# def row_extract_and_reproject(source_definition, source_row):
# def row_function(sd, row, key, fxn):
# def row_transform_and_convert(sd, row):
# def fxn_smash_case(fxn):
# def conform_smash_case(source_definition):
# def row_smash_case(sd, input):
# def row_merge(sd, row, key):
# def row_fxn_join(sd, row, key, fxn):
# def row_fxn_regexp(sd, row, key, fxn):
# def row_fxn_prefixed_number(sd, row, key, fxn):
# def row_fxn_postfixed_street(sd, row, key, fxn):
# def row_fxn_postfixed_unit(sd, row, key, fxn):
# def row_fxn_remove_prefix(sd, row, key, fxn):
# def row_fxn_remove_postfix(sd, row, key, fxn):
# def row_fxn_format(sd, row, key, fxn):
# def row_fxn_chain(sd, row, key, fxn):
# def row_fxn_first_non_empty(sd, row, key, fxn):
# def row_fxn_get_from_array_string(sd, row, key, fxn):
# def row_canonicalize_unit_and_number(sd, row):
# def _round_wgs84_to_7(n):
# def row_round_lat_lon(sd, row):
# def row_calculate_hash(cache_fingerprint, row):
# def row_convert_to_out(sd, row):
# def extract_to_source_csv(source_definition, source_path, extract_path):
# def transform_to_out_csv(source_definition, extract_path, dest_path):
# def conform_cli(source_definition, source_path, dest_path):
# def conform_license(license):
# def conform_attribution(license, attribution):
# def conform_sharealike(license):
# def check_source_tests(raw_source):
# class ConformResult:
# class DecompressionError(Exception):
# class DecompressionTask(object):
# class GuessDecompressTask(DecompressionTask):
# class ZipDecompressTask(DecompressionTask):
# class ExcerptDataTask(object):
# class ConvertToCsvTask(object):


# Path: openaddr/tests/conform.py
import os
import copy
import json
import csv
import re
import unittest
import tempfile
import shutil
from ..conform import (
    GEOM_FIELDNAME, X_FIELDNAME, Y_FIELDNAME,
    csv_source_to_csv, find_source_path, row_transform_and_convert,
    row_fxn_regexp, row_smash_case, row_round_lat_lon, row_merge,
    row_extract_and_reproject, row_convert_to_out, row_fxn_join, row_fxn_format,
    row_fxn_prefixed_number, row_fxn_postfixed_street,
    row_fxn_postfixed_unit,
    row_fxn_remove_prefix, row_fxn_remove_postfix, row_fxn_chain,
    row_fxn_first_non_empty,
    row_fxn_get_from_array_string,
    row_canonicalize_unit_and_number, conform_smash_case, conform_cli,
    convert_regexp_replace, conform_license,
    conform_attribution, conform_sharealike, normalize_ogr_filename_case,
    OPENADDR_CSV_SCHEMA, is_in, geojson_source_to_csv, check_source_tests
    )

                "field": "ADDRESS"
            },
            "street": {
                "function": "postfixed_street",
                "field": "ADDRESS"
            }
        } }
        d = { "ADDRESS": "123 MAPLE ST" }
        e = copy.deepcopy(d)
        e.update({ "OA:number": "123", "OA:street": "MAPLE ST" })

        d = row_fxn_prefixed_number(c, d, "number", c["conform"]["number"])
        d = row_fxn_postfixed_street(c, d, "street", c["conform"]["street"])
        self.assertEqual(e, d)

        "Regex prefixed_number and postfixed_street - no number"
        c = { "conform": {
            "number": {
                "function": "prefixed_number",
                "field": "ADDRESS"
            },
            "street": {
                "function": "postfixed_street",
                "field": "ADDRESS"
            }
        } }
        d = { "ADDRESS": "MAPLE ST" }
        e = copy.deepcopy(d)
        e.update({ "OA:number": "", "OA:street": "MAPLE ST" })


        d = row_fxn_prefixed_number(c, d, "number", c["conform"]["number"])