Skip to content

Commit

Permalink
8 using external file (#10)
Browse files Browse the repository at this point in the history
* refactor(sierra_status): improve code structure and add advanced tests

- Move AT command constants to a new conf.py file
- Enhance error handling and input validation in send_at_command function
- Add type hints and improve docstrings for better code readability
- Implement more robust logging with formatted output
- Create advanced test cases for edge cases and error scenarios
- Refactor existing tests to accommodate new changes
- Update default values and file naming patterns for consistency

* refactor(tests): improve test structure and coverage for USB handle module

- Consolidate redundant test cases for AT commands
- Enhance test coverage for animate_spinner function
- Improve error handling and edge case testing
- Remove commented-out test cases
- Add type hints to mock objects in test functions
- Use context managers for patching in some test cases
- Remove unnecessary empty lines between test classes

* feat(ci): add Black code formatter and enable linting

- Add Black to the pip install command alongside flake8
- Uncomment and update the linting step to use Black for code style checking
- Replace flake8 linting with Black's --check option

This change enhances the CI pipeline by introducing code formatting checks,
ensuring consistent code style across the project.

* style(formatting): apply black code formatter across project

- Apply consistent formatting to setup.py, cli.py, usb_handle.py, and test_usb_handle.py
- Improve code readability by breaking long lines and adjusting indentation
- Remove unnecessary blank lines and add spacing where needed
- Standardize string quotes (use double quotes consistently)
- Adjust import order in some files

This commit improves code consistency and adheres to PEP 8 style guidelines,
making the codebase more maintainable and easier to read.

* feat(usb_handle): enhance status logging and timing information

- Add start and finish timestamps to status output
- Include total script execution time in log
- Import 'log' function from math module (unused in this change)

This commit improves the diagnostic information provided by the
status retrieval process, making it easier to track when operations
started and completed, as well as the overall runtime of the script.

* feat(usb_handle): enhance status logging and timing information

- Add start and finish timestamps to status output
- Include total script execution time in log
- Import 'log' function from math module (unused in this change)

This commit improves the diagnostic information provided by the
status retrieval process, making it easier to track when operations
started and completed, as well as the overall runtime of the script.

* refactor(usb_handle): improve logging and documentation

- Remove timestamp from initial result string in get_module_status()
- Add detailed docstring for creat_status_file() function
- Enhance start_process() function with improved documentation and logging
- Move start time logging from result string to logging.info() call

This commit improves code readability and maintainability by enhancing
documentation and refining the logging approach.

* refactor(usb_handle): simplify timestamp formatting in creat_status_file function

- Replace separate time.strftime() call with reuse of existing time_stamp variable
- Improve code readability and reduce redundancy in result string formatting
- Ensure consistent timestamp format throughout the status file creation process

* update linting issue

* refactor(usb_handle): improve timestamp handling and code organization

- Move timestamp generation to start_process function
- Add timestamp to result string before creating status file
- Remove unnecessary blank lines
- Update test case to reflect new timestamp handling
  • Loading branch information
elkanamol authored Sep 8, 2024
1 parent 276718e commit 1accdc5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
28 changes: 26 additions & 2 deletions sierra_status/src/usb_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ def send_at_command(
if "OK\r\n" in result or "ERROR\r\n" in result:
break
animate_spinner()

except serial.SerialException as e:
logging.error(f"Serial communication error: {e}")
except ValueError as e:
Expand Down Expand Up @@ -125,6 +124,13 @@ def get_em_cops(port: str, baudrate: int = DEFAULT_BAUDRATE) -> str:
def creat_status_file(result: str, model: str) -> None:
"""
Creates a status file with the provided result.
Args:
result (str): The status information to be written to the file.
model (str): The model of the module.
Returns:
None
"""
try:
time_stamp = time.strftime("%Y%m%d_%H%M%S", time.localtime())
Expand All @@ -141,15 +147,33 @@ def start_process(
) -> None:
"""
Main function to retrieve the status of an EM9xxx module using AT commands.
Args:
port (str): The serial port to use.
model (str): The model of the module.
log_level (int): The logging level to use.
search (int): The search parameter to use.
baudrate (int, optional): The baud rate to use for the serial connection. Defaults to the DEFAULT_BAUDRATE.
Returns:
None
"""
start_time = time.time()
logging.basicConfig(
level=log_level, format="%(asctime)s - %(levelname)s - %(message)s"
)
logging.info(
f"Starting process for port {port} with model {model} and baudrate {baudrate}"
f"""Start time: {time.strftime('%Y-%m-%d_%H:%M:%S', time.localtime())}
Starting process for port {port}
with model {model} and baudrate {baudrate}"""
)
result = get_module_status(port, search, model, baudrate)
if result:
time_stamp = time.strftime("%Y%m%d_%H%M%S", time.localtime())
result = f"Finished time: {time_stamp}\n" + result
creat_status_file(result, model)
else:
logging.error("No result received from the module.")
logging.info(
f"Total time for running this script: {time.time() - start_time:.2f} seconds"
)
7 changes: 5 additions & 2 deletions tests/test_usb_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,15 @@ def test_creat_status_file(self, mock_strftime, mock_file) -> None:

@patch("sierra_status.src.usb_handle.get_module_status")
@patch("sierra_status.src.usb_handle.creat_status_file")
@patch("sierra_status.src.usb_handle.time.strftime")
def test_start_process_with_result(
self, mock_creat_status_file, mock_get_module_status
self, mock_strftime, mock_creat_status_file, mock_get_module_status
) -> None:
mock_get_module_status.return_value = "Test Status"
mock_strftime.return_value = "20230101_120000"
start_process(self.mock_port, "TestModel", logging.INFO, 0)
mock_creat_status_file.assert_called_with("Test Status", "TestModel")
expected_result = "Finished time: 20230101_120000\nTest Status"
mock_creat_status_file.assert_called_with(expected_result, "TestModel")

@patch("sierra_status.src.usb_handle.get_module_status")
@patch("sierra_status.src.usb_handle.creat_status_file")
Expand Down

0 comments on commit 1accdc5

Please sign in to comment.