Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MISC] Remove depencency of seqan3 io #44

Merged
merged 4 commits into from
Jan 20, 2022
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
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,14 @@ The following API changes should be documented as such:

If possible, provide tooling that performs the changes, e.g. a shell-script.
-->

## API changes

#### I/O

* In order to avoid using the seqan3 I/O module, you now have to give a list of file extensions explicitly to
`sharg::input_file_validator` and `sharg::output_file_validator`:
For example `sharg::input_file_validator validator{std::vector<std::string>{{"exe"}, {"fasta"}}};`. Please follow
https://github.com/seqan/seqan3/issues/2927 to see how the list of file extensions can be extracted from seqan3 files.
We also removed the `default_extensions()` function, as we now can construct `output_file_validator` with just a given
mode: `output_file_validator(output_file_open_options const mode)`. The extensions will be an empty array in this case.
1 change: 1 addition & 0 deletions include/sharg/argument_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#pragma once

#include <set>
#include <variant>

#include <sharg/detail/format_help.hpp>
#include <sharg/detail/format_html.hpp>
Expand Down
78 changes: 11 additions & 67 deletions include/sharg/validators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@

#pragma once

#include <seqan3/std/algorithm>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can fix that in a separate PR. I will make a follow up issue

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed here #47

#include <fstream>
#include <regex>

#include <seqan3/core/debug_stream/detail/to_string.hpp>
#include <seqan3/core/debug_stream/range.hpp>
#include <seqan3/io/detail/misc.hpp>

#include <sharg/detail/safe_filesystem_entry.hpp>
#include <sharg/exceptions.hpp>
Expand Down Expand Up @@ -262,7 +262,6 @@ value_list_validator(range_type && rng) -> value_list_validator<std::ranges::ran

/*!\brief An abstract base class for the file and directory validators.
* \ingroup argument_parser
* \tparam file_t The type of the file to get the valid extensions for; `void` on default.
*
* \details
*
Expand Down Expand Up @@ -458,7 +457,6 @@ class file_validator_base
/*!\brief A validator that checks if a given path is a valid input file.
* \ingroup argument_parser
* \implements sharg::validator
* \tparam file_t The type of the file to get the valid extensions for; `void` on default.
*
* \details
*
Expand All @@ -469,47 +467,26 @@ class file_validator_base
*
* \include test/snippet/argument_parser/validators_input_file.cpp
*
* The valid extensions can also be obtained from a seqan3 formatted file type, e.g. sharg::sequence_input_file, if
* it is given as template argument to this class. The following snippet demonstrates the different ways to instantiate
* the sharg::input_file_validator.
* The following snippet demonstrates the different ways to instantiate the sharg::input_file_validator.
*
* \include test/snippet/argument_parser/validators_input_file_ext_from_file.cpp
*
* \note The validator works on every type that can be implicitly converted to std::filesystem::path.
*
* \remark For a complete overview, take a look at \ref argument_parser
*/
template <typename file_t = void>
smehringer marked this conversation as resolved.
Show resolved Hide resolved
class input_file_validator : public file_validator_base
{
public:

static_assert(std::same_as<file_t, void> || seqan3::detail::has_type_valid_formats<file_t>,
"Expected either a template type with a static member called valid_formats (a file type) or void.");

// Import from base class.
using typename file_validator_base::option_value_type;

/*!\name Constructors, destructor and assignment
* \{
*/

/*!\brief Default constructor.
*
* \details
*
* If the class' template argument `file_t` names a valid seqan3 file type that contains a
* static member `valid_formats`, e.g. seqan3::sequence_input_file::valid_formats, then it generates the
* list of valid extensions from this file. Otherwise the extensions list is empty.
*/
input_file_validator()
smehringer marked this conversation as resolved.
Show resolved Hide resolved
{
if constexpr (!std::same_as<file_t, void>)
file_validator_base::extensions = seqan3::detail::valid_file_extensions<typename file_t::valid_formats>();

file_validator_base::extensions_str = create_extensions_str();
}

input_file_validator() = default; //!< Defaulted.
input_file_validator(input_file_validator const &) = default; //!< Defaulted.
smehringer marked this conversation as resolved.
Show resolved Hide resolved
input_file_validator(input_file_validator &&) = default; //!< Defaulted.
input_file_validator & operator=(input_file_validator const &) = default; //!< Defaulted.
Expand All @@ -518,17 +495,8 @@ class input_file_validator : public file_validator_base

/*!\brief Constructs from a given collection of valid extensions.
* \param[in] extensions The valid extensions to validate for.
*
* \details
*
* This constructor is only available if `file_t` does not name a valid seqan3 file type that contains a
* static member `valid_formats`.
*/
explicit input_file_validator(std::vector<std::string> extensions)
//!\cond
requires std::same_as<file_t, void>
//!\endcond
: file_validator_base{}
explicit input_file_validator(std::vector<std::string> extensions) : file_validator_base{}
{
file_validator_base::extensions = std::move(extensions);
file_validator_base::extensions_str = create_extensions_str();
Expand Down Expand Up @@ -592,7 +560,6 @@ enum class output_file_open_options
/*!\brief A validator that checks if a given path is a valid output file.
* \ingroup argument_parser
* \implements sharg::validator
* \tparam file_t The type of the file to get the valid extensions for; `void` on default.
*
* \details
*
Expand All @@ -607,22 +574,17 @@ enum class output_file_open_options
*
* \include test/snippet/argument_parser/validators_output_file.cpp
*
* The valid extensions can also be obtained from a seqan3 formatted file type, e.g. sharg::sequence_input_file, if
* it is given as template argument to this class. The following snippet demonstrates the different ways to instantiate
* the sharg::output_file_validator.
* The following snippet demonstrates the different ways to instantiate the sharg::output_file_validator.
*
* \include test/snippet/argument_parser/validators_output_file_ext_from_file.cpp
*
* \note The validator works on every type that can be implicitly converted to std::filesystem::path.
*
* \remark For a complete overview, take a look at \ref argument_parser
*/
template <typename file_t = void>
class output_file_validator : public file_validator_base
{
public:
static_assert(std::same_as<file_t, void> || seqan3::detail::has_type_valid_formats<file_t>,
"Expected either a template type with a static member called valid_formats (a file type) or void.");

// Import from base class.
using typename file_validator_base::option_value_type;
Expand All @@ -632,23 +594,20 @@ class output_file_validator : public file_validator_base
*/

//!\copydoc sharg::input_file_validator::input_file_validator()
output_file_validator() : output_file_validator{output_file_open_options::create_new}
{}
output_file_validator() : output_file_validator{output_file_open_options::create_new} {}

output_file_validator(output_file_validator const &) = default; //!< Defaulted.
output_file_validator(output_file_validator &&) = default; //!< Defaulted.
output_file_validator(output_file_validator const &) = default; //!< Defaulted.
output_file_validator(output_file_validator &&) = default; //!< Defaulted.
output_file_validator & operator=(output_file_validator const &) = default; //!< Defaulted.
output_file_validator & operator=(output_file_validator &&) = default; //!< Defaulted.
output_file_validator & operator=(output_file_validator &&) = default; //!< Defaulted.
virtual ~output_file_validator() = default; //!< Virtual Destructor.

/*!\brief Constructs from a given overwrite mode and a list of valid extensions.
* \param[in] mode A sharg::output_file_open_options indicating whether the validator throws if a file already
exists.
* \param[in] extensions The valid extensions to validate for. Defaults to
* sharg::output_file_validator::default_extensions.
* \param[in] extensions The valid extensions to validate for.
*/
explicit output_file_validator(output_file_open_options const mode,
std::vector<std::string> extensions = default_extensions())
explicit output_file_validator(output_file_open_options const mode, std::vector<std::string> extensions = {})
: file_validator_base{}, mode{mode}
{
file_validator_base::extensions = std::move(extensions);
Expand All @@ -659,21 +618,6 @@ class output_file_validator : public file_validator_base
using file_validator_base::file_validator_base;
//!\}

/*!\brief The default extensions of `file_t`.
* \returns A list of default extensions for `file_t`, will be empty if `file_t` is `void`.
*
* \details
*
* If `file_t` does name a valid seqan3 file type that contains a static member `valid_formats` returns the
* extensions of that `file_t` type. Otherwise returns an empty list.
*/
static std::vector<std::string> default_extensions()
{
if constexpr (!std::same_as<file_t, void>)
return seqan3::detail::valid_file_extensions<typename file_t::valid_formats>();
return {};
}

// Import the base::operator()
using file_validator_base::operator();

Expand Down
6 changes: 0 additions & 6 deletions test/snippet/validators_input_file_ext_from_file.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include <seqan3/io/sequence_file/input.hpp>

#include <sharg/validators.hpp>

int main()
Expand All @@ -12,9 +10,5 @@ int main()
sharg::input_file_validator validator2{std::vector{std::string{"exe"}, std::string{"fasta"}}};
std::cerr << validator2.get_help_page_message() << '\n';

// Give the seqan3 file type as a template argument to get all valid extensions for this file.
sharg::input_file_validator<seqan3::sequence_file_input<>> validator3{};
std::cerr << validator3.get_help_page_message() << '\n';

return 0;
}
1 change: 0 additions & 1 deletion test/snippet/validators_input_file_ext_from_file.err
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
The input file must exist and read permissions must be granted.
The input file must exist and read permissions must be granted. Valid file extensions are: [exe, fasta].
The input file must exist and read permissions must be granted. Valid file extensions are: [embl, fasta, fa, fna, ffn, faa, frn, fas, fastq, fq, genbank, gb, gbk, sam].
11 changes: 1 addition & 10 deletions test/snippet/validators_output_file_ext_from_file.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include <seqan3/io/sequence_file/output.hpp>

#include <sharg/validators.hpp>

int main()
Expand All @@ -10,15 +8,8 @@ int main()

// Specify your own extensions for the output file.
sharg::output_file_validator validator2{sharg::output_file_open_options::create_new,
std::vector{std::string{"exe"}, std::string{"fasta"}}};
std::vector{std::string{"exe"}, std::string{"fasta"}}};
std::cerr << validator2.get_help_page_message() << '\n';

// Give the seqan3 file type as a template argument to get all valid extensions for this file.
sharg::output_file_validator<seqan3::sequence_file_output<>> validator3
{
sharg::output_file_open_options::create_new
};
std::cerr << validator3.get_help_page_message() << '\n';

return 0;
}
1 change: 0 additions & 1 deletion test/snippet/validators_output_file_ext_from_file.err
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
The output file must not exist already and write permissions must be granted.
The output file must not exist already and write permissions must be granted. Valid file extensions are: [exe, fasta].
The output file must not exist already and write permissions must be granted. Valid file extensions are: [embl, fasta, fa, fna, ffn, faa, frn, fas, fastq, fq, genbank, gb, gbk, sam].
Loading