Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
200 lines (170 loc) · 6.77 KB
/
Copy pathapp.py
File metadata and controls
200 lines (170 loc) · 6.77 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
#!/usr/bin/env python3
"""
Main application entry point for myproject template.
This file demonstrates how to use the myproject package with proper error
handling and logging.
"""
import argparse
import json
import logging
import os
import sys
import time
from typing import Dict, Any, Optional
# Configure logging first so we can log import errors
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
handlers=[logging.StreamHandler(sys.stdout)]
)
logger = logging.getLogger("myproject")
# Try to add the src directory to the Python path if needed
try:
src_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "src")
if os.path.exists(src_path) and src_path not in sys.path:
logger.debug("Adding %s to Python path", src_path)
sys.path.insert(0, src_path)
logger.debug("Python path is now: %s", sys.path)
except (OSError, json.JSONDecodeError) as e:
logger.error("Error adjusting Python path: %s", e)
# Try to import the package
try:
import requests
except ImportError as e:
logger.critical("Failed to import requests: %s", e)
logger.debug("Make sure requests is installed: pip install requests")
sys.exit(1)
# Try to import from the myproject package
try:
from myproject import hello
logger.debug("Successfully imported hello from myproject")
except ImportError as e:
logger.critical(f"Failed to import from myproject: {e}")
logger.debug("This could be because:")
logger.debug("1. The package is not installed (run: pip install -e .)")
logger.debug("2. The src directory is not in the Python path")
logger.debug("3. The package structure has changed")
logger.debug(f"Current Python path: {sys.path}")
# Try a fallback for development
try:
logger.debug("Attempting fallback import...")
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from src.myproject import hello
logger.debug("Fallback import successful")
except ImportError as fallback_error:
logger.critical("Fallback import also failed: %s", fallback_error)
logger.debug("You may need to install the package: pip install -e .")
sys.exit(1)
# Now that logging is set up and imports are handled, we can continue
# Constants
DEFAULT_API_URL = "https://httpbin.org/get"
CONFIG_FILE = "config.json"
def load_config(config_path: str = CONFIG_FILE) -> Dict[str, Any]:
"""
Load configuration from a JSON file.
Args:
config_path: Path to the configuration file
Returns:
Dictionary containing configuration values
"""
try:
logger.debug("Attempting to load config from %s", config_path)
if os.path.exists(config_path):
with open(config_path, "r", encoding="utf-8") as f:
logger.info(f"Successfully loaded config from {config_path}")
return json.load(f)
else:
logger.warning(
f"Config file {config_path} not found, using defaults"
)
return {}
except json.JSONDecodeError as e:
logger.error(f"Error parsing config file: {e}")
return {}
except Exception as e:
logger.error(f"Unexpected error loading config: {e}")
return {}
def make_api_request(
url: str, params: Optional[Dict[str, Any]] = None
) -> Dict[str, Any]:
"""
Make an API request with error handling and logging.
Args:
url: The API endpoint URL
params: Optional query parameters
Returns:
API response as a dictionary
"""
start_time = time.time()
logger.info(f"Making API request to {url}")
try:
response = requests.get(url, params=params, timeout=10)
elapsed = time.time() - start_time
logger.debug(f"Request completed in {elapsed:.2f} seconds")
# Log different response codes appropriately
if response.status_code == 200:
logger.info(f"API request successful: {response.status_code}")
elif response.status_code >= 400 and response.status_code < 500:
logger.warning(f"API client error: {response.status_code}")
elif response.status_code >= 500:
logger.error(f"API server error: {response.status_code}")
response.raise_for_status()
return response.json()
except requests.exceptions.ConnectionError as e:
logger.error(f"Connection error: {e}")
logger.debug("Network connectivity issue or invalid URL")
raise
except requests.exceptions.Timeout as e:
logger.error(f"Request timed out: {e}")
logger.debug("API endpoint is taking too long to respond")
raise
except requests.exceptions.RequestException as e:
logger.error(f"Request failed: {e}")
raise
except json.JSONDecodeError as e:
logger.error(f"Error parsing API response: {e}")
logger.debug("API returned invalid JSON")
raise
except Exception as e:
logger.error(f"Unexpected error during API request: {e}")
raise
def main():
"""Main application entry point with command line argument parsing."""
parser = argparse.ArgumentParser(description="myproject CLI application")
parser.add_argument(
"--url", default=DEFAULT_API_URL, help="API URL to query"
)
parser.add_argument(
"--config", default=CONFIG_FILE, help="Path to config file"
)
parser.add_argument(
"--verbose", "-v", action="store_true", help="Enable verbose logging"
)
args = parser.parse_args()
# Set logging level based on verbosity
if args.verbose:
logger.setLevel(logging.DEBUG)
logger.debug("Verbose logging enabled")
try:
# Log package import status
logger.debug("Checking myproject package import")
message = hello()
logger.info(f"Package import successful: {message}")
# Load configuration
config = load_config(args.config)
# Make API request
url = config.get("api_url", args.url)
logger.debug(f"Using API URL: {url}")
response = make_api_request(url)
print(json.dumps(response, indent=2))
logger.info("Application completed successfully")
return 0
except ImportError as e:
logger.critical(f"Package import error: {e}")
logger.debug("Check that the package is installed correctly")
return 1
except Exception as e:
logger.critical(f"Application error: {e}")
return 1
if __name__ == "__main__":
sys.exit(main())