🔍 Code Extractor

class AttachmentItem

Maturity: 44

A data class representing metadata attributes of an attachment item, including its type, name, and size.

File:
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/outlook/mail/attachments/attachment_item.py
Lines:
6 - 28
Complexity:
simple

Purpose

AttachmentItem is a value object that encapsulates the essential attributes of a file or item to be attached in the Office 365 Outlook mail system. It inherits from ClientValue and provides a structured way to represent attachment metadata before the actual attachment is created. The class includes a factory method to create instances from file paths, automatically extracting file information.

Source Code

class AttachmentItem(ClientValue):
    """Represents attributes of an item to be attached."""

    def __init__(self, attachment_type=None, name=None, size=None):
        """
        :param int attachment_type:
        :param str name:
        :param int size:
        """
        super(AttachmentItem, self).__init__()
        self.attachmentType = attachment_type
        self.name = name
        self.size = size

    @staticmethod
    def create_file(path):
        file_name = os.path.basename(path)
        file_size = os.stat(path).st_size
        from office365.outlook.mail.attachments.attachment_type import AttachmentType

        return AttachmentItem(
            attachment_type=AttachmentType.file, name=file_name, size=file_size
        )

Parameters

Name Type Default Kind
bases ClientValue -

Parameter Details

attachment_type: An integer representing the type of attachment (e.g., file, item, reference). Expected to be a value from the AttachmentType enumeration. Can be None if not specified during initialization.

name: A string representing the name of the attachment (typically the filename). Can be None if not specified during initialization.

size: An integer representing the size of the attachment in bytes. Can be None if not specified during initialization.

Return Value

Instantiation returns an AttachmentItem object with the specified attachment_type, name, and size attributes. The create_file static method returns a fully populated AttachmentItem instance with metadata extracted from the provided file path.

Class Interface

Methods

__init__(self, attachment_type=None, name=None, size=None)

Purpose: Initializes an AttachmentItem instance with optional attachment metadata

Parameters:

  • attachment_type: Integer representing the attachment type (default: None)
  • name: String representing the attachment name (default: None)
  • size: Integer representing the attachment size in bytes (default: None)

Returns: None (constructor)

create_file(path) -> AttachmentItem static

Purpose: Factory method that creates an AttachmentItem instance from a file path, automatically extracting file metadata

Parameters:

  • path: String path to the file from which to create the attachment item

Returns: AttachmentItem instance populated with file metadata (name, size, and type set to AttachmentType.file)

Attributes

Name Type Description Scope
attachmentType int or None Stores the type of attachment as an integer value, typically from AttachmentType enumeration instance
name str or None Stores the name of the attachment, typically the filename instance
size int or None Stores the size of the attachment in bytes instance

Dependencies

  • os
  • office365.runtime.client_value
  • office365.outlook.mail.attachments.attachment_type

Required Imports

import os
from office365.runtime.client_value import ClientValue

Conditional/Optional Imports

These imports are only needed under specific conditions:

from office365.outlook.mail.attachments.attachment_type import AttachmentType

Condition: only when using the create_file static method

Required (conditional)

Usage Example

# Manual instantiation
attachment = AttachmentItem(attachment_type=1, name='document.pdf', size=1024000)

# Using the factory method to create from file
attachment = AttachmentItem.create_file('/path/to/document.pdf')

# Accessing attributes
print(attachment.name)  # 'document.pdf'
print(attachment.size)  # file size in bytes
print(attachment.attachmentType)  # AttachmentType.file

Best Practices

  • Use the create_file static method when creating AttachmentItem from existing files to automatically populate metadata
  • Ensure the file path exists and is accessible before calling create_file to avoid FileNotFoundError
  • This is a value object (inherits from ClientValue), so it should be treated as immutable after creation
  • The attachment_type should correspond to values defined in the AttachmentType enumeration for consistency
  • This class only stores metadata; it does not handle the actual file content or upload process
  • Instance attributes can be None if not provided during initialization, so handle potential None values when accessing

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class AttachmentInfo 80.4% similar

    AttachmentInfo is a class that represents the attributes of an attachment, inheriting from ClientValue to provide client-side value representation capabilities.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/todo/attachments/info.py
  • class ItemAttachment 78.2% similar

    A class representing an attachment that contains an Outlook item (contact, event, or message) attached to a user event, message, or post.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/outlook/mail/attachments/item.py
  • class Attachment 76.6% similar

    Represents a file or item (contact, event, or message) attached to an event or message in Office 365, providing methods to download and access attachment content and metadata.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/outlook/mail/attachments/attachment.py
  • class FileAttachment 74.7% similar

    Represents a file attachment (text file, Word document, etc.) associated with an Outlook user event, message, or post, providing access to file metadata and content.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/outlook/mail/attachments/file.py
  • class AttachmentCreationInformation 71.5% similar

    A data class that encapsulates the properties required to create a file attachment in SharePoint, including filename and binary content.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/attachments/creation_information.py
← Back to Browse