Skip to content
This repository was archived by the owner on Nov 18, 2024. It is now read-only.

Commit 44d0f3e

Browse files
committed
- Properly kills the terminal before starting this API.
1 parent 26b59b1 commit 44d0f3e

File tree

3 files changed

+59
-12
lines changed

3 files changed

+59
-12
lines changed

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "thetadata"
7-
version = "0.8.0"
7+
version = "0.8.1"
88
authors = [
99
{ name="Bailey Danseglio", email="[email protected]" },
1010
{ name="Adler Weber", email="[email protected]" },

thetadata/client.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from tqdm import tqdm
1515
import pandas as pd
1616

17+
from . import terminal
1718
from .enums import *
1819
from .parsing import (
1920
Header,
@@ -234,14 +235,15 @@ def __init__(self, port: int = 11000, timeout: Optional[float] = 60, launch: boo
234235

235236
print('If you require API support, feel free to join our discord server! https://discord.thetadata.us')
236237
if launch:
238+
terminal.kill_existing_terminal()
237239
if username == "default" or passwd == "default":
238240
print('------------------------------------------------------------------------------------------------')
239241
print("You are using the free version of Theta Data. You are currently limited to "
240242
"20 requests / minute.\nA data subscription can be purchased at https://thetadata.net. "
241243
"If you already have a ThetaData\nsubscription, specify the username and passwd parameters.")
242244
print('------------------------------------------------------------------------------------------------')
243-
check_download(auto_update)
244-
Thread(target=launch_terminal, args=[username, passwd, use_bundle, jvm_mem]).start()
245+
if check_download(auto_update):
246+
Thread(target=launch_terminal, args=[username, passwd, use_bundle, jvm_mem, auto_update]).start()
245247
else:
246248
print("You are not launching the terminal. This means you should have an external instance already running.")
247249

thetadata/terminal.py

+54-9
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,23 @@
22
import platform
33
import re
44
import shutil
5+
import signal
56
import subprocess
67
import sys
78
import urllib.request
89
import zipfile
910
from pathlib import Path
1011

12+
import psutil
1113
import wget
1214

1315
jdk_path = Path.home().joinpath('ThetaData').joinpath('ThetaTerminal') \
1416
.joinpath('jdk-19.0.1').joinpath('bin')
1517

1618
to_extract = Path.home().joinpath('ThetaData').joinpath('ThetaTerminal')
1719

20+
_thetadata_jar = "ThetaTerminal.jar"
21+
1822

1923
def bar_progress(current, total, width=80):
2024
progress_message = "Downloading open-jdk 19.0.1 --> %d%% Complete" % (current / total * 100)
@@ -52,12 +56,12 @@ def _verify_java():
5256
# version = float(re.search(pattern, version.decode('utf8')).groups()[0])
5357

5458
# if version < 11:
55-
# print('Java 11 or higher is required to use this API. You are using using Java '
59+
# print('Java 11 or higher is required to use this API. You are using Java '
5660
# + str(version) + '. Please upgrade to a newer version.')
5761
# exit(1)
5862

5963

60-
def launch_terminal(username: str = None, passwd: str = None, use_bundle: bool = True, jvm_mem: int = 0):
64+
def launch_terminal(username: str = None, passwd: str = None, use_bundle: bool = True, jvm_mem: int = 0, move_jar: bool = True):
6165
cwd = None
6266
use_it = False
6367

@@ -66,7 +70,8 @@ def launch_terminal(username: str = None, passwd: str = None, use_bundle: bool =
6670

6771
if use_it:
6872
cwd = jdk_path
69-
shutil.move("ThetaTerminal.jar", str(cwd.joinpath('ThetaTerminal.jar')))
73+
if move_jar:
74+
shutil.move("ThetaTerminal.jar", str(cwd.joinpath('ThetaTerminal.jar')))
7075
else:
7176
_verify_java()
7277

@@ -88,9 +93,49 @@ def launch_terminal(username: str = None, passwd: str = None, use_bundle: bool =
8893
print(line.decode('utf-8').rstrip("\n"))
8994

9095

91-
def check_download(auto_update: bool):
92-
if os.path.exists('ThetaTerminal.jar') or auto_update:
93-
jar = urllib.request.urlopen("https://download-latest.thetadata.us")
94-
with open('ThetaTerminal.jar', 'wb') as output:
95-
output.write(jar.read())
96-
output.close()
96+
def check_download(auto_update: bool) -> bool:
97+
try:
98+
if os.path.exists('ThetaTerminal.jar') or auto_update:
99+
jar = urllib.request.urlopen("https://download-latest.thetadata.us")
100+
with open('ThetaTerminal.jar', 'wb') as output:
101+
output.write(jar.read())
102+
output.close()
103+
return True
104+
except:
105+
print('Unable to fetch the latest terminal version.')
106+
return False
107+
108+
109+
def kill_existing_terminal() -> None:
110+
"""
111+
Utility function to kill any ThetaData terminal processes by scanning all running proceeses
112+
and killing such process
113+
"""
114+
for pid in psutil.pids():
115+
try:
116+
cmdline_args = psutil.Process(pid=pid).cmdline()
117+
for arg in cmdline_args:
118+
if _thetadata_jar in arg:
119+
os.kill(pid, signal.SIGTERM)
120+
except:
121+
pass
122+
123+
124+
def is_terminal_instance_running() -> bool:
125+
"""
126+
Checks if thetadata terminal is running or not
127+
Returns:
128+
bool: True if running else False
129+
"""
130+
running = False
131+
for pid in psutil.pids():
132+
try:
133+
cmdline_args = psutil.Process(pid=pid).cmdline()
134+
for arg in cmdline_args:
135+
if _thetadata_jar in arg:
136+
running = True
137+
break
138+
except:
139+
pass
140+
return running
141+

0 commit comments

Comments
 (0)