Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/filepattern/cpp/internal/filepattern.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "filepattern.hpp"
#include "../util/util.hpp"
#include <chrono>

using namespace std;
Expand All @@ -25,7 +26,7 @@ FilePatternObject::FilePatternObject(const string& path, const string& file_patt
this->recursive_ = recursive; // Iterate over subdirectories

// check if filepattern contains directory capturing
if (file_pattern.find('/') != std::string::npos || file_pattern.find('\\') != std::string::npos) {
if (s::isPath(file_pattern)) {
this->setCaptureDirectoryNames(true);
this->recursive_ = true; // need to be recursive to capture directory names

Expand Down Expand Up @@ -76,6 +77,7 @@ void FilePatternObject::matchFilesOneDir(){
auto length = end - start;
throw invalid_argument("Invalid pattern found in bracket expressions in filepattern: \"" + this->getRegexFilePattern().substr(start, length+1) + "\"");
}

regex pattern_regex = regex(this->getRegexFilePattern());
smatch sm;

Expand Down Expand Up @@ -115,7 +117,7 @@ void FilePatternObject::matchFilesMultDir(){
}

if(regex_match(file, sm, pattern_regex)){

if(this->getJustPath() || this->captureDirectoryNames()) {
tup = getVariableMap(file_path, sm);
} else {
Expand Down
23 changes: 23 additions & 0 deletions src/filepattern/cpp/util/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,35 @@ static const std::string SLASH = "/";
#endif




/**
* @brief Functions for std::strings and Types.
*
*/
namespace s {

inline bool isPath(const std::string& str) {

if (str.find('/') != std::string::npos) {
return true;
}

// regex characters to escape
const std::unordered_set<char> escape_chars = {'*', '?', '^', '$', '(', ')', '[', ']', '|'};

for (auto i = 0; i < str.size()-1; ++i) {
if (str[i] == '\\' &&
std::find(std::begin(escape_chars), std::end(escape_chars), str[i+1]) == std::end(escape_chars)
) {
return true; // Contains a slash that is not proceeded by a character being escaped
}
}

// no slashes were found that are not used for escaping characters
return false;
}

inline std::string escapeForwardSlashes(const std::string& input) {
std::string result;

Expand Down
20 changes: 20 additions & 0 deletions tests/test_filepattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ class TestFilePattern():
test_generate_filepattern_data.generate_data()
test_generate_filepattern_data.generate_channel_data()
test_generate_filepattern_data.generate_sorted_data()
test_generate_filepattern_data.generate_bracket_data()

def test_file_pattern(self):

Expand Down Expand Up @@ -636,6 +637,25 @@ def test_file_pattern_sorting(self):

assert sorted(indices) == indices

def test_file_pattern_brackets(self):

bracket_path = self.root_directory + '/test_data/bracket_data/'

pattern = "x\\(0-31\\)_y\\(01-48\\)_c{c:d}.ome.tif"

files = fp.FilePattern(bracket_path, pattern)

result = []

for file in files: # test iterator without call
result.append(file)

print(result)

for i in range(len(result)):
result[i][0]['c'] == i
os.path.basename(result[i][1][0]) == f'x(0-31)_y(01-48)_c{i}.ome.tif'


# Todo: These tests need new data to be added after replacing the old version of filepattern.
"""
Expand Down
27 changes: 27 additions & 0 deletions tests/test_generate_filepattern_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,39 @@ def generate_text_data():
filename = f"img_r{r:03}_c{c:03}.tif"
file.write(filename + "\n")

def generate_bracket_data():
directory = 'test_data'
root_directory = os.path.dirname(os.path.realpath(__file__))
path = os.path.join(root_directory, directory)
data_path = path + '/bracket_data'

try:
os.mkdir(path)
print('Data directory created at ' + path)
except FileExistsError:
print("Data directory already exists")

try:
os.mkdir(data_path)
print('Data directory created at ' + data_path)
except FileExistsError:
print("Data directory already exists")

for i in range(0, MAX):
data1 = 'x(0-31)_y(01-48)_c0.ome.tif'
data2 = 'x(0-31)_y(01-48)_c1.ome.tif'
f1 = open(data_path + '/' + data1, 'w+')
f1.close()
f2 = open(data_path + '/' + data2, 'w+')
f2.close()


if __name__ == '__main__':
generate_data()
generate_channel_data()
generate_sorted_data()
generate_text_data()
generate_bracket_data()


MAX = 3
Expand Down
Loading