-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
244 lines (194 loc) · 7.12 KB
/
main.py
File metadata and controls
244 lines (194 loc) · 7.12 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
"""
CLI entry point for testing the package tracking MCP server.
Provides command-line interface for testing tracking functionality
without requiring an MCP client.
"""
import asyncio
import argparse
import logging
import sys
from typing import List
from src.config import settings
from src.tracking.fedex_tracker import FedExTracker
from src.tracking.ups_tracker import UPSTracker
from src.models import TrackingCarrier, TrackingResult
# Reason: Configure logging for CLI usage
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
async def test_fedex_tracking(tracking_numbers: List[str]) -> None:
"""
Test FedEx tracking functionality.
Args:
tracking_numbers: List of FedEx tracking numbers to test
"""
print("\\n=== Testing FedEx Tracking ===")
try:
tracker = FedExTracker()
if len(tracking_numbers) == 1:
result = await tracker.track_package(tracking_numbers[0])
print_tracking_result(result)
else:
results = await tracker.track_multiple_packages(tracking_numbers)
for result in results:
print_tracking_result(result)
print("-" * 50)
except Exception as e:
print(f"FedEx tracking test failed: {e}")
async def test_ups_tracking(tracking_numbers: List[str]) -> None:
"""
Test UPS tracking functionality.
Args:
tracking_numbers: List of UPS tracking numbers to test
"""
print("\\n=== Testing UPS Tracking ===")
try:
tracker = UPSTracker()
if len(tracking_numbers) == 1:
result = await tracker.track_package(tracking_numbers[0])
print_tracking_result(result)
else:
results = await tracker.track_multiple_packages(tracking_numbers)
for result in results:
print_tracking_result(result)
print("-" * 50)
except Exception as e:
print(f"UPS tracking test failed: {e}")
def print_tracking_result(result: TrackingResult) -> None:
"""
Print tracking result in a formatted way.
Args:
result: TrackingResult to display
"""
print(f"Tracking Number: {result.tracking_number}")
print(f"Carrier: {result.carrier.value.upper()}")
print(f"Status: {result.status.value}")
if result.error_message:
print(f"Error: {result.error_message}")
return
if result.estimated_delivery:
print(f"Estimated Delivery: {result.estimated_delivery}")
if result.delivery_address:
print(f"Delivery Address: {result.delivery_address}")
if result.service_type:
print(f"Service Type: {result.service_type}")
if result.weight:
print(f"Weight: {result.weight}")
if result.events:
print(f"\\nTracking Events ({len(result.events)} total):")
for i, event in enumerate(result.events[:5]): # Show first 5 events
print(f" {i+1}. {event.timestamp} - {event.description}")
if event.location:
print(f" Location: {event.location}")
if len(result.events) > 5:
print(f" ... and {len(result.events) - 5} more events")
def validate_tracking_numbers(tracking_numbers: List[str], carrier: str) -> bool:
"""
Validate tracking numbers for the specified carrier.
Args:
tracking_numbers: List of tracking numbers to validate
carrier: Carrier name (fedex or ups)
Returns:
bool: True if all numbers are valid
"""
print(f"\\n=== Validating {carrier.upper()} Tracking Numbers ===")
if carrier.lower() == "fedex":
tracker = FedExTracker()
elif carrier.lower() == "ups":
tracker = UPSTracker()
else:
print(f"Unsupported carrier: {carrier}")
return False
all_valid = True
for tracking_number in tracking_numbers:
is_valid = tracker.validate_tracking_number(tracking_number)
status = "✓ Valid" if is_valid else "✗ Invalid"
print(f" {tracking_number}: {status}")
if not is_valid:
all_valid = False
return all_valid
async def main():
"""Main CLI entry point."""
parser = argparse.ArgumentParser(
description="Test package tracking functionality",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python main.py --fedex 123456789012
python main.py --ups 1Z12345E0123456789
python main.py --fedex 123456789012 987654321098
python main.py --validate --fedex 123456789012
python main.py --test-mode
"""
)
parser.add_argument(
"--fedex",
nargs="+",
help="FedEx tracking number(s) to track"
)
parser.add_argument(
"--ups",
nargs="+",
help="UPS tracking number(s) to track"
)
parser.add_argument(
"--validate",
action="store_true",
help="Only validate tracking numbers without tracking"
)
parser.add_argument(
"--test-mode",
action="store_true",
help="Run in test mode with sample tracking numbers"
)
parser.add_argument(
"--server",
action="store_true",
help="Start MCP server"
)
args = parser.parse_args()
# Reason: Start MCP server if requested
if args.server:
from src.server import main as server_main
server_main()
return
# Reason: Show configuration info
print("=== Package Tracking CLI ===")
print(f"FedEx Sandbox: {settings.fedex_sandbox}")
print(f"UPS Sandbox: {settings.ups_sandbox}")
print(f"Log Level: {settings.log_level}")
try:
# Reason: Validate credentials
settings.validate_api_credentials()
print("✓ API credentials configured")
except ValueError as e:
print(f"⚠ Warning: {e}")
print("Some features may not work without proper credentials")
# Reason: Test mode with sample tracking numbers
if args.test_mode:
print("\\n=== Running in Test Mode ===")
# Note: These are example tracking numbers for testing format validation
sample_fedex = ["123456789012", "12345678901234"]
sample_ups = ["1Z12345E0123456789", "1Z12345E1234567890"]
validate_tracking_numbers(sample_fedex, "fedex")
validate_tracking_numbers(sample_ups, "ups")
print("\\nNote: Use real tracking numbers with --fedex or --ups for actual tracking")
return
# Reason: Validate tracking numbers if requested
if args.validate:
if args.fedex:
validate_tracking_numbers(args.fedex, "fedex")
if args.ups:
validate_tracking_numbers(args.ups, "ups")
return
# Reason: Track packages
if args.fedex:
await test_fedex_tracking(args.fedex)
if args.ups:
await test_ups_tracking(args.ups)
if not args.fedex and not args.ups and not args.test_mode:
parser.print_help()
if __name__ == "__main__":
asyncio.run(main())