Skip to content

Conversation

@olliewalsh
Copy link
Collaborator

@olliewalsh olliewalsh commented May 28, 2025

Re-implement without relying on ConfigParser which does not support duplicate options.
Extend unit test coverage for this and correct the existing test data.

Summary by Sourcery

Re-implement quadlet file generation to support duplicate options by replacing the ConfigParser-based IniFile with a custom UnitFile, update Quadlet methods to use UnitFile, and extend unit tests to cover new model and chat-template scenarios with proper file-existence simulation.

Bug Fixes:

  • Handle duplicate unit-file options by replacing ConfigParser with a custom implementation that collects multiple values per key.

Enhancements:

  • Refactor Quadlet class and its helper methods to use the new UnitFile API instead of IniFile.
  • Introduce UnitFile.write and internal _write routines to emit sections with repeated keys.

Tests:

  • Extend Args and Input test helpers to simulate model and chat-template file existence via monkeypatched os.path.exists.
  • Add new test data fixtures for 'modelfromstore' and 'modelfromstore_ct' scenarios and correct existing quadlet test outputs.

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented May 28, 2025

Reviewer's Guide

Replace the ConfigParser-based IniFile with a custom UnitFile to support duplicate options, update Quadlet methods to use UnitFile, and enhance tests with new input flags, mocks, and data cases.

Sequence Diagram: Handling Duplicate Options with UnitFile

sequenceDiagram
    participant Q as Quadlet
    participant UF as UnitFile
    participant FS as FileSystem

    Q->>UF: new UnitFile("service.container")
    Q->>UF: add("ExampleSection", "MyKey", "value1")
    Q->>UF: add("ExampleSection", "MyKey", "value2") # Duplicate key added

    Q->>UF: write("/path/to/quadlets")
    activate UF
    UF->>FS: open("/path/to/quadlets/service.container", "w")
    activate FS
    FS-->>UF: file_handle
    deactivate FS

    UF->>UF: _write(file_handle) # Internal call to format output
    UF->>FS: write_line(file_handle, "[ExampleSection]")
    activate FS; deactivate FS
    UF->>FS: write_line(file_handle, "MyKey=value1")
    activate FS; deactivate FS
    UF->>FS: write_line(file_handle, "MyKey=value2") # Duplicate key written
    activate FS; deactivate FS
    UF->>FS: write_line(file_handle, "\n")
    activate FS; deactivate FS

    UF->>FS: close(file_handle)
    activate FS; deactivate FS
    deactivate UF
Loading

Class Diagram: Refactoring IniFile to UnitFile for Quadlet Configuration

classDiagram
    direction LR

    class IniFile {
        <<Replaced>>
        +filename: str
        -config: ConfigParser
        +__init__(filename: str)
        +add(section: str, key: str, value: str) void
        +write(dirpath: str) void
    }

    class UnitFile {
        <<New>>
        +filename: str
        -sections: dict
        +__init__(filename: str)
        +add(section: str, key: str, value: str) void
        +write(dirpath: str) void
        -_write(f: FileHandle) void
    }

    class Quadlet {
        +model: str
        +chat_template: str
        +image: str
        +args: object
        +exec_args: list
        +name: str
        +rag: str
        +rag_name: str
        +__init__(model, chat_template, image, args, exec_args)
        +kube() UnitFile
        +generate() list~UnitFile~
        -_gen_chat_template_volume(quadlet_file: UnitFile) void
        -_gen_env(quadlet_file: UnitFile) void
        -_gen_image(name, image) UnitFile
        -_gen_name(quadlet_file: UnitFile) void
        -_gen_model_volume(quadlet_file: UnitFile) list~UnitFile~
        -_gen_port(quadlet_file: UnitFile) void
        -_gen_rag_volume(quadlet_file: UnitFile) list~UnitFile~
    }

    Quadlet ..> UnitFile : Uses
Loading

File-Level Changes

Change Details Files
Introduce UnitFile to replace IniFile/ConfigParser and support duplicate keys
  • Remove ConfigParser import and IniFile class
  • Implement UnitFile with a sections dict storing list of values per key
  • Add custom _write method to emit sections and duplicate options
ramalama/file.py
Refactor Quadlet to use UnitFile instead of IniFile
  • Change return types and instantiations from IniFile to UnitFile in all generation methods
  • Adjust the container description to use self.name instead of self.model
  • Replace config.write calls in tests with file._write
ramalama/quadlet.py
test/unit/test_quadlet.py
Extend and update unit tests for new behavior and test data
  • Enhance Args and Input constructors to handle MODEL, model_file_exists, and chat_template_file_exists flags
  • Mock os.path.exists via monkeypatch to simulate file presence
  • Add test cases for models and chat templates loaded from store
  • Include new expected output files for modelfromstore and modelfromstore_ct
test/unit/test_quadlet.py
test/unit/data/test_quadlet/modelfromstore/modelfromstore.container
test/unit/data/test_quadlet/modelfromstore_ct/modelfromstore_ct.container

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @olliewalsh - I've reviewed your changes and found some issues that need to be addressed.

Blocking issues:

  • Recursive call to write causes infinite recursion (link)

General comments:

  • Avoid using mutable default arguments (e.g. env=[], exec_args=[], args=Args()) – use None and assign new lists/instances inside the constructor to prevent shared state.
  • Replace print statements in Quadlet methods with a proper logging framework to avoid side-effects and allow configurable log levels.
  • The need to call UnitFile._write in tests suggests the public write API could be refactored to accept file-like objects directly, removing reliance on the private method.
Here's what I looked at during the review
  • 🔴 General issues: 1 blocking issue
  • 🟢 Security: all looks good
  • 🟡 Testing: 1 issue found
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@olliewalsh olliewalsh force-pushed the quadlet_duplicate_options branch 2 times, most recently from 487bcf2 to be2fc17 Compare May 28, 2025 00:44
Re-implement without relying on ConfigParser which does not support duplicate
options.
Extend unit test coverage for this and correct the existing test data.

Signed-off-by: Oliver Walsh <[email protected]>
@olliewalsh olliewalsh force-pushed the quadlet_duplicate_options branch from be2fc17 to 82c45f2 Compare May 28, 2025 01:16
@olliewalsh
Copy link
Collaborator Author

@sourcery-ai review

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @olliewalsh - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟡 Testing: 1 issue found
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Member

@engelmi engelmi left a comment

Choose a reason for hiding this comment

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

LGTM

@rhatdan rhatdan merged commit b9171dc into containers:main May 28, 2025
15 of 16 checks passed
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.

4 participants