# Path: foil/paths.py
# def match_files(files, pattern: Pattern):
#     """Yields file name if matches a regular expression pattern."""
# 
#     for name in files:
#         if re.match(pattern, name):
#             yield name
# 
# def match_file_listing(paths, pattern: Pattern):
#     for path in paths:
#         filename = os.path.basename(path)
# 
#         if re.match(pattern, filename):
#             yield path
# 
# def get_file_listing_sha(listing_paths: Iterable) -> str:
#     """Return sha256 string for group of FTP listings."""
# 
#     return sha256(''.join(sorted(listing_paths)).encode('utf-8')).hexdigest()
# 
# def match_zipfile_members(zipfile_path: str, pattern: Pattern):
#     """Match files to a pattern within a zip file's content."""
# 
#     with ZipFile(zipfile_path, mode='r') as zfile:
#         members = zfile.namelist()
# 
#     yield from match_files(members, pattern)
# 
# def find_zipfile_member(zipfile_path: str, pattern: Pattern):
#     """Return the first match to a regex within a zip file's content."""
# 
#     return next(match_zipfile_members(zipfile_path, pattern))
# 
# def strip_full_path(path):
#     """String away a path separator prefix."""
# 
#     relative_path = path.lstrip(os.sep)
#     return relative_path
# 
# def merge_paths(base_path, path):
#     """Merge a base path with another full path, path."""
# 
#     return os.path.join(base_path, strip_full_path(path))
# 
# def strip_file_listing(paths):
#     """Remove the file listing(s) from the directory path(s)."""
# 
#     return (os.path.split(path)[0] for path in paths)
# 
# def unique_listing_directories(file_listing) -> set:
#     """Return unique directory set for file listing collection."""
# 
#     directories = strip_file_listing(file_listing)
#     return set(directories)


import os
import unittest
import zipfile
from tempfile import NamedTemporaryFile
from foil.paths import (match_files, match_file_listing,
                        get_file_listing_sha,
                        match_zipfile_members,
                        find_zipfile_member,
                        strip_full_path, merge_paths,
                        strip_file_listing, unique_listing_directories)
        from hashlib import sha256

        self.assertEqual(expected, result)

    def test_merge_paths(self):
        directory_name = 'blah_sub_directory'
        sub_directory = os.sep + directory_name

        path2 = os.path.join(self.base_path, sub_directory)

        expected = os.path.join(self.base_path, directory_name)
        result = merge_paths(self.base_path, path2)

        self.assertEqual(expected, result)


class TestFileDirectoryListing(unittest.TestCase):

    def setUp(self):
        self.paths = ['/foobar/bar.zip', '/foobar/foo.txt', '/foobar/bar/foo.txt']

    def test_strip_file_listing(self):
        expected = ['/foobar', '/foobar', '/foobar/bar']
        result = list(strip_file_listing(self.paths))

        self.assertEqual(expected, result)

    def test_unique_listing_directories(self):
        expected = set(['/foobar', '/foobar/bar'])
        result = unique_listing_directories(self.paths)


# Path: foil/paths.py
# def match_files(files, pattern: Pattern):
#     """Yields file name if matches a regular expression pattern."""
# 
#     for name in files:
#         if re.match(pattern, name):
#             yield name
# 
# def match_file_listing(paths, pattern: Pattern):
#     for path in paths:
#         filename = os.path.basename(path)
# 
#         if re.match(pattern, filename):
#             yield path
# 
# def get_file_listing_sha(listing_paths: Iterable) -> str:
#     """Return sha256 string for group of FTP listings."""
# 
#     return sha256(''.join(sorted(listing_paths)).encode('utf-8')).hexdigest()
# 
# def match_zipfile_members(zipfile_path: str, pattern: Pattern):
#     """Match files to a pattern within a zip file's content."""
# 
#     with ZipFile(zipfile_path, mode='r') as zfile:
#         members = zfile.namelist()
# 
#     yield from match_files(members, pattern)
# 
# def find_zipfile_member(zipfile_path: str, pattern: Pattern):
#     """Return the first match to a regex within a zip file's content."""
# 
#     return next(match_zipfile_members(zipfile_path, pattern))
# 
# def strip_full_path(path):
#     """String away a path separator prefix."""
# 
#     relative_path = path.lstrip(os.sep)
#     return relative_path
# 
# def merge_paths(base_path, path):
#     """Merge a base path with another full path, path."""
# 
#     return os.path.join(base_path, strip_full_path(path))
# 
# def strip_file_listing(paths):
#     """Remove the file listing(s) from the directory path(s)."""
# 
#     return (os.path.split(path)[0] for path in paths)
# 
# def unique_listing_directories(file_listing) -> set:
#     """Return unique directory set for file listing collection."""
# 
#     directories = strip_file_listing(file_listing)
#     return set(directories)


# Path: tests/test_paths.py
import os
import unittest
import zipfile
from tempfile import NamedTemporaryFile
from foil.paths import (match_files, match_file_listing,
                        get_file_listing_sha,
                        match_zipfile_members,
                        find_zipfile_member,
                        strip_full_path, merge_paths,
                        strip_file_listing, unique_listing_directories)
        from hashlib import sha256


        self.assertEqual(expected, result)

    def test_merge_paths(self):
        directory_name = 'blah_sub_directory'
        sub_directory = os.sep + directory_name

        path2 = os.path.join(self.base_path, sub_directory)

        expected = os.path.join(self.base_path, directory_name)
        result = merge_paths(self.base_path, path2)

        self.assertEqual(expected, result)


class TestFileDirectoryListing(unittest.TestCase):

    def setUp(self):
        self.paths = ['/foobar/bar.zip', '/foobar/foo.txt', '/foobar/bar/foo.txt']

    def test_strip_file_listing(self):
        expected = ['/foobar', '/foobar', '/foobar/bar']
        result = list(strip_file_listing(self.paths))

        self.assertEqual(expected, result)

    def test_unique_listing_directories(self):
        expected = set(['/foobar', '/foobar/bar'])
        result = unique_listing_directories(self.paths)


        self.assertEqual(expected, result)