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

Expose env var handling to python #856

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

PhilipDeegan
Copy link
Member

@PhilipDeegan PhilipDeegan commented Jun 21, 2024

For #831

To allow python to know about env vars from C++

suggestive as is

> python3 -c "from pyphare import cpp; cpp.print_env_vars_info()"
PHARE_SCOPE_TIMING: Enable function scope timing
Options:
  1: ON
  0: OFF

PHARE_LOG: Write logs to $CWD/.log
Options:
  RANK_FILES: Write logs $CWD/.log, a file per rank
  DATETIME_FILES: Write logs $CWD/.log, filename per rank and datetime
  NONE: print normally to std::cout

Summary by CodeRabbit

  • New Features

    • Introduced a centralized environment variable handling system in the app.
    • Added new functions for converting strings to specific types and retrieving environment variables.
    • Implemented new MPI lifecycle management features.
  • Enhancements

    • Improved logging functionality and environmental variable handling in various components.
    • Integrated environment variables into the simulator and logging systems.
  • Testing

    • Added new tests for the Env class to verify logging behavior under different configurations.
  • Documentation

    • Updated Python bindings to include environment variable functions and info printing.

Copy link

coderabbitai bot commented Jun 21, 2024

Walkthrough

Walkthrough

The changes introduce extensive enhancements to environment variable management and testing within the PHARE namespace. This includes new structures and functions for handling environment variables, MPI lifecycle management, and type conversion utilities. Additionally, various files integrate the newly added environment handling capabilities, and new tests verify these functionalities.

Changes

File(s) Change Summary
res/cmake/test.cmake Added a new subdirectory env under tests/core/utilities in the CMake configuration for testing.
src/core/env.hpp Added Var struct, Env class with methods for handling environment variables, and several environment variables.
src/core/utilities/mpi_utils.cpp, src/core/utilities/mpi_utils.hpp Added bool any(bool b) function and Lifecycle struct for MPI initialization and finalization.
src/core/utilities/types.hpp Introduced template functions from_string and get_env_as for type conversion and environment variable retrieval.
src/hdf5/detail/h5/h5_file.hpp Condensed file() function declaration and streamlined write_attribute function.
src/phare/phare.hpp Integrated Env class for scope timing and modified SamraiLifeCycle logic.
src/python3/cpp_etc.cpp Added functions for environment variable interaction (phare_env_*) and binding to Python.
src/simulator/simulator.hpp Changed environment variable handling for logging initialization and functionality.
tests/core/utilities/env/CMakeLists.txt, tests/core/utilities/env/test_env.cpp Added CMake project and tests for phare-env module, focusing on logging functionality in Env class.
pyphare/pyphare/cpp/__init__.py Added functions env_vars and print_env_vars_info for handling and printing environment variable information.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Simulator
    participant Env
    participant Var

    User->>Simulator: Start simulation
    Simulator->>Env: Get PHARE_SCOPE_TIMING
    Env->>Var: Retrieve value
    Var-->>Env: Return timing setting
    Env-->>Simulator: Return scope timing value
    Simulator-->>User: Run simulation with scope timing
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 6

Outside diff range and nitpick comments (1)
src/simulator/simulator.hpp (1)

Line range hint 114-165: Refactor logging setup to ensure resource safety and exception safety.

The current implementation of the logging setup in the Simulator class might lead to resource leaks or undefined behavior in case of exceptions. Consider using RAII techniques to manage resources more safely.

