forked from foxox/nzconfigtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera_uploader.py
More file actions
419 lines (336 loc) · 15.9 KB
/
camera_uploader.py
File metadata and controls
419 lines (336 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
"""
Nikon Z Camera Menu Upload Tool
Automatically uploads menu settings files to camera memory card
Supports multiple camera connection methods and safety checks
"""
import sys
import os
import shutil
import time
import argparse
from pathlib import Path
import platform
class CameraUploader:
def __init__(self):
self.system = platform.system().lower()
self.camera_paths = []
self.safety_checks = True
def find_camera_drives(self):
"""Find connected camera memory cards"""
self.camera_paths = []
if self.system == "windows":
self._find_windows_drives()
elif self.system == "darwin": # macOS
self._find_macos_drives()
elif self.system == "linux":
self._find_linux_drives()
return self.camera_paths
def _find_windows_drives(self):
"""Find camera drives on Windows"""
# Use alternative method that doesn't require win32api
import string
for letter in string.ascii_uppercase:
drive = f"{letter}:\\"
if os.path.exists(drive):
if self._is_camera_drive(drive):
self.camera_paths.append(drive)
def _find_macos_drives(self):
"""Find camera drives on macOS"""
volumes_path = Path("/Volumes")
if volumes_path.exists():
for volume in volumes_path.iterdir():
if volume.is_dir() and self._is_camera_drive(str(volume)):
self.camera_paths.append(str(volume))
def _find_linux_drives(self):
"""Find camera drives on Linux"""
# Check common mount points
mount_points = ["/media", "/mnt", "/run/media"]
for mount_base in mount_points:
mount_path = Path(mount_base)
if mount_path.exists():
# Check user-specific mounts (like /media/username/)
for user_dir in mount_path.iterdir():
if user_dir.is_dir():
for volume in user_dir.iterdir():
if volume.is_dir() and self._is_camera_drive(str(volume)):
self.camera_paths.append(str(volume))
# Also check direct mounts
for volume in mount_path.iterdir():
if volume.is_dir() and self._is_camera_drive(str(volume)):
self.camera_paths.append(str(volume))
def _is_camera_drive(self, path):
"""Check if a drive/path is a camera memory card"""
try:
drive_path = Path(path)
# Check for camera-specific indicators
indicators = [
"DCIM", # Digital Camera Images folder
"MISC", # Misc folder often present on camera cards
"PRIVATE", # Private folder on some cameras
"NIKON", # Nikon-specific folder
]
for indicator in indicators:
if (drive_path / indicator).exists():
return True
# Check for existing camera menu files (different cameras use different names)
menu_patterns = ["NCSET*.BIN"]
for pattern in menu_patterns:
if list(drive_path.glob(pattern)):
return True
# Check for typical camera file patterns
dcim_path = drive_path / "DCIM"
if dcim_path.exists():
for folder in dcim_path.iterdir():
if folder.is_dir() and any(folder.glob("DSC_*.JPG")):
return True
return False
except (OSError, PermissionError):
return False
def _find_menu_file(self, drive_path):
"""Find the menu file on a camera drive (different cameras use different names)"""
drive_path = Path(drive_path)
# Common menu file patterns for different Nikon Z cameras
menu_patterns = [
"NCSET016.BIN", # Z5 Mark 2
"NCSET006.BIN", # Z5 Mark 1
"NCSET*.BIN", # Any NCSET file
]
for pattern in menu_patterns:
if "*" in pattern:
# Use glob for wildcard patterns
menu_files = list(drive_path.glob(pattern))
if menu_files:
return menu_files[0] # Return first match
else:
# Direct file check
menu_file = drive_path / pattern
if menu_file.exists():
return menu_file
return None
def upload_menu_file(self, source_file, target_drive=None, backup_existing=True):
"""Upload menu file to camera"""
source_path = Path(source_file)
if not source_path.exists():
raise FileNotFoundError(f"Source file not found: {source_file}")
# Find camera if not specified
if target_drive is None:
cameras = self.find_camera_drives()
if not cameras:
raise RuntimeError("No camera memory card detected. Please ensure camera is connected and card is accessible.")
if len(cameras) > 1:
print("Multiple camera drives detected:")
for i, drive in enumerate(cameras, 1):
print(f" {i}. {drive}")
while True:
try:
choice = int(input("Select drive (1-{}): ".format(len(cameras))))
if 1 <= choice <= len(cameras):
target_drive = cameras[choice - 1]
break
else:
print("Invalid choice. Please try again.")
except ValueError:
print("Please enter a number.")
else:
target_drive = cameras[0]
target_path = Path(target_drive)
# Find existing menu file to determine correct filename
existing_menu_file = self._find_menu_file(target_path)
if existing_menu_file:
target_file = existing_menu_file
print(f"Found existing menu file: {existing_menu_file.name}")
else:
# Default to most common filename if no existing file found
target_file = target_path / "NCSET006.BIN"
print("No existing menu file found, using default: NCSET006.BIN")
print(f"Target camera drive: {target_drive}")
print(f"Source file: {source_file}")
print(f"Target file: {target_file}")
# Safety checks
if self.safety_checks:
self._perform_safety_checks(source_path, target_path)
# Backup existing file if requested
if backup_existing and target_file.exists():
backup_name = f"{target_file.stem}_backup_{int(time.time())}{target_file.suffix}"
backup_file = target_path / backup_name
print(f"Backing up existing menu file to: {backup_file}")
shutil.copy2(target_file, backup_file)
# Upload file
print("Uploading menu file...")
try:
shutil.copy2(source_path, target_file)
print("✓ Upload successful!")
# Verify upload
if self._verify_upload(source_path, target_file):
print("✓ Upload verified - files match")
else:
print("⚠ Warning: Uploaded file does not match source")
except Exception as e:
raise RuntimeError(f"Upload failed: {str(e)}")
def _perform_safety_checks(self, source_path, target_path):
"""Perform safety checks before upload"""
print("Performing safety checks...")
# Check if target is writable
if not os.access(target_path, os.W_OK):
raise PermissionError(f"Cannot write to camera drive: {target_path}")
# Check source file size (should be reasonable for a menu file)
source_size = source_path.stat().st_size
if source_size < 1000 or source_size > 1000000: # 1KB to 1MB range
response = input(f"Warning: Source file size ({source_size} bytes) seems unusual for a menu file. Continue? (y/N): ")
if response.lower() != 'y':
raise RuntimeError("Upload cancelled by user")
# Check file format by reading header
try:
with open(source_path, 'rb') as f:
header = f.read(32)
if not header.startswith(b'NIKON'):
response = input("Warning: File does not appear to be a Nikon camera menu file. Continue? (y/N): ")
if response.lower() != 'y':
raise RuntimeError("Upload cancelled by user")
except Exception:
print("Warning: Could not verify file format")
print("✓ Safety checks passed")
def _verify_upload(self, source_path, target_path):
"""Verify that upload was successful"""
try:
# Compare file sizes
source_size = source_path.stat().st_size
target_size = target_path.stat().st_size
if source_size != target_size:
return False
# Compare file content (first and last 1KB)
with open(source_path, 'rb') as src, open(target_path, 'rb') as tgt:
# Check first 1KB
src_start = src.read(1024)
tgt_start = tgt.read(1024)
if src_start != tgt_start:
return False
# Check last 1KB
if source_size > 1024:
src.seek(-1024, 2)
tgt.seek(-1024, 2)
src_end = src.read(1024)
tgt_end = tgt.read(1024)
if src_end != tgt_end:
return False
return True
except Exception:
return False
def list_camera_drives(self):
"""List all detected camera drives"""
cameras = self.find_camera_drives()
if not cameras:
print("No camera memory cards detected.")
return
print("Detected camera drives:")
for i, drive in enumerate(cameras, 1):
drive_path = Path(drive)
# Get drive info
try:
total, used, free = shutil.disk_usage(drive)
total_gb = total / (1024**3)
free_gb = free / (1024**3)
# Check for existing menu file
menu_file = self._find_menu_file(drive_path)
if menu_file:
menu_status = f"✓ Has menu file ({menu_file.name})"
else:
menu_status = "✗ No menu file"
print(f" {i}. {drive}")
print(f" Size: {total_gb:.1f} GB (Free: {free_gb:.1f} GB)")
print(f" Status: {menu_status}")
# Show some camera indicators
indicators = []
if (drive_path / "DCIM").exists():
dcim_count = len(list((drive_path / "DCIM").glob("*/*.JPG")))
if dcim_count > 0:
indicators.append(f"{dcim_count} photos")
if indicators:
print(f" Content: {', '.join(indicators)}")
except OSError:
print(f" {i}. {drive} (Cannot access drive info)")
print()
def download_menu_file(self, source_drive=None, target_file=None):
"""Download menu file from camera"""
# Find camera if not specified
if source_drive is None:
cameras = self.find_camera_drives()
if not cameras:
raise RuntimeError("No camera memory card detected.")
if len(cameras) > 1:
print("Multiple camera drives detected:")
for i, drive in enumerate(cameras, 1):
print(f" {i}. {drive}")
while True:
try:
choice = int(input("Select drive (1-{}): ".format(len(cameras))))
if 1 <= choice <= len(cameras):
source_drive = cameras[choice - 1]
break
else:
print("Invalid choice. Please try again.")
except ValueError:
print("Please enter a number.")
else:
source_drive = cameras[0]
source_path_base = Path(source_drive)
source_path = self._find_menu_file(source_path_base)
if not source_path:
raise FileNotFoundError(f"No menu file found on camera: {source_drive}")
print(f"Found menu file: {source_path.name}")
# Generate target filename if not specified
if target_file is None:
timestamp = time.strftime("%Y%m%d_%H%M%S")
target_file = f"{source_path.stem}_downloaded_{timestamp}{source_path.suffix}"
target_path = Path(target_file)
print(f"Downloading menu file from: {source_path}")
print(f"Saving to: {target_path}")
try:
shutil.copy2(source_path, target_path)
print("✓ Download successful!")
# Show file info
file_size = target_path.stat().st_size
print(f"File size: {file_size} bytes")
except Exception as e:
raise RuntimeError(f"Download failed: {str(e)}")
def main():
parser = argparse.ArgumentParser(description="Nikon Z Camera Menu Upload Tool")
parser.add_argument("--version", action="version", version="1.0")
subparsers = parser.add_subparsers(dest="command", help="Available commands")
# Upload command
upload_parser = subparsers.add_parser("upload", help="Upload menu file to camera")
upload_parser.add_argument("file", help="Menu file to upload")
upload_parser.add_argument("--drive", help="Target camera drive (auto-detect if not specified)")
upload_parser.add_argument("--no-backup", action="store_true", help="Don't backup existing menu file")
upload_parser.add_argument("--no-safety-checks", action="store_true", help="Skip safety checks")
# Download command
download_parser = subparsers.add_parser("download", help="Download menu file from camera")
download_parser.add_argument("--drive", help="Source camera drive (auto-detect if not specified)")
download_parser.add_argument("--output", help="Output filename (auto-generate if not specified)")
# List command
list_parser = subparsers.add_parser("list", help="List detected camera drives")
args = parser.parse_args()
if not args.command:
parser.print_help()
return
uploader = CameraUploader()
try:
if args.command == "upload":
uploader.safety_checks = not args.no_safety_checks
uploader.upload_menu_file(
args.file,
target_drive=args.drive,
backup_existing=not args.no_backup
)
elif args.command == "download":
uploader.download_menu_file(
source_drive=args.drive,
target_file=args.output
)
elif args.command == "list":
uploader.list_camera_drives()
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()