Skip to content

Commit

Permalink
Bind Query alike to C++
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoinePrv committed Jan 31, 2024
1 parent 15ce499 commit 733eea8
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 134 deletions.
11 changes: 11 additions & 0 deletions libmamba/include/mamba/api/repoquery.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//
// The full license is in the file LICENSE, distributed with this software.

#include <iosfwd>
#include <string>
#include <vector>

Expand All @@ -21,6 +22,16 @@ namespace mamba
RecursiveTable = 4,
};

[[nodiscard]] auto make_repoquery(
MPool& pool,
QueryType type,
QueryResultFormat format,
const std::vector<std::string>& queries,
bool show_all_builds,
const Context::GraphicsParams& graphics_params,
std::ostream& out
) -> bool;

[[nodiscard]] auto repoquery(
Configuration& config,
QueryType type,
Expand Down
14 changes: 10 additions & 4 deletions libmamba/include/mamba/core/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,27 @@ namespace mamba
auto operator=(const QueryResult&) -> QueryResult& = default;
auto operator=(QueryResult&&) -> QueryResult& = default;

[[nodiscard]] auto query_type() const -> QueryType;
[[nodiscard]] auto type() const -> QueryType;
[[nodiscard]] auto query() const -> const std::string&;
[[nodiscard]] auto empty() const -> bool;

auto sort(std::string_view field) -> QueryResult&;

auto groupby(std::string_view field) -> QueryResult&;

auto reset() -> QueryResult&;

auto table(std::ostream&) const -> std::ostream&;
auto table(std::ostream&, const std::vector<std::string_view>& fmt) const -> std::ostream&;
[[nodiscard]] auto table_to_str() const -> std::string;

auto tree(std::ostream&, const GraphicsParams& graphics) const -> std::ostream&;
[[nodiscard]] auto json() const -> nlohmann::json;
[[nodiscard]] auto tree_to_str(const GraphicsParams& graphics) const -> std::string;

auto pretty(std::ostream&, const Context::OutputParams& outputParams) const -> std::ostream&;
[[nodiscard]] auto json() const -> nlohmann::json;

[[nodiscard]] auto empty() const -> bool;
auto pretty(std::ostream&, bool show_all_builds) const -> std::ostream&;
[[nodiscard]] auto pretty_to_str(bool show_all_builds) const -> std::string;

private:

Expand Down
56 changes: 39 additions & 17 deletions libmamba/src/api/repoquery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,30 +76,29 @@ namespace mamba
}
}

bool repoquery(
Configuration& config,
auto make_repoquery(
MPool& pool,
QueryType type,
QueryResultFormat format,
bool use_local,
const std::vector<std::string>& queries
)
const std::vector<std::string>& queries,
bool show_all_builds,
const Context::GraphicsParams& graphics_params,
std::ostream& out
) -> bool
{
auto& ctx = config.context();
auto pool = repoquery_init(ctx, config, format, use_local);

if (type == QueryType::Search)
{
auto res = Query::find(pool, queries);
switch (format)
{
case QueryResultFormat::Json:
std::cout << res.groupby("name").json().dump(4);
out << res.groupby("name").json().dump(4);
break;
case QueryResultFormat::Pretty:
res.pretty(std::cout, ctx.output_params);
res.pretty(out, show_all_builds);
break;
default:
res.groupby("name").table(std::cout);
res.groupby("name").table(out);
}
return !res.empty();
}
Expand All @@ -119,14 +118,14 @@ namespace mamba
{
case QueryResultFormat::Tree:
case QueryResultFormat::Pretty:
res.tree(std::cout, config.context().graphics_params);
res.tree(out, graphics_params);
break;
case QueryResultFormat::Json:
std::cout << res.json().dump(4);
out << res.json().dump(4);
break;
case QueryResultFormat::Table:
case QueryResultFormat::RecursiveTable:
res.sort("name").table(std::cout);
res.sort("name").table(out);
}
return !res.empty();
}
Expand All @@ -146,15 +145,15 @@ namespace mamba
{
case QueryResultFormat::Tree:
case QueryResultFormat::Pretty:
res.tree(std::cout, config.context().graphics_params);
res.tree(out, graphics_params);
break;
case QueryResultFormat::Json:
std::cout << res.json().dump(4);
out << res.json().dump(4);
break;
case QueryResultFormat::Table:
case QueryResultFormat::RecursiveTable:
res.sort("name").table(
std::cout,
out,
{ "Name",
"Version",
"Build",
Expand All @@ -167,5 +166,28 @@ namespace mamba
}
return !res.empty();
}
throw std::invalid_argument("Invalid QueryType");
}

