# coding: utf-8

"""
    ESnet implementation of the IRI api

     A simple implementation of the IRI facility API using python and the fastApi library.  For more information, see: [https://iri.science/](https://iri.science/)  <img src=\"https://iri.science/images/doe-icon-old.png\" height=50 /> 

    The version of the OpenAPI document: 1.0.0
    Generated by OpenAPI Generator (https://openapi-generator.tech)

    Do not edit the class manually.
"""  # noqa: E501


from __future__ import annotations
import pprint
import re  # noqa: F401
import json

from pydantic import BaseModel, ConfigDict, Field, StrictBool, StrictStr
from typing import Any, ClassVar, Dict, List, Optional
from typing_extensions import Annotated
from esnet_iri.models.container import Container
from esnet_iri.models.job_attributes import JobAttributes
from esnet_iri.models.resource_spec import ResourceSpec
from typing import Optional, Set
from typing_extensions import Self

class JobSpecInput(BaseModel):
    """
    Specification for a job.
    """ # noqa: E501
    executable: Optional[Annotated[str, Field(min_length=1, strict=True)]] = Field(default=None, description="Path to the executable to run. If container is specified, this will be used as the entrypoint to the container.")
    container: Optional[Container] = Field(default=None, description="Container specification for containerized execution")
    arguments: Optional[List[StrictStr]] = Field(default=None, description="Command-line arguments to pass to the executable or container")
    directory: Optional[Annotated[str, Field(min_length=1, strict=True)]] = Field(default=None, description="Working directory for the job")
    name: Optional[Annotated[str, Field(min_length=1, strict=True)]] = Field(default=None, description="Name of the job")
    inherit_environment: Optional[StrictBool] = Field(default=True, description="Whether to inherit the environment variables from the submission environment")
    environment: Optional[Dict[str, StrictStr]] = Field(default=None, description="Environment variables to set for the job. If container is specified, these will be set inside the container.")
    stdin_path: Optional[Annotated[str, Field(min_length=1, strict=True)]] = Field(default=None, description="Path to file to use as standard input")
    stdout_path: Optional[Annotated[str, Field(min_length=1, strict=True)]] = Field(default=None, description="Path to file to write standard output")
    stderr_path: Optional[Annotated[str, Field(min_length=1, strict=True)]] = Field(default=None, description="Path to file to write standard error")
    resources: Optional[ResourceSpec] = Field(default=None, description="Resource requirements for the job")
    attributes: Optional[JobAttributes] = Field(default=None, description="Additional job attributes such as duration, queue, and account")
    pre_launch: Optional[Annotated[str, Field(min_length=1, strict=True)]] = Field(default=None, description="Script or commands to run before launching the job")
    post_launch: Optional[Annotated[str, Field(min_length=1, strict=True)]] = Field(default=None, description="Script or commands to run after the job completes")
    launcher: Optional[Annotated[str, Field(min_length=1, strict=True)]] = Field(default=None, description="Job launcher to use (e.g., 'mpirun', 'srun')")
    __properties: ClassVar[List[str]] = ["executable", "container", "arguments", "directory", "name", "inherit_environment", "environment", "stdin_path", "stdout_path", "stderr_path", "resources", "attributes", "pre_launch", "post_launch", "launcher"]

    model_config = ConfigDict(
        populate_by_name=True,
        validate_assignment=True,
        protected_namespaces=(),
    )


    def to_str(self) -> str:
        """Returns the string representation of the model using alias"""
        return pprint.pformat(self.model_dump(by_alias=True))

    def to_json(self) -> str:
        """Returns the JSON representation of the model using alias"""
        # TODO: pydantic v2: use .model_dump_json(by_alias=True, exclude_unset=True) instead
        return json.dumps(self.to_dict())

    @classmethod
    def from_json(cls, json_str: str) -> Optional[Self]:
        """Create an instance of JobSpecInput from a JSON string"""
        return cls.from_dict(json.loads(json_str))

    def to_dict(self) -> Dict[str, Any]:
        """Return the dictionary representation of the model using alias.

        This has the following differences from calling pydantic's
        `self.model_dump(by_alias=True)`:

        * `None` is only added to the output dict for nullable fields that
          were set at model initialization. Other fields with value `None`
          are ignored.
        """
        excluded_fields: Set[str] = set([
        ])

        _dict = self.model_dump(
            by_alias=True,
            exclude=excluded_fields,
            exclude_none=True,
        )
        # override the default output from pydantic by calling `to_dict()` of container
        if self.container:
            _dict['container'] = self.container.to_dict()
        # override the default output from pydantic by calling `to_dict()` of resources
        if self.resources:
            _dict['resources'] = self.resources.to_dict()
        # override the default output from pydantic by calling `to_dict()` of attributes
        if self.attributes:
            _dict['attributes'] = self.attributes.to_dict()
        return _dict

    @classmethod
    def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]:
        """Create an instance of JobSpecInput from a dict"""
        if obj is None:
            return None

        if not isinstance(obj, dict):
            return cls.model_validate(obj)

        _obj = cls.model_validate({
            "executable": obj.get("executable"),
            "container": Container.from_dict(obj["container"]) if obj.get("container") is not None else None,
            "arguments": obj.get("arguments"),
            "directory": obj.get("directory"),
            "name": obj.get("name"),
            "inherit_environment": obj.get("inherit_environment") if obj.get("inherit_environment") is not None else True,
            "environment": obj.get("environment"),
            "stdin_path": obj.get("stdin_path"),
            "stdout_path": obj.get("stdout_path"),
            "stderr_path": obj.get("stderr_path"),
            "resources": ResourceSpec.from_dict(obj["resources"]) if obj.get("resources") is not None else None,
            "attributes": JobAttributes.from_dict(obj["attributes"]) if obj.get("attributes") is not None else None,
            "pre_launch": obj.get("pre_launch"),
            "post_launch": obj.get("post_launch"),
            "launcher": obj.get("launcher")
        })
        return _obj


