# Path: nptdms/writer.py
# class ChannelObject(TdmsObject):
#     """A TDMS object for a channel with data
#     """
# 
#     def __init__(self, group, channel, data, properties=None):
#         """Initialise a new ChannelObject
# 
#         :param group: The name of the group this channel is in.
#         :param channel: The name of this channel.
#         :param data: 1-D Numpy array of data to be written.
#         :param properties: A dictionary mapping property names to
#             their value.
#         """
#         self.group = group
#         self.channel = channel
#         self.data = _to_np_array(data)
#         if self.data.ndim != 1:
#             raise ValueError("Channel data must be a 1d array")
#         self.properties = properties
# 
#     @property
#     def has_data(self):
#         return True
# 
#     @property
#     def data_type(self):
#         try:
#             return numpy_data_types[self.data.dtype]
#         except (AttributeError, KeyError):
#             try:
#                 return _to_tdms_value(self.data[0]).__class__
#             except IndexError:
#                 return Void
# 
#     @property
#     def path(self):
#         """The string representation of this channel's path
#         """
#         return str(ObjectPath(self.group, self.channel))
# 
# class TdmsSegment(object):
#     """A segment of data to be written to a file
#     """
# 
#     def __init__(self, objects, version=4712):
#         """Initialise a new segment of TDMS data
# 
#         :param objects: A list of TdmsObject instances.
#         :param version: The TDMS format version to write, which must be either 4712 (the default) or 4713.
#         """
#         paths = set(obj.path for obj in objects)
#         if len(paths) != len(objects):
#             raise ValueError("Duplicate object paths found")
# 
#         self.objects = objects
#         self._tdms_version = version
# 
#     def write(self, file):
#         metadata = self.metadata()
#         metadata_size = sum(len(val.bytes) for val in metadata)
# 
#         toc = ['kTocMetaData', 'kTocRawData', 'kTocNewObjList']
#         leadin = self.leadin(toc, metadata_size)
# 
#         file.write(b''.join(val.bytes for val in leadin))
#         file.write(b''.join(val.bytes for val in metadata))
#         self._write_data(file)
# 
#     def metadata(self):
#         metadata = []
#         metadata.append(Uint32(len(self.objects)))
#         for obj in self.objects:
#             metadata.append(String(obj.path))
#             metadata.extend(self.raw_data_index(obj))
#             properties = read_properties_dict(obj.properties)
#             num_properties = len(properties)
#             metadata.append(Uint32(num_properties))
#             for prop_name, prop_value in properties.items():
#                 metadata.append(String(prop_name))
#                 metadata.append(Int32(prop_value.enum_value))
#                 metadata.append(prop_value)
#         return metadata
# 
#     def raw_data_index(self, obj):
#         if hasattr(obj, 'data'):
#             data_type = Int32(obj.data_type.enum_value)
#             dimension = Uint32(1)
#             num_values = Uint64(len(obj.data))
# 
#             data_index = [Uint32(20), data_type, dimension, num_values]
#             # For strings, we also need to write the total data size in bytes
#             if obj.data_type == String:
#                 total_size = object_data_size(obj.data_type, obj.data)
#                 data_index.append(Uint64(total_size))
# 
#             return data_index
#         else:
#             return [Bytes(b'\xFF\xFF\xFF\xFF')]
# 
#     def leadin(self, toc, metadata_size):
#         leadin = []
#         leadin.append(Bytes(b'TDSm'))
# 
#         toc_mask = long(0)
#         for toc_flag in toc:
#             toc_mask = toc_mask | toc_properties[toc_flag]
#         leadin.append(Int32(toc_mask))
# 
#         leadin.append(Int32(self._tdms_version))
# 
#         next_segment_offset = metadata_size + self._data_size()
#         raw_data_offset = metadata_size
#         leadin.append(Uint64(next_segment_offset))
#         leadin.append(Uint64(raw_data_offset))
# 
#         return leadin
# 
#     def _data_size(self):
#         data_size = 0
#         for obj in self.objects:
#             if hasattr(obj, 'data'):
#                 data_size += object_data_size(obj.data_type, obj.data)
#         return data_size
# 
#     def _write_data(self, file):
#         for obj in self.objects:
#             if hasattr(obj, 'data'):
#                 write_data(file, obj)
# 
# def read_properties_dict(properties_dict):
#     if properties_dict is None:
#         return {}
# 
#     return OrderedDict(
#         (key, _to_tdms_value(val))
#         for key, val in properties_dict.items())


from datetime import datetime
    from collections import OrderedDict
from nptdms.writer import ChannelObject, TdmsSegment, read_properties_dict
from nptdms.types import *
import pytest
import numpy as np
    obj = StubObject(
        path="object_path",
        has_data=False,
        properties={})

    segment = TdmsSegment([obj])
    metadata = segment.metadata()

    expected_values = [
        Uint32(1),  # Number of objects
        String("object_path"),
        Bytes(b'\xFF\xFF\xFF\xFF'),  # Raw data index
        Uint32(0),  # Number of properties
        ]

    _assert_sequence_equal(metadata, expected_values)


def test_properties_are_converted_to_tdms_types():
    test_time = datetime.utcnow()

    properties = {
        "prop1": Int32(1),
        "prop2": 2,
        "prop3": "foo",
        "prop4": True,
        "prop5": 3.142,
        "prop6": test_time,
    }