bool repoquery(
Configuration& config,
QueryType type,
QueryResultFormat format,
bool use_local,
const std::vector<std::string>& queries
)
{
auto& ctx = config.context();
auto pool = repoquery_init(ctx, config, format, use_local);
return make_repoquery(
pool,
type,
format,
queries,
ctx.output_params.verbosity > 0,
ctx.graphics_params,
std::cout
);
}

}
28 changes: 24 additions & 4 deletions libmamba/src/core/query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ namespace mamba
reset_pkg_view_list();
}

auto QueryResult::query_type() const -> QueryType
auto QueryResult::type() const -> QueryType
{
return m_type;
}
Expand Down Expand Up @@ -590,6 +590,13 @@ namespace mamba
);
}

auto QueryResult::table_to_str() const -> std::string
{
auto ss = std::stringstream();
table(ss);
return ss.str();
}

namespace
{
/** Remove potential subdir from channel name (not url!). */
Expand Down Expand Up @@ -867,6 +874,13 @@ namespace mamba
return out;
}

auto QueryResult::tree_to_str(const Context::GraphicsParams& params) const -> std::string
{
auto ss = std::stringstream();
tree(ss, params);
return ss.str();
}

auto QueryResult::json() const -> nlohmann::json
{
nlohmann::json j;
Expand Down Expand Up @@ -912,8 +926,7 @@ namespace mamba
return j;
}

auto QueryResult::pretty(std::ostream& out, const Context::OutputParams& outputParams) const
-> std::ostream&
auto QueryResult::pretty(std::ostream& out, bool show_all_builds) const -> std::ostream&
{
if (m_pkg_id_list.empty())
{
Expand All @@ -934,13 +947,20 @@ namespace mamba
out,
entry.second[0],
std::vector(entry.second.begin() + 1, entry.second.end()),
outputParams.verbosity > 0
show_all_builds
);
}
}
return out;
}

auto QueryResult::pretty_to_str(bool show_all_builds) const -> std::string
{
auto ss = std::stringstream();
pretty(ss, show_all_builds);
return ss.str();
}

