JC321 commited on
Commit
c824ed2
·
verified ·
1 Parent(s): 132f2ee

Upload edgar_client.py

Browse files
Files changed (1) hide show
  1. edgar_client.py +43 -68
edgar_client.py CHANGED
@@ -67,80 +67,55 @@ class EdgarDataClient:
67
  if not self.edgar:
68
  return
69
 
70
- # Wrap get_submissions and get_company_facts with timeout
71
  original_get_submissions = self.edgar.get_submissions
72
  original_get_company_facts = self.edgar.get_company_facts
73
 
74
  def get_submissions_with_timeout(cik):
75
- import signal
76
-
77
- def timeout_handler(signum, frame):
78
- raise TimeoutError("SEC API request timeout (30s)")
79
-
80
- # Set signal alarm (Unix only, Windows needs different approach)
81
- try:
82
- signal.signal(signal.SIGALRM, timeout_handler)
83
- signal.alarm(self.timeout)
84
- result = original_get_submissions(cik)
85
- signal.alarm(0) # Cancel alarm
86
- return result
87
- except AttributeError:
88
- # Windows doesn't support signal.SIGALRM, use threading.Timer fallback
89
- result = [None]
90
- exception = [None]
91
-
92
- def wrapper():
93
- try:
94
- result[0] = original_get_submissions(cik)
95
- except Exception as e:
96
- exception[0] = e
97
-
98
- thread = threading.Thread(target=wrapper, daemon=True)
99
- thread.start()
100
- thread.join(timeout=self.timeout)
101
-
102
- if thread.is_alive():
103
- raise TimeoutError("SEC API request timeout (30s)")
104
-
105
- if exception[0]:
106
- raise exception[0]
107
-
108
- return result[0]
109
 
110
  def get_company_facts_with_timeout(cik):
111
- import signal
112
-
113
- def timeout_handler(signum, frame):
114
- raise TimeoutError("SEC API request timeout (30s)")
115
-
116
- try:
117
- signal.signal(signal.SIGALRM, timeout_handler)
118
- signal.alarm(self.timeout)
119
- result = original_get_company_facts(cik)
120
- signal.alarm(0)
121
- return result
122
- except AttributeError:
123
- # Windows fallback
124
- result = [None]
125
- exception = [None]
126
-
127
- def wrapper():
128
- try:
129
- result[0] = original_get_company_facts(cik)
130
- except Exception as e:
131
- exception[0] = e
132
-
133
- thread = threading.Thread(target=wrapper, daemon=True)
134
- thread.start()
135
- thread.join(timeout=self.timeout)
136
-
137
- if thread.is_alive():
138
- raise TimeoutError("SEC API request timeout (30s)")
139
-
140
- if exception[0]:
141
- raise exception[0]
142
-
143
- return result[0]
144
 
145
  self.edgar.get_submissions = get_submissions_with_timeout
146
  self.edgar.get_company_facts = get_company_facts_with_timeout
 
67
  if not self.edgar:
68
  return
69
 
70
+ # Wrap get_submissions and get_company_facts with timeout (thread-based, Gradio compatible)
71
  original_get_submissions = self.edgar.get_submissions
72
  original_get_company_facts = self.edgar.get_company_facts
73
 
74
  def get_submissions_with_timeout(cik):
75
+ """Thread-based timeout wrapper for get_submissions (Gradio compatible)"""
76
+ result = [None]
77
+ exception = [None]
78
+
79
+ def wrapper():
80
+ try:
81
+ result[0] = original_get_submissions(cik)
82
+ except Exception as e:
83
+ exception[0] = e
84
+
85
+ thread = threading.Thread(target=wrapper, daemon=True)
86
+ thread.start()
87
+ thread.join(timeout=self.timeout)
88
+
89
+ if thread.is_alive():
90
+ raise TimeoutError(f"SEC API request timeout ({self.timeout}s)")
91
+
92
+ if exception[0]:
93
+ raise exception[0]
94
+
95
+ return result[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
96
 
97
  def get_company_facts_with_timeout(cik):
98
+ """Thread-based timeout wrapper for get_company_facts (Gradio compatible)"""
99
+ result = [None]
100
+ exception = [None]
101
+
102
+ def wrapper():
103
+ try:
104
+ result[0] = original_get_company_facts(cik)
105
+ except Exception as e:
106
+ exception[0] = e
107
+
108
+ thread = threading.Thread(target=wrapper, daemon=True)
109
+ thread.start()
110
+ thread.join(timeout=self.timeout)
111
+
112
+ if thread.is_alive():
113
+ raise TimeoutError(f"SEC API request timeout ({self.timeout}s)")
114
+
115
+ if exception[0]:
116
+ raise exception[0]
117
+
118
+ return result[0]
 
 
 
 
 
 
 
 
 
 
 
 
119
 
120
  self.edgar.get_submissions = get_submissions_with_timeout
121
  self.edgar.get_company_facts = get_company_facts_with_timeout