GHIElectronics.Endpoint.Devices.Storage 0.5.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,199 @@
1
+ """SD cards and USB drives of Endpoint, with the same API as .NET's
2
+ GHIElectronics.Endpoint.Devices.Storage:
3
+
4
+ from GHIElectronics.Endpoint.Core import EPM815, DeviceConnectionStatus
5
+ from GHIElectronics.Endpoint.Devices.Storage import FileSystem, StorageController
6
+
7
+ usb = StorageController.FromName(EPM815.StorageController.UsbHostMassStorage)
8
+
9
+ def changed(sender, e):
10
+ if e.DeviceStatus == DeviceConnectionStatus.Connected:
11
+ drive = FileSystem.Mount(e.Hdc) # /media/usb0/, usb1, ...
12
+ with open(drive.Name + "log.txt", "w") as f:
13
+ f.write("Hello")
14
+ FileSystem.Flush(e.Hdc) # before the drive is pulled out
15
+
16
+ usb.ConnectionChanged += changed
17
+ usb.Enable() # the drives already plugged in are reported too
18
+
19
+ The events come on the library's storage thread. A drive that is pulled out
20
+ is unmounted by the library. The SD card is /media/sd/; USB drives get the
21
+ first free /media/usbN/, as Windows gives the first free drive letter. A
22
+ controller stays alive until it is disposed.
23
+ """
24
+
25
+ import ctypes
26
+ import pathlib
27
+ from enum import IntEnum
28
+
29
+ from GHIElectronics.Endpoint.Core import DeviceConnectionStatus
30
+ from GHIElectronics.Endpoint.Core._errors import IOException
31
+ from GHIElectronics.Endpoint.Core._event import Event
32
+ from GHIElectronics.Endpoint.Core._native import StorageCallback, native
33
+
34
+ __all__ = ["DeviceConnectionEventArgs", "DeviceConnectionStatus", "DriveInfo", "DriveType", "FileSystem",
35
+ "StorageController"]
36
+
37
+
38
+ class DriveType(IntEnum):
39
+ """The same values as .NET's System.IO.DriveType"""
40
+
41
+ Unknown = 0
42
+ NoRootDirectory = 1
43
+ Removable = 2
44
+ Fixed = 3
45
+ Network = 4
46
+ CDRom = 5
47
+ Ram = 6
48
+
49
+
50
+ class DeviceConnectionEventArgs:
51
+ def __init__(self, deviceStatus, hdc, volumeLabel):
52
+ self.DeviceStatus = deviceStatus
53
+ self.Hdc = hdc
54
+ """The drive, for FileSystem.Mount, Flush and Unmount, until it is pulled out"""
55
+ self.VolumeLabel = volumeLabel
56
+ """The drive's name ("LOGGER"), empty if it has none"""
57
+
58
+ def __repr__(self):
59
+ return f"DeviceConnectionEventArgs({self.DeviceStatus.name}, Hdc={self.Hdc}, VolumeLabel={self.VolumeLabel!r})"
60
+
61
+
62
+ class DriveInfo:
63
+ """A drive's folder and its sizes, as .NET's System.IO.DriveInfo. The drive's
64
+ label is DeviceConnectionEventArgs.VolumeLabel."""
65
+
66
+ def __init__(self, driveName):
67
+ self._name = str(driveName)
68
+
69
+ @property
70
+ def Name(self):
71
+ """The drive's folder, "/media/usb0/" """
72
+ return self._name
73
+
74
+ @property
75
+ def RootDirectory(self):
76
+ return pathlib.Path(self._name)
77
+
78
+ @property
79
+ def IsReady(self):
80
+ return self._get(False)["ready"]
81
+
82
+ @property
83
+ def DriveFormat(self):
84
+ """"vfat", "exfat", "ntfs" or "ext4" """
85
+ return self._get(True)["format"]
86
+
87
+ @property
88
+ def DriveType(self):
89
+ return DriveType(self._get(False)["type"])
90
+
91
+ @property
92
+ def TotalSize(self):
93
+ return self._get(True)["total"]
94
+
95
+ @property
96
+ def TotalFreeSpace(self):
97
+ return self._get(True)["free"]
98
+
99
+ @property
100
+ def AvailableFreeSpace(self):
101
+ return self._get(True)["available"]
102
+
103
+ def _get(self, must_be_ready):
104
+ fmt = ctypes.create_string_buffer(64)
105
+ kind, ready = native.out_int(), native.out_int()
106
+ total, free, available = native.out_int64(), native.out_int64(), native.out_int64()
107
+ native.driveinfo_get(self._name.encode(), fmt, len(fmt), native.ref(kind), native.ref(ready),
108
+ native.ref(total), native.ref(free), native.ref(available))
109
+ if must_be_ready and not ready.value:
110
+ raise IOException(f"The drive {self._name} is not ready")
111
+ return {"format": fmt.value.decode(), "type": kind.value, "ready": bool(ready.value),
112
+ "total": total.value, "free": free.value, "available": available.value}
113
+
114
+ def __fspath__(self):
115
+ return self._name
116
+
117
+ def __repr__(self):
118
+ return f"DriveInfo({self._name!r})"
119
+
120
+
121
+ class StorageController:
122
+ @staticmethod
123
+ def FromName(name):
124
+ """EPM815.StorageController.SdCard or EPM815.StorageController.UsbHostMassStorage"""
125
+ return StorageController(name)
126
+
127
+ def __init__(self, name):
128
+ self._name = name
129
+ self._event = Event()
130
+ self._handle = None
131
+ self._callback = StorageCallback(self._changed) # kept: the library calls it
132
+ handle = native.out_handle()
133
+ native.storage_create(name.encode(), self._callback, None, native.ref(handle))
134
+ self._handle = handle
135
+ native.keep_alive(self)
136
+
137
+ @property
138
+ def Name(self):
139
+ return self._name
140
+
141
+ @property
142
+ def ConnectionChanged(self):
143
+ """A drive connected or disconnected: handler(sender, e). Add with +=, remove with -="""
144
+ return self._event
145
+
146
+ @ConnectionChanged.setter
147
+ def ConnectionChanged(self, value):
148
+ if value is not self._event:
149
+ raise TypeError("Use += and -= to add and remove an event handler")
150
+
151
+ def _h(self):
152
+ if self._handle is None:
153
+ raise native.disposed("StorageController")
154
+ return self._handle
155
+
156
+ def Enable(self):
157
+ """Starts reporting drives: first the ones already plugged in"""
158
+ native.storage_enable(self._h())
159
+
160
+ def Disable(self):
161
+ native.storage_disable(self._h())
162
+
163
+ def Dispose(self):
164
+ """Stops the events (one being delivered finishes first)"""
165
+ if self._handle is not None:
166
+ native.storage_dispose(self._handle)
167
+ self._handle = None
168
+ native.let_go(self)
169
+
170
+ def _changed(self, context, hdc, status, label):
171
+ self._event(self, DeviceConnectionEventArgs(DeviceConnectionStatus(status), hdc,
172
+ label.decode() if label else ""))
173
+
174
+ def __enter__(self):
175
+ return self
176
+
177
+ def __exit__(self, *exception):
178
+ self.Dispose()
179
+
180
+ def __del__(self):
181
+ self.Dispose()
182
+
183
+
184
+ class FileSystem:
185
+ @staticmethod
186
+ def Mount(hdc):
187
+ """Mounts the drive (if it is not yet) and gives its folder, a DriveInfo"""
188
+ name = ctypes.create_string_buffer(256)
189
+ native.storage_mount(hdc, name, len(name))
190
+ return DriveInfo(name.value.decode())
191
+
192
+ @staticmethod
193
+ def Flush(hdc):
194
+ """Writes everything to the drive: call it before the drive is pulled out"""
195
+ native.storage_flush(hdc)
196
+
197
+ @staticmethod
198
+ def Unmount(hdc):
199
+ native.storage_unmount(hdc)
@@ -0,0 +1,20 @@
1
+ Metadata-Version: 2.4
2
+ Name: GHIElectronics.Endpoint.Devices.Storage
3
+ Version: 0.5.3
4
+ Summary: SD cards and USB drives of GHI Electronics Endpoint: insert and remove events, mounting.
5
+ Author-email: "GHI Electronics, LLC" <support@ghielectronics.com>
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://www.ghielectronics.com/
8
+ Project-URL: Repository, https://github.com/ghi-electronics/Endpoint-Libraries
9
+ Project-URL: Issues, https://github.com/ghi-electronics/Endpoint-Libraries/issues
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Operating System :: POSIX :: Linux
12
+ Requires-Python: >=3.9
13
+ Description-Content-Type: text/markdown
14
+ Requires-Dist: GHIElectronics.Endpoint.Core<0.6,>=0.5.3
15
+
16
+ # GHIElectronics.Endpoint.Devices.Storage
17
+
18
+ SD cards and USB drives of GHI Electronics Endpoint: insert and remove events, mounting.
19
+
20
+ Part of the [GHI Electronics Endpoint](https://github.com/ghi-electronics/Endpoint-Libraries) libraries. See the repository README for the full package list and getting-started guide.
@@ -0,0 +1,5 @@
1
+ GHIElectronics/Endpoint/Devices/Storage/__init__.py,sha256=tfyhKDxZyzR5457PxolDH3TYhZghwwp6H-_n6TABIAU,6545
2
+ ghielectronics_endpoint_devices_storage-0.5.3.dist-info/METADATA,sha256=Dm9cpYysy6LnarsQ2PnjfuRu-eu3hhypMt9_1bWUC1c,1039
3
+ ghielectronics_endpoint_devices_storage-0.5.3.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
4
+ ghielectronics_endpoint_devices_storage-0.5.3.dist-info/top_level.txt,sha256=NpOZNgd0iquwrRPrTOhUDxc2V9TtNFLv5Z_Dx3YZtIU,15
5
+ ghielectronics_endpoint_devices_storage-0.5.3.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.2.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+