auto QueryResult::empty() const -> bool
{
return m_dep_graph.empty();
Expand Down
129 changes: 20 additions & 109 deletions libmambapy/src/libmambapy/bindings/legacy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,13 +497,6 @@ bind_submodule_impl(pybind11::module_ m)
.def_static("parse", &query_type_parse);
py::implicitly_convertible<py::str, QueryType>();

/*py::class_<Query>(m, "Query")
.def(py::init<MPool&>())
.def("find", &Query::find)
.def("whoneeds", &Query::whoneeds)
.def("depends", &Query::depends)
;*/

py::enum_<QueryResultFormat>(m, "QueryResultFormat")
.value("Json", QueryResultFormat::Json)
.value("Tree", QueryResultFormat::Tree)
Expand All @@ -513,112 +506,30 @@ bind_submodule_impl(pybind11::module_ m)
.def(py::init(&mambapy::enum_from_str<QueryResultFormat>));
py::implicitly_convertible<py::str, QueryType>();

auto queries_find = [](MPool& pool,
const std::vector<std::string>& queries,
const QueryResultFormat format) -> std::string
{
QueryResult res = Query::find(pool, queries);
std::stringstream res_stream;
switch (format)
{
case QueryResultFormat::Json:
res_stream << res.groupby("name").json().dump(4);
break;
case QueryResultFormat::Tree:
case QueryResultFormat::Table:
case QueryResultFormat::RecursiveTable:
res.groupby("name").table(res_stream);
break;
case QueryResultFormat::Pretty:
res.groupby("name").pretty(res_stream, mambapy::singletons.context().output_params);
}
if (res.empty() && format != QueryResultFormat::Json)
{
res_stream << fmt::format("{}", fmt::join(queries, " "))
<< " may not be installed. Try specifying a channel with '-c,--channel' option\n";
}
return res_stream.str();
};

py::class_<Query>(m, "Query")
.def_static(
"find",
[queries_find](MPool& pool, const std::string& query, QueryResultFormat format) -> std::string
{ return queries_find(pool, { query }, format); }
)
.def_static("find", queries_find)
.def_static(
"whoneeds",
[](MPool& pool, const std::string& query, QueryResultFormat format) -> std::string
{
// QueryResult res = q.whoneeds(query, tree);
std::stringstream res_stream;
QueryResult res = Query::whoneeds(pool, query, (format == QueryResultFormat::Tree));
switch (format)
{
case QueryResultFormat::Tree:
case QueryResultFormat::Pretty:
res.tree(res_stream, mambapy::singletons.context().graphics_params);
break;
case QueryResultFormat::Json:
res_stream << res.json().dump(4);
break;
case QueryResultFormat::Table:
case QueryResultFormat::RecursiveTable:
res.table(
res_stream,
{ "Name",
"Version",
"Build",
printers::alignmentMarker(printers::alignment::left),
printers::alignmentMarker(printers::alignment::right),
util::concat("Depends:", query),
"Channel",
"Subdir" }
);
}
if (res.empty() && format != QueryResultFormat::Json)
{
res_stream << query
<< " may not be installed. Try giving a channel with '-c,--channel' option for remote repoquery\n";
}
return res_stream.str();
}
)
.def_static(
"depends",
[](MPool& pool, const std::string& query, QueryResultFormat format) -> std::string
py::class_<QueryResult>(m, "QueryResult")
.def_property_readonly("type", &QueryResult::type)
.def_property_readonly("query", &QueryResult::query)
.def("sort", &QueryResult::sort, py::return_value_policy::reference)
.def("groupby", &QueryResult::groupby, py::return_value_policy::reference)
.def("reset", &QueryResult::reset, py::return_value_policy::reference)
.def("table", &QueryResult::table_to_str)
.def("tree", &QueryResult::tree_to_str)
.def("pretty", &QueryResult::pretty_to_str)
.def("json", [](const QueryResult& query) { return query.json().dump(); })
.def(
"to_dict",
[](const QueryResult& query)
{
QueryResult res = Query::depends(
pool,
query,
(format == QueryResultFormat::Tree || format == QueryResultFormat::RecursiveTable)
);
std::stringstream res_stream;
switch (format)
{
case QueryResultFormat::Tree:
case QueryResultFormat::Pretty:
res.tree(res_stream, mambapy::singletons.context().graphics_params);
break;
case QueryResultFormat::Json:
res_stream << res.json().dump(4);
break;
case QueryResultFormat::Table:
case QueryResultFormat::RecursiveTable:
// res.table(res_stream, {"Name", "Version", "Build", concat("Depends:",
// query), "Channel"});
res.table(res_stream);
}
if (res.empty() && format != QueryResultFormat::Json)
{
res_stream << query
<< " may not be installed. Try giving a channel with '-c,--channel' option for remote repoquery\n";
}
return res_stream.str();
auto json_module = pybind11::module_::import("json");
return json_module.attr("loads")(query.json().dump());
}
);

py::class_<Query>(m, "Query")
.def_static("find", &Query::find)
.def_static("whoneeds", &Query::whoneeds)
.def_static("depends", &Query::depends);

py::class_<SubdirData>(m, "SubdirData")
.def(
"create_repo",
Expand Down

0 comments on commit 733eea8

Please sign in to comment.