Skip to content

Commit

Permalink
STYLE: Clean up PythonPluginTemplateFile file.
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Jackson <[email protected]>
  • Loading branch information
imikejackson committed Feb 7, 2025
1 parent 769f030 commit 6d9223b
Showing 1 changed file with 41 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@ namespace nx::core
inline std::string CamelCaseToSnakeCase(const std::string& input)
{
std::string result;
for(char ch : input)
for(const char letter : input)
{
if(std::isupper(ch))
if(std::isupper(letter) != 0)
{
if(!result.empty())
{
result += '_';
}
result += std::tolower(ch);
result += std::tolower(letter);
}
else
{
result += ch;
result += letter;
}
}
return result;
}

inline const std::string k_FilterIncludeInsertToken = "# FILTER_INCLUDE_INSERT";
inline const std::string k_FilterNameInsertToken = "# FILTER_NAME_INSERT";
// inline const std::string k_FilterIncludeInsertToken = "# FILTER_INCLUDE_INSERT";
// inline const std::string k_FilterNameInsertToken = "# FILTER_NAME_INSERT";

/**
*
Expand Down Expand Up @@ -72,16 +72,16 @@ inline Result<> InsertFilterNameInPluginFiles(const std::filesystem::path& plugi
{
pluginName.pop_back();
}
std::filesystem::path plugPath(pluginName);
pluginName = plugPath.stem().string();
// const std::filesystem::path plugPath(pluginName);
// pluginName = plugPath.stem().string();

fs::path pluginPyPath = pluginPath / "Plugin.py";
const fs::path pluginPyPath = pluginPath / "Plugin.py";
if(!fs::exists(pluginPyPath))
{
return MakeErrorResult(-2000, fmt::format("Non-existent plugin file at path: {}", pluginPyPath.string()));
}

fs::path initPyPath = pluginPath / "__init__.py";
const fs::path initPyPath = pluginPath / "__init__.py";
if(!fs::exists(initPyPath))
{
return MakeErrorResult(-2001, fmt::format("Non-existent plugin file at path: {}", initPyPath.string()));
Expand All @@ -92,20 +92,20 @@ inline Result<> InsertFilterNameInPluginFiles(const std::filesystem::path& plugi
std::ifstream file(initPyPath.string());
std::stringstream buffer;
buffer << file.rdbuf();
std::string content = buffer.str();
const std::string content = buffer.str();
file.close();
std::vector<std::string> lines = nx::core::StringUtilities::split(content, "\n", true);
if(lines.back().empty())
{
lines.pop_back();
}
// Create the output file by opening the same file for OVER WRITE.
// Create the output file by opening the same file for OVERWRITE.
std::ofstream outFile = std::ofstream(initPyPath.string(), std::ios_base::binary | std::ios_base::trunc);

std::string filterMarkerLine = fmt::format("# FILTER_START: {}", filterName);
std::string lastMarkerLine = "def get_plugin():";
const std::string filterMarkerLine = fmt::format("# FILTER_START: {}", filterName);
const std::string lastMarkerLine = "def get_plugin():";

std::string filterImportToken = fmt::format("from {0}.{1} import {1}", pluginName, filterName);
// const std::string filterImportToken = fmt::format("from {0}.{1} import {1}", pluginName, filterName);
bool insertToken = true;
for(auto& line : lines)
{
Expand All @@ -131,21 +131,21 @@ inline Result<> InsertFilterNameInPluginFiles(const std::filesystem::path& plugi
std::ifstream file(pluginPyPath.string());
std::stringstream buffer;
buffer << file.rdbuf();
std::string content = buffer.str();
const std::string content = buffer.str();
file.close();
std::vector<std::string> lines = nx::core::StringUtilities::split(content, "\n", true);
if(lines.back().empty())
{
lines.pop_back();
}
// Create the output file by opening the same file for OVER WRITE.
// Create the output file by opening the same file for OVERWRITE.
std::ofstream outFile = std::ofstream(pluginPyPath.string(), std::ios_base::binary | std::ios_base::trunc);

std::string filterMarkerLine = fmt::format("# FILTER_START: {}", filterName);
std::string lastMarkerLine = "import simplnx as nx";
const std::string filterMarkerLine = fmt::format("# FILTER_START: {}", filterName);
const std::string lastMarkerLine = "import simplnx as nx";

std::string filterImportToken = fmt::format("from {0}.{1} import {1}", pluginName, filterName);
std::string filterInsertToken = fmt::format("'{}'", filterName);
// const std::string filterImportToken = fmt::format("from {0}.{1} import {1}", pluginName, filterName);
// const std::string filterInsertToken = fmt::format("'{}'", filterName);

bool insertToken = true;
for(auto& line : lines)
Expand Down Expand Up @@ -185,13 +185,13 @@ inline Result<> AtomicallyWriteFile(const std::string& content, const std::files
AtomicFile tempFile = std::move(atomicFileResult.value());
{
// Scope this so that the file closes first before we then 'commit' with the atomic file
std::ofstream fout(tempFile.tempFilePath(), std::ios_base::out | std::ios_base::binary);
if(!fout.is_open())
std::ofstream fileOut(tempFile.tempFilePath(), std::ios_base::out | std::ios_base::binary);
if(!fileOut.is_open())
{
return MakeErrorResult(-74100, fmt::format("Error creating and opening output file at path: {}", tempFile.tempFilePath().string()));
}

fout << content;
fileOut << content;
}

return tempFile.commit();
Expand All @@ -213,11 +213,9 @@ inline Result<> WritePythonFilterToPlugin(const std::filesystem::path& outputPat
// Write Python Filter Skeleton code
// **************************************************************************
{
const std::string content = GeneratePythonFilter(filterName, filterName, Uuid::GenerateV4().str());

const std::string content = GeneratePythonFilter(filterName, humanName, Uuid::GenerateV4().str());
const fs::path outputFilePath = outputPath / fmt::format("{}.py", filterName);

result = AtomicallyWriteFile(content, outputFilePath);
result = MergeResults(result, AtomicallyWriteFile(content, outputFilePath));
}

// **************************************************************************
Expand All @@ -244,12 +242,12 @@ inline Result<> WritePythonFilterToPlugin(const std::filesystem::path& outputPat

const fs::path outputFilePath = outputPath / ".." / ".." / "docs" / fmt::format("{}.md", filterName);

result = AtomicallyWriteFile(docOut.str(), outputFilePath);
result = MergeResults(result, AtomicallyWriteFile(docOut.str(), outputFilePath));
}

if(updatePluginFiles)
{
result = InsertFilterNameInPluginFiles(outputPath, filterName);
result = MergeResults(result, InsertFilterNameInPluginFiles(outputPath, filterName));
}
return result;
}
Expand Down Expand Up @@ -300,16 +298,16 @@ inline std::string GeneratePythonPlugin(const std::string& pluginName, const std
auto filterList = StringUtilities::split(pluginFilterList, ',');
content = StringUtilities::replace(content, "#PLUGIN_FILTER_LIST#", fmt::format("{}", fmt::join(filterList, ", ")));

std::stringstream ss;
std::stringstream codeStream;

for(const auto& name : filterList)
{
ss << "# FILTER_START: " << name << "\n"
<< "from ." << name << " import " << name << "\n"
<< "_filters.append(" << name << ")\n"
<< "# FILTER_END: " << name << "\n\n";
codeStream << "# FILTER_START: " << name << "\n"
<< "from ." << name << " import " << name << "\n"
<< "_filters.append(" << name << ")\n"
<< "# FILTER_END: " << name << "\n\n";
}
content = StringUtilities::replace(content, "#PLUGIN_IMPORT_CODE#", ss.str());
content = StringUtilities::replace(content, "#PLUGIN_IMPORT_CODE#", codeStream.str());

return content;
}
Expand Down Expand Up @@ -362,9 +360,7 @@ inline Result<> WritePythonPluginFiles(const std::filesystem::path& outputDirect
return MakeErrorResult(-74100, fmt::format("Error creating and opening output file at path: {}", tempFile.tempFilePath().string()));
}

std::string content = GeneratePythonPlugin(pluginName, pluginShortName, pluginDescription, pluginFilterList);

fout << content;
fout << GeneratePythonPlugin(pluginName, pluginShortName, pluginDescription, pluginFilterList);
}
Result<> commitResult = tempFile.commit();
if(commitResult.invalid())
Expand Down Expand Up @@ -406,15 +402,15 @@ inline Result<> WritePythonPluginFiles(const std::filesystem::path& outputDirect
aList.append("'get_plugin'");
content = StringUtilities::replace(content, "#PLUGIN_FILTER_LIST#", aList);

std::stringstream ss;
std::stringstream codeStream;
for(const auto& name : filterList)
{
ss << "# FILTER_START: " << name << "\n"
<< "from ." << name << " import " << name << "\n"
<< "__all__.append('" << name << "')\n"
<< "# FILTER_END: " << name << "\n\n";
codeStream << "# FILTER_START: " << name << "\n"
<< "from ." << name << " import " << name << "\n"
<< "__all__.append('" << name << "')\n"
<< "# FILTER_END: " << name << "\n\n";
}
content = StringUtilities::replace(content, "#PLUGIN_IMPORT_CODE#", ss.str());
content = StringUtilities::replace(content, "#PLUGIN_IMPORT_CODE#", codeStream.str());

fout << content;
}
Expand Down

0 comments on commit 6d9223b

Please sign in to comment.