# Path: nptdms/writer.py
# class ChannelObject(TdmsObject):
#     """A TDMS object for a channel with data
#     """
# 
#     def __init__(self, group, channel, data, properties=None):
#         """Initialise a new ChannelObject
# 
#         :param group: The name of the group this channel is in.
#         :param channel: The name of this channel.
#         :param data: 1-D Numpy array of data to be written.
#         :param properties: A dictionary mapping property names to
#             their value.
#         """
#         self.group = group
#         self.channel = channel
#         self.data = _to_np_array(data)
#         if self.data.ndim != 1:
#             raise ValueError("Channel data must be a 1d array")
#         self.properties = properties
# 
#     @property
#     def has_data(self):
#         return True
# 
#     @property
#     def data_type(self):
#         try:
#             return numpy_data_types[self.data.dtype]
#         except (AttributeError, KeyError):
#             try:
#                 return _to_tdms_value(self.data[0]).__class__
#             except IndexError:
#                 return Void
# 
#     @property
#     def path(self):
#         """The string representation of this channel's path
#         """
#         return str(ObjectPath(self.group, self.channel))
# 
# class TdmsSegment(object):
#     """A segment of data to be written to a file
#     """
# 
#     def __init__(self, objects, version=4712):
#         """Initialise a new segment of TDMS data
# 
#         :param objects: A list of TdmsObject instances.
#         :param version: The TDMS format version to write, which must be either 4712 (the default) or 4713.
#         """
#         paths = set(obj.path for obj in objects)
#         if len(paths) != len(objects):
#             raise ValueError("Duplicate object paths found")
# 
#         self.objects = objects
#         self._tdms_version = version
# 
#     def write(self, file):
#         metadata = self.metadata()
#         metadata_size = sum(len(val.bytes) for val in metadata)
# 
#         toc = ['kTocMetaData', 'kTocRawData', 'kTocNewObjList']
#         leadin = self.leadin(toc, metadata_size)
# 
#         file.write(b''.join(val.bytes for val in leadin))
#         file.write(b''.join(val.bytes for val in metadata))
#         self._write_data(file)
# 
#     def metadata(self):
#         metadata = []
#         metadata.append(Uint32(len(self.objects)))
#         for obj in self.objects:
#             metadata.append(String(obj.path))
#             metadata.extend(self.raw_data_index(obj))
#             properties = read_properties_dict(obj.properties)
#             num_properties = len(properties)
#             metadata.append(Uint32(num_properties))
#             for prop_name, prop_value in properties.items():
#                 metadata.append(String(prop_name))
#                 metadata.append(Int32(prop_value.enum_value))
#                 metadata.append(prop_value)
#         return metadata
# 
#     def raw_data_index(self, obj):
#         if hasattr(obj, 'data'):
#             data_type = Int32(obj.data_type.enum_value)
#             dimension = Uint32(1)
#             num_values = Uint64(len(obj.data))
# 
#             data_index = [Uint32(20), data_type, dimension, num_values]
#             # For strings, we also need to write the total data size in bytes
#             if obj.data_type == String:
#                 total_size = object_data_size(obj.data_type, obj.data)
#                 data_index.append(Uint64(total_size))
# 
#             return data_index
#         else:
#             return [Bytes(b'\xFF\xFF\xFF\xFF')]
# 
#     def leadin(self, toc, metadata_size):
#         leadin = []
#         leadin.append(Bytes(b'TDSm'))
# 
#         toc_mask = long(0)
#         for toc_flag in toc:
#             toc_mask = toc_mask | toc_properties[toc_flag]
#         leadin.append(Int32(toc_mask))
# 
#         leadin.append(Int32(self._tdms_version))
# 
#         next_segment_offset = metadata_size + self._data_size()
#         raw_data_offset = metadata_size
#         leadin.append(Uint64(next_segment_offset))
#         leadin.append(Uint64(raw_data_offset))
# 
#         return leadin
# 
#     def _data_size(self):
#         data_size = 0
#         for obj in self.objects:
#             if hasattr(obj, 'data'):
#                 data_size += object_data_size(obj.data_type, obj.data)
#         return data_size
# 
#     def _write_data(self, file):
#         for obj in self.objects:
#             if hasattr(obj, 'data'):
#                 write_data(file, obj)
# 
# def read_properties_dict(properties_dict):
#     if properties_dict is None:
#         return {}
# 
#     return OrderedDict(
#         (key, _to_tdms_value(val))
#         for key, val in properties_dict.items())


# Path: nptdms/test/writer/test_tdms_segment.py
from datetime import datetime
    from collections import OrderedDict
from nptdms.writer import ChannelObject, TdmsSegment, read_properties_dict
from nptdms.types import *
import pytest
import numpy as np

    obj = StubObject(
        path="object_path",
        has_data=False,
        properties={})

    segment = TdmsSegment([obj])
    metadata = segment.metadata()

    expected_values = [
        Uint32(1),  # Number of objects
        String("object_path"),
        Bytes(b'\xFF\xFF\xFF\xFF'),  # Raw data index
        Uint32(0),  # Number of properties
        ]

    _assert_sequence_equal(metadata, expected_values)


def test_properties_are_converted_to_tdms_types():
    test_time = datetime.utcnow()

    properties = {
        "prop1": Int32(1),
        "prop2": 2,
        "prop3": "foo",
        "prop4": True,
        "prop5": 3.142,
        "prop6": test_time,
    }


    tdms_properties = read_properties_dict(properties)