+ struct LogGuard {
+     std::ofstream& log_out;
+     std::streambuf* original_buf;
+     LogGuard(std::ofstream& log) : log_out(log), original_buf(std::cout.rdbuf(log.rdbuf())) {}
+     ~LogGuard() { std::cout.rdbuf(original_buf); }
+ };
- if (auto log = Env::INSTANCE().PHARE_LOG(); log and log != "NONE") {
+ if (auto log = Env::INSTANCE().PHARE_LOG(); log and log != "NONE") {
+     LogGuard log_guard(log_out);
+ }
Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between fc41581 and 4ea8f28.

Files selected for processing (9)
  • res/cmake/test.cmake (1 hunks)
  • src/core/env.hpp (1 hunks)
  • src/core/utilities/mpi_utils.cpp (1 hunks)
  • src/core/utilities/mpi_utils.hpp (1 hunks)
  • src/core/utilities/types.hpp (2 hunks)
  • src/python3/cpp_etc.cpp (2 hunks)
  • src/simulator/simulator.hpp (3 hunks)
  • tests/core/utilities/env/CMakeLists.txt (1 hunks)
  • tests/core/utilities/env/test_env.cpp (1 hunks)
Files skipped from review due to trivial changes (1)
  • res/cmake/test.cmake
Additional context used
Path-based instructions (4)
src/core/env.hpp (1)

Pattern **/*.hpp: Review the C++ code, point out issues relative to principles of clean code, expressiveness, and performance.

src/core/utilities/mpi_utils.hpp (1)

Pattern **/*.hpp: Review the C++ code, point out issues relative to principles of clean code, expressiveness, and performance.

src/core/utilities/types.hpp (1)

Pattern **/*.hpp: Review the C++ code, point out issues relative to principles of clean code, expressiveness, and performance.

src/simulator/simulator.hpp (1)

Pattern **/*.hpp: Review the C++ code, point out issues relative to principles of clean code, expressiveness, and performance.

Additional comments not posted (9)
tests/core/utilities/env/CMakeLists.txt (3)

3-3: Proper project naming.

The project name test-phare-env is clear and follows conventional naming practices for test projects. Good job on this clarity.


1-1: Ensure CMake version compatibility.

The specified CMake minimum version is 3.20.1. Verify that all CMake features used in this file are compatible with this version to prevent potential issues on systems with older CMake versions.

Verification successful

To verify the CMake version compatibility, we need to list all CMake commands used in the CMakeLists.txt file and check if any of them require a version higher than 3.20.1.


The commands listed in the CMakeLists.txt file are all compatible with CMake version 3.20.1. Here are the details:

  • cmake_minimum_required: Introduced before 3.20.1.
  • project: Introduced before 3.20.1.
  • set: Introduced before 3.20.1.
  • add_executable: Introduced before 3.20.1.
  • add_phare_test: Custom command, not a standard CMake command.
  • target_include_directories: Introduced before 3.20.1.
  • target_link_directories: Introduced before 3.20.1.
  • target_link_libraries: Introduced before 3.20.1.

All the commands used are compatible with CMake version 3.20.1 or earlier. Therefore, the specified CMake version is appropriate.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Check for usage of CMake features introduced after version 3.20.1.
ast-grep --lang cmake --pattern $'$_ > 3.20.1' ./tests/core/utilities/env/CMakeLists.txt

Length of output: 200



Script:

#!/bin/bash
# Description: List all unique CMake commands used in the CMakeLists.txt file.
grep -oP '^\s*[^#]*\b(\w+)\b' ./tests/core/utilities/env/CMakeLists.txt | sort -u

Length of output: 496


7-7: Check consistency in target configurations.

Ensure that the executable name, link directories, and test setup are consistently using the ${PROJECT_NAME} variable. This maintains consistency and reduces hardcoding errors.

Also applies to: 13-13, 21-21

Verification successful

The corrected script successfully identified the use of ${PROJECT_NAME} in target configurations within the CMakeLists.txt file. The following instances were found:

  • target_include_directories(${PROJECT_NAME} PRIVATE
  • target_link_directories(${PROJECT_NAME} PUBLIC ${MPI_LIBRARY_PATH})
  • target_link_libraries(${PROJECT_NAME} PRIVATE

This indicates that ${PROJECT_NAME} is being used consistently in the target configurations.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Ensure consistent use of `${PROJECT_NAME}` in target configurations.
rg --type cmake --multiline $'^target_.*${PROJECT_NAME}.*$' ./tests/core/utilities/env/CMakeLists.txt

Length of output: 225



Script:

#!/bin/bash
# Description: Ensure consistent use of `${PROJECT_NAME}` in target configurations.
rg --type cmake --multiline '^target_.*\$\{PROJECT_NAME\}.*$' ./tests/core/utilities/env/CMakeLists.txt

Length of output: 270

tests/core/utilities/env/test_env.cpp (3)

3-5: Correct inclusion of necessary headers.

The inclusion of core/env.hpp and gtest/gtest.h is essential for the tests, and it's done correctly. Including <cstdlib> for environment manipulation functions (setenv, etc.) is also appropriate.


41-46: Proper initialization and execution of tests.

The main function correctly initializes Google Test and the MPI lifecycle, ensuring proper setup and teardown for MPI-dependent tests. This is crucial for tests that depend on MPI configurations.


10-22: Review the implementation of test cases.

The test cases are well-structured and seem to correctly test the functionality of the environment variable handling. However, ensure that the expectations set in the tests (like EXPECT_EQ(var->result(), ".log/" + std::to_string(core::mpi::rank()) + ".out") in lines 35-36) are valid under all test environments.

Also applies to: 24-37

Verification successful

The search results indicate that var->result() and core::mpi::rank() are implemented in the codebase, and their usage in the test case seems consistent with their definitions. Specifically, var->result() returns a string that includes the rank from core::mpi::rank(), which matches the expectation in the test case.

  • src/core/env.hpp contains the implementation of result() which constructs a string using core::mpi::rank().
  • src/core/env.hpp and other files show the use of core::mpi::rank() to append the rank to strings, consistent with the test expectations.

Therefore, the expectations set in the tests appear to be valid under all test environments.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the validity of expectations in test cases under different environments.
# This script checks if the expected results are valid in all configurations.
rg --type cpp 'EXPECT_EQ' ./tests/core/utilities/env/test_env.cpp

Length of output: 415



Script:

#!/bin/bash
# Description: Gather details about var->result() and core::mpi::rank()
# This script checks the implementation of var->result() and core::mpi::rank()

# Find the definition of var->result()
ast-grep --lang cpp --pattern $'function result' > result_impl.txt

# Find the definition of core::mpi::rank()
ast-grep --lang cpp --pattern $'namespace core { namespace mpi { function rank' > rank_impl.txt

# Output the results
cat result_impl.txt rank_impl.txt

Length of output: 511



Script:

#!/bin/bash
# Description: Gather details about var->result() and core::mpi::rank()
# This script checks the implementation of var->result() and core::mpi::rank()

# Find the definition of var->result()
ast-grep --lang cpp --pattern 'function result()' > result_impl.txt

# Find the definition of core::mpi::rank()
ast-grep --lang cpp --pattern 'namespace core { namespace mpi { function rank()' > rank_impl.txt

# Output the results
cat result_impl.txt rank_impl.txt

Length of output: 515



Script:

#!/bin/bash
# Description: Gather details about var->result() and core::mpi::rank()
# This script checks the implementation of var->result() and core::mpi::rank()

# Search for the definition of var->result()
rg --type cpp 'result()' > result_impl.txt

# Search for the definition of core::mpi::rank()
rg --type cpp 'core::mpi::rank()' > rank_impl.txt

# Output the results
cat result_impl.txt rank_impl.txt

Length of output: 3954

src/core/utilities/mpi_utils.cpp (1)

30-30: Review the implementation of the any function.

The any function uses MPI_Allreduce to compute a global sum, effectively determining if any processes have the boolean value true. This is a correct and efficient use of MPI for this purpose. Ensure that the function is used appropriately throughout the codebase.

src/python3/cpp_etc.cpp (2)

5-5: Correct inclusion of environment handling header.

The inclusion of core/env.hpp is necessary for the new environment variable handling functions. This is correctly done and ensures the Python module can interact with the C++ environment variables.


60-69: Review the new Python bindings for environment variable handling.

The new functions (phare_env_exists, phare_env_val, phare_env_result) are correctly implemented to interact with the C++ environment variable handling system. Ensure that these functions are used correctly in the Python code and handle potential exceptions, especially with the assertions used.

src/core/env.hpp Outdated Show resolved Hide resolved
src/core/utilities/mpi_utils.hpp Outdated Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Outside diff range and nitpick comments (2)
src/hdf5/detail/h5/h5_file.hpp (2)

72-72: Consider adding a more descriptive function comment.

The file() function returns a reference to h5file_, which is used frequently within the class. Adding a comment explaining the purpose and usage of this function could enhance readability and maintainability.


Line range hint 72-149: General suggestion: Improve error handling and documentation.

While the class effectively handles various data operations in an MPI environment, adding more detailed error messages and comprehensive documentation for each method could greatly improve maintainability and ease of use. This is particularly important in a complex environment like MPI where debugging can be challenging.

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 4ea8f28 and 1bce4ce.

Files selected for processing (10)
  • res/cmake/test.cmake (1 hunks)
  • src/core/env.hpp (1 hunks)
  • src/core/utilities/mpi_utils.cpp (1 hunks)
  • src/core/utilities/mpi_utils.hpp (1 hunks)
  • src/core/utilities/types.hpp (2 hunks)
  • src/hdf5/detail/h5/h5_file.hpp (2 hunks)
  • src/python3/cpp_etc.cpp (2 hunks)
  • src/simulator/simulator.hpp (3 hunks)
  • tests/core/utilities/env/CMakeLists.txt (1 hunks)
  • tests/core/utilities/env/test_env.cpp (1 hunks)
Files skipped from review as they are similar to previous changes (9)
  • res/cmake/test.cmake
  • src/core/env.hpp
  • src/core/utilities/mpi_utils.cpp
  • src/core/utilities/mpi_utils.hpp
  • src/core/utilities/types.hpp
  • src/python3/cpp_etc.cpp
  • src/simulator/simulator.hpp
  • tests/core/utilities/env/CMakeLists.txt
  • tests/core/utilities/env/test_env.cpp
Additional context used
Path-based instructions (1)
src/hdf5/detail/h5/h5_file.hpp (1)

Pattern **/*.hpp: Review the C++ code, point out issues relative to principles of clean code, expressiveness, and performance.

@PHAREHUB PHAREHUB deleted a comment from coderabbitai bot Jun 21, 2024
@PHAREHUB PHAREHUB deleted a comment from coderabbitai bot Jun 21, 2024
@PHAREHUB PHAREHUB deleted a comment from coderabbitai bot Jun 21, 2024
@PHAREHUB PHAREHUB deleted a comment from coderabbitai bot Jun 21, 2024
@PHAREHUB PHAREHUB deleted a comment from coderabbitai bot Jun 21, 2024
for k, v in cpp_etc_lib().phare_env_vars().items():
print(f"{k}: {v.desc}")
print("Options:")
for k, v in v.options:

Check notice

Code scanning / CodeQL

Nested loops with same variable Note

Nested for statement uses loop variable 'k' of enclosing
for statement
.
Nested for statement uses loop variable 'v' of enclosing
for statement
.
Copy link
Member Author

Choose a reason for hiding this comment

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

I think it works, because of scoping, but it's not clear

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 1bce4ce and 1e6ec37.

Files selected for processing (12)
  • pyphare/pyphare/cpp/init.py (1 hunks)
  • res/cmake/test.cmake (1 hunks)
  • src/core/env.hpp (1 hunks)
  • src/core/utilities/mpi_utils.cpp (1 hunks)
  • src/core/utilities/mpi_utils.hpp (1 hunks)
  • src/core/utilities/types.hpp (2 hunks)
  • src/hdf5/detail/h5/h5_file.hpp (2 hunks)
  • src/phare/phare.hpp (2 hunks)
  • src/python3/cpp_etc.cpp (2 hunks)
  • src/simulator/simulator.hpp (3 hunks)
  • tests/core/utilities/env/CMakeLists.txt (1 hunks)
  • tests/core/utilities/env/test_env.cpp (1 hunks)
Files skipped from review as they are similar to previous changes (10)
  • res/cmake/test.cmake
  • src/core/env.hpp
  • src/core/utilities/mpi_utils.cpp
  • src/core/utilities/mpi_utils.hpp
  • src/core/utilities/types.hpp
  • src/hdf5/detail/h5/h5_file.hpp
  • src/python3/cpp_etc.cpp
  • src/simulator/simulator.hpp
  • tests/core/utilities/env/CMakeLists.txt
  • tests/core/utilities/env/test_env.cpp
Additional context used
Path-based instructions (1)
src/phare/phare.hpp (1)

Pattern **/*.hpp: Review the C++ code, point out issues relative to principles of clean code, expressiveness, and performance.

Ruff
pyphare/pyphare/cpp/__init__.py

25-25: Local variable err is assigned to but never used (F841)

Remove assignment to unused variable err


47-47: Loop control variable v overrides iterable it iterates (B020)

GitHub Check: CodeQL
pyphare/pyphare/cpp/__init__.py

[notice] 47-47: Nested loops with same variable
Nested for statement uses loop variable 'k' of enclosing for statement.
Nested for statement uses loop variable 'v' of enclosing for statement.

Additional comments not posted (1)
pyphare/pyphare/cpp/__init__.py (1)

39-41: Approved: Function env_vars looks good.

This function correctly fetches and returns the environment variables.

@@ -41,7 +43,7 @@ class SamraiLifeCycle
= std::make_shared<StreamAppender>(StreamAppender{&std::cout});
SAMRAI::tbox::Logger::getInstance()->setWarningAppender(appender);
PHARE_WITH_PHLOP( //
if (auto e = core::get_env("PHARE_SCOPE_TIMING", "false"); e == "1" || e == "true")
if (auto e = Env::INSTANCE().PHARE_SCOPE_TIMING(); e == "1" || e == "true")
Copy link

Choose a reason for hiding this comment

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

Approved with a suggestion for readability improvement.

The logic for checking the environment variable and initializing the scope timer is correct. However, consider simplifying the conditional check for better readability.

-            if (auto e = Env::INSTANCE().PHARE_SCOPE_TIMING(); e == "1" || e == "true")
+            auto envSetting = Env::INSTANCE().PHARE_SCOPE_TIMING();
+            if (envSetting == "1" || envSetting == "true")
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (auto e = Env::INSTANCE().PHARE_SCOPE_TIMING(); e == "1" || e == "true")
auto envSetting = Env::INSTANCE().PHARE_SCOPE_TIMING();
if (envSetting == "1" || envSetting == "true")

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Outside diff range and nitpick comments (1)
pyphare/pyphare/cpp/__init__.py (1)

Line range hint 25-25: Remove unused variable assignment.

The variable err is assigned but never used, which is unnecessary and can be removed to clean up the code.

-    except ImportError as err:
+    except ImportError:
Tools
Ruff

47-47: Loop control variable v overrides iterable it iterates (B020)

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 1e6ec37 and 43f48e1.

Files selected for processing (12)
  • pyphare/pyphare/cpp/init.py (1 hunks)
  • res/cmake/test.cmake (1 hunks)
  • src/core/env.hpp (1 hunks)
  • src/core/utilities/mpi_utils.cpp (1 hunks)
  • src/core/utilities/mpi_utils.hpp (1 hunks)
  • src/core/utilities/types.hpp (2 hunks)
  • src/hdf5/detail/h5/h5_file.hpp (2 hunks)
  • src/phare/phare.hpp (2 hunks)
  • src/python3/cpp_etc.cpp (2 hunks)
  • src/simulator/simulator.hpp (3 hunks)
  • tests/core/utilities/env/CMakeLists.txt (1 hunks)
  • tests/core/utilities/env/test_env.cpp (1 hunks)
Files skipped from review due to trivial changes (1)
  • src/simulator/simulator.hpp
Files skipped from review as they are similar to previous changes (10)
  • res/cmake/test.cmake
  • src/core/env.hpp
  • src/core/utilities/mpi_utils.cpp
  • src/core/utilities/mpi_utils.hpp
  • src/core/utilities/types.hpp
  • src/hdf5/detail/h5/h5_file.hpp
  • src/phare/phare.hpp
  • src/python3/cpp_etc.cpp
  • tests/core/utilities/env/CMakeLists.txt
  • tests/core/utilities/env/test_env.cpp
Additional context used
Ruff
pyphare/pyphare/cpp/__init__.py

25-25: Local variable err is assigned to but never used (F841)

Remove assignment to unused variable err


47-47: Loop control variable v overrides iterable it iterates (B020)

Additional comments not posted (1)
pyphare/pyphare/cpp/__init__.py (1)

39-40: New function to expose environment variables to Python.

The function env_vars() has been added to fetch environment variables from C++ through the cpp_etc_lib(). This is in line with the PR's objective to expose environment variable handling to Python.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Outside diff range and nitpick comments (2)
pyphare/pyphare/cpp/__init__.py (1)

Line range hint 25-25: Remove unused variable err.

The variable err is assigned but never used, which can lead to cleaner code by removing it.

-    except ImportError as err:
+    except ImportError:
Tools
Ruff

47-47: Loop control variable v overrides iterable it iterates (B020)

GitHub Check: CodeQL

[notice] 47-47: Nested loops with same variable
Nested for statement uses loop variable 'k' of enclosing for statement.
Nested for statement uses loop variable 'v' of enclosing for statement.

src/simulator/simulator.hpp (1)

10-10: Ensure appropriate encapsulation of includes.

Consider using forward declarations to reduce compile-time dependencies and improve compilation speed, especially for large projects like this one.

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 43f48e1 and 8421deb.

Files selected for processing (12)
  • pyphare/pyphare/cpp/init.py (1 hunks)
  • res/cmake/test.cmake (1 hunks)
  • src/core/env.hpp (1 hunks)
  • src/core/utilities/mpi_utils.cpp (1 hunks)
  • src/core/utilities/mpi_utils.hpp (1 hunks)
  • src/core/utilities/types.hpp (2 hunks)
  • src/hdf5/detail/h5/h5_file.hpp (2 hunks)
  • src/phare/phare.hpp (2 hunks)
  • src/python3/cpp_etc.cpp (2 hunks)
  • src/simulator/simulator.hpp (3 hunks)
  • tests/core/utilities/env/CMakeLists.txt (1 hunks)
  • tests/core/utilities/env/test_env.cpp (1 hunks)
Files skipped from review due to trivial changes (1)
  • src/hdf5/detail/h5/h5_file.hpp
Files skipped from review as they are similar to previous changes (9)
  • res/cmake/test.cmake
  • src/core/env.hpp
  • src/core/utilities/mpi_utils.cpp
  • src/core/utilities/mpi_utils.hpp
  • src/core/utilities/types.hpp
  • src/phare/phare.hpp
  • src/python3/cpp_etc.cpp
  • tests/core/utilities/env/CMakeLists.txt
  • tests/core/utilities/env/test_env.cpp
Additional context used
Path-based instructions (1)
src/simulator/simulator.hpp (1)

Pattern **/*.hpp: Review the C++ code, point out issues relative to principles of clean code, expressiveness, and performance.

Ruff
pyphare/pyphare/cpp/__init__.py

25-25: Local variable err is assigned to but never used (F841)

Remove assignment to unused variable err


47-47: Loop control variable v overrides iterable it iterates (B020)

GitHub Check: CodeQL
pyphare/pyphare/cpp/__init__.py

[notice] 47-47: Nested loops with same variable
Nested for statement uses loop variable 'k' of enclosing for statement.
Nested for statement uses loop variable 'v' of enclosing for statement.

Additional comments not posted (2)
src/simulator/simulator.hpp (2)

114-114: Review logging initialization.

The use of Env::INSTANCE().PHARE_LOG("PHARE_LOG_FILE") for initializing log_out is a good practice as it centralizes environment variable management. Ensure that the PHARE_LOG_FILE is appropriately managed and documented in the environment configuration.


165-165: Check conditional logging setup.

The conditional setup for logging based on the PHARE_LOG environment variable is a robust design choice. It allows for flexible logging configuration, which can be especially useful in a simulation environment. Ensure that the NONE option is clearly documented and handled in all relevant parts of the application.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Outside diff range and nitpick comments (1)
pyphare/pyphare/cpp/__init__.py (1)

Line range hint 25-25: Remove assignment to unused variable.

The variable err is assigned in the except block but never used, which is unnecessary. You can omit it to clean up the code.

-    except ImportError as err:
+    except ImportError:
Tools
Ruff

47-47: Loop control variable v overrides iterable it iterates (B020)

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 8421deb and 2a42b6b.

Files selected for processing (12)
  • pyphare/pyphare/cpp/init.py (1 hunks)
  • res/cmake/test.cmake (1 hunks)
  • src/core/env.hpp (1 hunks)
  • src/core/utilities/mpi_utils.cpp (1 hunks)
  • src/core/utilities/mpi_utils.hpp (1 hunks)
  • src/core/utilities/types.hpp (2 hunks)
  • src/hdf5/detail/h5/h5_file.hpp (2 hunks)
  • src/phare/phare.hpp (2 hunks)
  • src/python3/cpp_etc.cpp (2 hunks)
  • src/simulator/simulator.hpp (3 hunks)
  • tests/core/utilities/env/CMakeLists.txt (1 hunks)
  • tests/core/utilities/env/test_env.cpp (1 hunks)
Files skipped from review due to trivial changes (1)
  • src/simulator/simulator.hpp
Files skipped from review as they are similar to previous changes (10)
  • res/cmake/test.cmake
  • src/core/env.hpp
  • src/core/utilities/mpi_utils.cpp
  • src/core/utilities/mpi_utils.hpp
  • src/core/utilities/types.hpp
  • src/hdf5/detail/h5/h5_file.hpp
  • src/phare/phare.hpp
  • src/python3/cpp_etc.cpp
  • tests/core/utilities/env/CMakeLists.txt
  • tests/core/utilities/env/test_env.cpp
Additional context used
Ruff
pyphare/pyphare/cpp/__init__.py

25-25: Local variable err is assigned to but never used (F841)

Remove assignment to unused variable err


47-47: Loop control variable v overrides iterable it iterates (B020)

Additional comments not posted (1)
pyphare/pyphare/cpp/__init__.py (1)

39-40: Expose environment variables to Python.

This function provides a Python interface to access environment variables managed by the C++ layer, aligning with the PR's objectives.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Outside diff range and nitpick comments (1)
pyphare/pyphare/cpp/__init__.py (1)

Line range hint 25-25: Enhance error handling by using the error information.

The variable err is caught but not used. It's good practice to use the error information to provide more context in the error handling.

-    except ImportError as err:
+    except ImportError as err:
+        print(f"Failed to import pybindlibs.cpp_dbg: {err}")
+        raise
Tools
Ruff

47-47: Loop control variable v overrides iterable it iterates (B020)

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 2a42b6b and b278edf.

Files selected for processing (12)
  • pyphare/pyphare/cpp/init.py (1 hunks)
  • res/cmake/test.cmake (1 hunks)
  • src/core/env.hpp (1 hunks)
  • src/core/utilities/mpi_utils.cpp (1 hunks)
  • src/core/utilities/mpi_utils.hpp (1 hunks)
  • src/core/utilities/types.hpp (2 hunks)
  • src/hdf5/detail/h5/h5_file.hpp (2 hunks)
  • src/phare/phare.hpp (2 hunks)
  • src/python3/cpp_etc.cpp (2 hunks)
  • src/simulator/simulator.hpp (3 hunks)
  • tests/core/utilities/env/CMakeLists.txt (1 hunks)
  • tests/core/utilities/env/test_env.cpp (1 hunks)
Files skipped from review due to trivial changes (1)
  • src/simulator/simulator.hpp
Files skipped from review as they are similar to previous changes (10)
  • res/cmake/test.cmake
  • src/core/env.hpp
  • src/core/utilities/mpi_utils.cpp
  • src/core/utilities/mpi_utils.hpp
  • src/core/utilities/types.hpp
  • src/hdf5/detail/h5/h5_file.hpp
  • src/phare/phare.hpp
  • src/python3/cpp_etc.cpp
  • tests/core/utilities/env/CMakeLists.txt
  • tests/core/utilities/env/test_env.cpp
Additional context used
Ruff
pyphare/pyphare/cpp/__init__.py

25-25: Local variable err is assigned to but never used (F841)

Remove assignment to unused variable err


47-47: Loop control variable v overrides iterable it iterates (B020)

Additional comments not posted (1)
pyphare/pyphare/cpp/__init__.py (1)

39-40: Function env_vars is correctly implemented.

This function properly delegates to the cpp_etc_lib to fetch environment variables, aligning with the PR's objectives to expose environment variable handling to Python.

Comment on lines +43 to +50
def print_env_vars_info():
for k, v in cpp_etc_lib().phare_env_vars().items():
print(f"{k}: {v.desc}")
print("Options:")
for k, v in v.options:
print(f" {k}: {v}")
print("")

Copy link

Choose a reason for hiding this comment

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

Resolve variable shadowing in nested loops.

The nested loop uses the same variable names k and v for both the outer and inner loops, which can lead to confusion and errors due to variable shadowing. Consider using different variable names for the inner loop.

-        for k, v in v.options:
+        for option_key, option_value in v.options:
+            print(f"  {option_key}: {option_value}")
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def print_env_vars_info():
for k, v in cpp_etc_lib().phare_env_vars().items():
print(f"{k}: {v.desc}")
print("Options:")
for k, v in v.options:
print(f" {k}: {v}")
print("")
def print_env_vars_info():
for k, v in cpp_etc_lib().phare_env_vars().items():
print(f"{k}: {v.desc}")
print("Options:")
for option_key, option_value in v.options:
print(f" {option_key}: {option_value}")
print("")
Tools
Ruff

47-47: Loop control variable v overrides iterable it iterates (B020)

PHARE_LOG_LINE_STR(std::to_string(core::mpi::size()) << " " << path)
if (!core::all(paths, [&](auto const& path) { return path == paths[0]; }))
throw std::runtime_error("Function does not support different paths per mpi core");
if (!core::all(paths, [&](auto const& path) { return path == paths[0]; })) throw std::
Copy link
Member Author

Choose a reason for hiding this comment

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

... throw std::

:\

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant