vnai 2.1.1__py3-none-any.whl → 2.1.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- vnai/__init__.py +78 -78
- vnai/beam/__init__.py +2 -2
- vnai/beam/metrics.py +72 -72
- vnai/beam/pulse.py +30 -30
- vnai/beam/quota.py +104 -104
- vnai/flow/__init__.py +1 -1
- vnai/flow/queue.py +54 -54
- vnai/flow/relay.py +160 -160
- vnai/scope/__init__.py +3 -3
- vnai/scope/profile.py +222 -222
- vnai/scope/promo.py +112 -117
- vnai/scope/state.py +73 -73
- {vnai-2.1.1.dist-info → vnai-2.1.3.dist-info}/METADATA +20 -20
- vnai-2.1.3.dist-info/RECORD +16 -0
- vnai-2.1.1.dist-info/RECORD +0 -16
- {vnai-2.1.1.dist-info → vnai-2.1.3.dist-info}/WHEEL +0 -0
- {vnai-2.1.1.dist-info → vnai-2.1.3.dist-info}/top_level.txt +0 -0
vnai/scope/profile.py
CHANGED
@@ -1,223 +1,223 @@
|
|
1
|
-
_W='type_confidence'
|
2
|
-
_V='detected_type'
|
3
|
-
_U='commercial_app'
|
4
|
-
_T='version'
|
5
|
-
_S='django'
|
6
|
-
_R='fastapi'
|
7
|
-
_Q='streamlit'
|
8
|
-
_P='indicators'
|
9
|
-
_O='commercial_indicators'
|
10
|
-
_N='likely_commercial'
|
11
|
-
_M='KAGGLE_KERNEL_RUN_TYPE'
|
12
|
-
_L='machine_id'
|
13
|
-
_K='domain'
|
14
|
-
_J='.git'
|
15
|
-
_I='backtesting'
|
16
|
-
_H='commercial_probability'
|
17
|
-
_G='timestamp'
|
18
|
-
_F='business_hours_usage'
|
19
|
-
_E='google.colab'
|
20
|
-
_D='unknown'
|
21
|
-
_C=False
|
22
|
-
_B=None
|
23
|
-
_A=True
|
24
|
-
import os,sys,platform,uuid,hashlib,psutil,threading,time,importlib.metadata
|
25
|
-
from datetime import datetime
|
26
|
-
import subprocess
|
27
|
-
from pathlib import Path
|
28
|
-
class Inspector:
|
29
|
-
_instance=_B;_lock=_B
|
30
|
-
def __new__(cls):
|
31
|
-
import threading
|
32
|
-
if cls._lock is _B:cls._lock=threading.Lock()
|
33
|
-
with cls._lock:
|
34
|
-
if cls._instance is _B:cls._instance=super(Inspector,cls).__new__(cls);cls._instance._initialize()
|
35
|
-
return cls._instance
|
36
|
-
def _initialize(self):self.cache={};self.cache_ttl=3600;self.last_examination=0;self.machine_id=_B;self._colab_auth_triggered=_C;self.home_dir=Path.home();self.project_dir=self.home_dir/'.vnstock';self.project_dir.mkdir(exist_ok=_A);self.id_dir=self.project_dir/'id';self.id_dir.mkdir(exist_ok=_A);self.machine_id_path=self.id_dir/'machine_id.txt';self.examine()
|
37
|
-
def examine(self,force_refresh=_C):
|
38
|
-
D='script';C='terminal';B='hosting_service';A='environment';current_time=time.time()
|
39
|
-
if not force_refresh and current_time-self.last_examination<self.cache_ttl:return self.cache
|
40
|
-
info={_G:datetime.now().isoformat(),'python_version':platform.python_version(),'os_name':platform.system(),'platform':platform.platform()};info[_L]=self.fingerprint()
|
41
|
-
try:
|
42
|
-
import importlib.util;ipython_spec=importlib.util.find_spec('IPython')
|
43
|
-
if ipython_spec:
|
44
|
-
from IPython import get_ipython;ipython=get_ipython()
|
45
|
-
if ipython is not _B:
|
46
|
-
info[A]='jupyter'
|
47
|
-
if _E in sys.modules:info[B]='colab'
|
48
|
-
elif _M in os.environ:info[B]='kaggle'
|
49
|
-
else:info[B]='local_jupyter'
|
50
|
-
elif sys.stdout.isatty():info[A]=C
|
51
|
-
else:info[A]=D
|
52
|
-
elif sys.stdout.isatty():info[A]=C
|
53
|
-
else:info[A]=D
|
54
|
-
except:info[A]=_D
|
55
|
-
try:info['cpu_count']=os.cpu_count();info['memory_gb']=round(psutil.virtual_memory().total/1024**3,1)
|
56
|
-
except:pass
|
57
|
-
is_colab=_E in sys.modules
|
58
|
-
if is_colab:info['is_colab']=_A;self.detect_colab_with_delayed_auth()
|
59
|
-
try:info['commercial_usage']=self.enhanced_commercial_detection();info['project_context']=self.analyze_project_structure();info['git_info']=self.analyze_git_info();info['usage_pattern']=self.detect_usage_pattern();info['dependencies']=self.analyze_dependencies()
|
60
|
-
except Exception as e:info['detection_error']=str(e)
|
61
|
-
self.cache=info;self.last_examination=current_time;return info
|
62
|
-
def fingerprint(self):
|
63
|
-
if self.machine_id:return self.machine_id
|
64
|
-
if self.machine_id_path.exists():
|
65
|
-
try:
|
66
|
-
with open(self.machine_id_path,'r')as f:self.machine_id=f.read().strip();return self.machine_id
|
67
|
-
except:pass
|
68
|
-
is_colab=self.detect_colab_with_delayed_auth()
|
69
|
-
try:system_info=platform.node()+platform.platform()+platform.machine();self.machine_id=hashlib.md5(system_info.encode()).hexdigest()
|
70
|
-
except:self.machine_id=str(uuid.uuid4())
|
71
|
-
try:
|
72
|
-
with open(self.machine_id_path,'w')as f:f.write(self.machine_id)
|
73
|
-
except:pass
|
74
|
-
return self.machine_id
|
75
|
-
def detect_hosting(self):
|
76
|
-
A='Google Colab';hosting_markers={'COLAB_GPU':A,_M:'Kaggle','BINDER_SERVICE_HOST':'Binder','CODESPACE_NAME':'GitHub Codespaces','STREAMLIT_SERVER_HEADLESS':'Streamlit Cloud','CLOUD_SHELL':'Cloud Shell'}
|
77
|
-
for(env_var,host_name)in hosting_markers.items():
|
78
|
-
if env_var in os.environ:return host_name
|
79
|
-
if _E in sys.modules:return A
|
80
|
-
return'local'
|
81
|
-
def detect_commercial_usage(self):
|
82
|
-
F='client';E='enterprise';D='dir_patterns';C='env_vars';B='file_patterns';A='env_domains';commercial_indicators={A:['.com','.io','.co',E,'corp','inc'],B:['invoice','payment','customer',F,'product','sale'],C:['COMPANY','BUSINESS','ENTERPRISE','CORPORATE'],D:['company','business',E,'corporate',F]};env_values=' '.join(os.environ.values()).lower();domain_match=any(domain in env_values for domain in commercial_indicators[A]);env_var_match=any(var in os.environ for var in commercial_indicators[C]);current_dir=os.getcwd().lower();dir_match=any(pattern in current_dir for pattern in commercial_indicators[D])
|
83
|
-
try:files=[f.lower()for f in os.listdir()if os.path.isfile(f)];file_match=any(any(pattern in f for pattern in commercial_indicators[B])for f in files)
|
84
|
-
except:file_match=_C
|
85
|
-
indicators=[domain_match,env_var_match,dir_match,file_match];commercial_probability=sum(indicators)/len(indicators);return{_N:commercial_probability>.3,_H:commercial_probability,_O:{'domain_match':domain_match,'env_var_match':env_var_match,'dir_match':dir_match,'file_match':file_match}}
|
86
|
-
def scan_packages(self):
|
87
|
-
A='financetoolkit';package_groups={'vnstock_family':['vnstock','vnstock3','vnstock_ezchart','vnstock_data_pro','vnstock_market_data_pipeline','vnstock_ta','vnii','vnai'],'analytics':['openbb','pandas_ta'],'static_charts':['matplotlib','seaborn','altair'],'dashboard':[_Q,'voila','panel','shiny','dash'],'interactive_charts':['mplfinance','plotly','plotline','bokeh','pyecharts','highcharts-core','highcharts-stock','mplchart'],'datafeed':['yfinance','alpha_vantage','pandas-datareader','investpy'],'official_api':['ssi-fc-data','ssi-fctrading'],'risk_return':['pyfolio','empyrical','quantstats',A],'machine_learning':['scipy','sklearn','statsmodels','pytorch','tensorflow','keras','xgboost'],_P:['stochastic','talib','tqdm','finta',A,'tulipindicators'],_I:['vectorbt',_I,'bt','zipline','pyalgotrade','backtrader','pybacktest','fastquant','lean','ta','finmarketpy','qstrader'],'server':[_R,'flask','uvicorn','gunicorn'],'framework':['lightgbm','catboost',_S]};installed={}
|
88
|
-
for(category,packages)in package_groups.items():
|
89
|
-
installed[category]=[]
|
90
|
-
for pkg in packages:
|
91
|
-
try:version=importlib.metadata.version(pkg);installed[category].append({'name':pkg,_T:version})
|
92
|
-
except:pass
|
93
|
-
return installed
|
94
|
-
def setup_vnstock_environment(self):
|
95
|
-
env_file=self.id_dir/'environment.json';env_data={'accepted_agreement':_A,_G:datetime.now().isoformat(),_L:self.fingerprint()}
|
96
|
-
try:
|
97
|
-
with open(env_file,'w')as f:import json;json.dump(env_data,f)
|
98
|
-
return _A
|
99
|
-
except Exception as e:print(f"Failed to set up vnstock environment: {e}");return _C
|
100
|
-
def detect_colab_with_delayed_auth(self,immediate=_C):
|
101
|
-
is_colab=_E in sys.modules
|
102
|
-
if is_colab and not self._colab_auth_triggered:
|
103
|
-
if immediate:
|
104
|
-
self._colab_auth_triggered=_A;user_id=self.get_or_create_user_id()
|
105
|
-
if user_id and user_id!=self.machine_id:
|
106
|
-
self.machine_id=user_id
|
107
|
-
try:
|
108
|
-
with open(self.machine_id_path,'w')as f:f.write(user_id)
|
109
|
-
except:pass
|
110
|
-
else:
|
111
|
-
def delayed_auth():
|
112
|
-
time.sleep(300);user_id=self.get_or_create_user_id()
|
113
|
-
if user_id and user_id!=self.machine_id:
|
114
|
-
self.machine_id=user_id
|
115
|
-
try:
|
116
|
-
with open(self.machine_id_path,'w')as f:f.write(user_id)
|
117
|
-
except:pass
|
118
|
-
thread=threading.Thread(target=delayed_auth,daemon=_A);thread.start()
|
119
|
-
return is_colab
|
120
|
-
def get_or_create_user_id(self):
|
121
|
-
if self._colab_auth_triggered:return self.machine_id
|
122
|
-
try:
|
123
|
-
from google.colab import drive;print('\n📋 Kết nối tài khoản Google Drive để lưu các thiết lập của dự án.');print('Dữ liệu phiên làm việc với Colab của bạn sẽ bị xóa nếu không lưu trữ vào Google Drive.\n');self._colab_auth_triggered=_A;drive.mount('/content/drive');id_path='/content/drive/MyDrive/.vnstock/user_id.txt'
|
124
|
-
if os.path.exists(id_path):
|
125
|
-
with open(id_path,'r')as f:return f.read().strip()
|
126
|
-
else:
|
127
|
-
user_id=str(uuid.uuid4());os.makedirs(os.path.dirname(id_path),exist_ok=_A)
|
128
|
-
with open(id_path,'w')as f:f.write(user_id)
|
129
|
-
return user_id
|
130
|
-
except Exception as e:return self.machine_id
|
131
|
-
def analyze_project_structure(self):
|
132
|
-
E='root_dirs';D='manage.py';C='wsgi.py';B='data_science';A='app.py';current_dir=os.getcwd();project_indicators={_U:['app','services','products','customers','billing'],'financial_tool':['portfolio',_I,'trading','strategy'],B:['models','notebooks','datasets','visualization'],'educational':['examples','lectures','assignments','slides']};project_type={}
|
133
|
-
for(category,markers)in project_indicators.items():
|
134
|
-
match_count=0
|
135
|
-
for marker in markers:
|
136
|
-
if os.path.exists(os.path.join(current_dir,marker)):match_count+=1
|
137
|
-
if len(markers)>0:project_type[category]=match_count/len(markers)
|
138
|
-
try:
|
139
|
-
root_files=[f for f in os.listdir(current_dir)if os.path.isfile(os.path.join(current_dir,f))];root_dirs=[d for d in os.listdir(current_dir)if os.path.isdir(os.path.join(current_dir,d))];file_markers={'python_project':['setup.py','pyproject.toml','requirements.txt'],B:['notebook.ipynb','.ipynb_checkpoints'],'web_app':[A,C,D,'server.py'],'finance_app':['portfolio.py','trading.py','backtest.py']};file_project_type=_D
|
140
|
-
for(ptype,markers)in file_markers.items():
|
141
|
-
if any(marker in root_files for marker in markers):file_project_type=ptype;break
|
142
|
-
frameworks=[];framework_markers={_S:[D,'settings.py'],'flask':[A,C],_Q:['streamlit_app.py',A],_R:['main.py',A]}
|
143
|
-
for(framework,markers)in framework_markers.items():
|
144
|
-
if any(marker in root_files for marker in markers):frameworks.append(framework)
|
145
|
-
except Exception as e:root_files=[];root_dirs=[];file_project_type=_D;frameworks=[]
|
146
|
-
return{'project_dir':current_dir,_V:max(project_type.items(),key=lambda x:x[1])[0]if project_type else _D,'file_type':file_project_type,'is_git_repo':_J in(root_dirs if E in locals()else[]),'frameworks':frameworks,'file_count':len(root_files)if'root_files'in locals()else 0,'directory_count':len(root_dirs)if E in locals()else 0,_W:project_type}
|
147
|
-
def analyze_git_info(self):
|
148
|
-
I='license_type';H='has_license';G='repo_path';F='rev-parse';E='/';D='has_git';C=':';B='git';A='@'
|
149
|
-
try:
|
150
|
-
result=subprocess.run([B,F,'--is-inside-work-tree'],capture_output=_A,text=_A)
|
151
|
-
if result.returncode!=0:return{D:_C}
|
152
|
-
repo_root=subprocess.run([B,F,'--show-toplevel'],capture_output=_A,text=_A);repo_path=repo_root.stdout.strip()if repo_root.stdout else _B;repo_name=os.path.basename(repo_path)if repo_path else _B;has_license=_C;license_type=_D
|
153
|
-
if repo_path:
|
154
|
-
license_files=[os.path.join(repo_path,'LICENSE'),os.path.join(repo_path,'LICENSE.txt'),os.path.join(repo_path,'LICENSE.md')]
|
155
|
-
for license_file in license_files:
|
156
|
-
if os.path.exists(license_file):
|
157
|
-
has_license=_A
|
158
|
-
try:
|
159
|
-
with open(license_file,'r')as f:
|
160
|
-
content=f.read().lower()
|
161
|
-
if'mit license'in content:license_type='MIT'
|
162
|
-
elif'apache license'in content:license_type='Apache'
|
163
|
-
elif'gnu general public'in content:license_type='GPL'
|
164
|
-
elif'bsd 'in content:license_type='BSD'
|
165
|
-
except:pass
|
166
|
-
break
|
167
|
-
remote=subprocess.run([B,'config','--get','remote.origin.url'],capture_output=_A,text=_A);remote_url=remote.stdout.strip()if remote.stdout else _B
|
168
|
-
if remote_url:
|
169
|
-
remote_url=remote_url.strip();domain=_B
|
170
|
-
if remote_url:
|
171
|
-
if remote_url.startswith('git@')or A in remote_url and C in remote_url.split(A)[1]:domain=remote_url.split(A)[1].split(C)[0]
|
172
|
-
elif remote_url.startswith('http'):
|
173
|
-
url_parts=remote_url.split('//')
|
174
|
-
if len(url_parts)>1:
|
175
|
-
auth_and_domain=url_parts[1].split(E,1)[0]
|
176
|
-
if A in auth_and_domain:domain=auth_and_domain.split(A)[-1]
|
177
|
-
else:domain=auth_and_domain
|
178
|
-
else:
|
179
|
-
import re;domain_match=re.search('@([^:/]+)|https?://(?:[^@/]+@)?([^/]+)',remote_url)
|
180
|
-
if domain_match:domain=domain_match.group(1)or domain_match.group(2)
|
181
|
-
owner=_B;repo_name=_B
|
182
|
-
if domain:
|
183
|
-
if'github'in domain:
|
184
|
-
if C in remote_url and A in remote_url:
|
185
|
-
parts=remote_url.split(C)[-1].split(E)
|
186
|
-
if len(parts)>=2:owner=parts[0];repo_name=parts[1].replace(_J,'')
|
187
|
-
else:
|
188
|
-
url_parts=remote_url.split('//')
|
189
|
-
if len(url_parts)>1:
|
190
|
-
path_parts=url_parts[1].split(E)
|
191
|
-
if len(path_parts)>=3:
|
192
|
-
domain_part=path_parts[0]
|
193
|
-
if A in domain_part:owner_index=1
|
194
|
-
else:owner_index=1
|
195
|
-
if len(path_parts)>owner_index:owner=path_parts[owner_index]
|
196
|
-
if len(path_parts)>owner_index+1:repo_name=path_parts[owner_index+1].replace(_J,'')
|
197
|
-
commit_count=subprocess.run([B,'rev-list','--count','HEAD'],capture_output=_A,text=_A);branch_count=subprocess.run([B,'branch','--list'],capture_output=_A,text=_A);branch_count=len(branch_count.stdout.strip().split('\n'))if branch_count.stdout else 0;return{_K:domain,'owner':owner,'commit_count':int(commit_count.stdout.strip())if commit_count.stdout else 0,'branch_count':branch_count,D:_A,G:repo_path if G in locals()else _B,'repo_name':repo_name,H:has_license if H in locals()else _C,I:license_type if I in locals()else _D}
|
198
|
-
except Exception as e:pass
|
199
|
-
return{D:_C}
|
200
|
-
def detect_usage_pattern(self):current_time=datetime.now();is_weekday=current_time.weekday()<5;hour=current_time.hour;is_business_hours=9<=hour<=18;return{_F:is_weekday and is_business_hours,'weekday':is_weekday,'hour':hour,_G:current_time.isoformat()}
|
201
|
-
def enhanced_commercial_detection(self):
|
202
|
-
basic=self.detect_commercial_usage()
|
203
|
-
try:
|
204
|
-
project_files=os.listdir(os.getcwd());commercial_frameworks=['django-oscar','opencart','magento','saleor','odoo','shopify','woocommerce'];framework_match=_C
|
205
|
-
for framework in commercial_frameworks:
|
206
|
-
if any(framework in f for f in project_files):framework_match=_A;break
|
207
|
-
db_files=[f for f in project_files if'database'in f.lower()or'db_config'in f.lower()or f.endswith('.db')];has_database=len(db_files)>0
|
208
|
-
except:framework_match=_C;has_database=_C
|
209
|
-
domain_check=self.analyze_git_info();domain_is_commercial=_C
|
210
|
-
if domain_check and domain_check.get(_K):commercial_tlds=['.com','.io','.co','.org','.net'];domain_is_commercial=any(tld in domain_check[_K]for tld in commercial_tlds)
|
211
|
-
project_structure=self.analyze_project_structure();indicators=[basic[_H],framework_match,has_database,domain_is_commercial,project_structure.get(_W,{}).get(_U,0),self.detect_usage_pattern()[_F]];indicators=[i for i in indicators if i is not _B]
|
212
|
-
if indicators:score=sum(1. if isinstance(i,bool)and i else i if isinstance(i,(int,float))else 0 for i in indicators)/len(indicators)
|
213
|
-
else:score=0
|
214
|
-
return{_H:score,_N:score>.4,_P:{'basic_indicators':basic[_O],'framework_match':framework_match,'has_database':has_database,'domain_is_commercial':domain_is_commercial,'project_structure':project_structure.get(_V),_F:self.detect_usage_pattern()[_F]}}
|
215
|
-
def analyze_dependencies(self):
|
216
|
-
A='has_commercial_deps'
|
217
|
-
try:
|
218
|
-
import pkg_resources;enterprise_packages=['snowflake-connector-python','databricks','azure','aws','google-cloud','stripe','atlassian','salesforce','bigquery','tableau','sap'];commercial_deps=[]
|
219
|
-
for pkg in pkg_resources.working_set:
|
220
|
-
if any(ent in pkg.key for ent in enterprise_packages):commercial_deps.append({'name':pkg.key,_T:pkg.version})
|
221
|
-
return{A:len(commercial_deps)>0,'commercial_deps_count':len(commercial_deps),'commercial_deps':commercial_deps}
|
222
|
-
except:return{A:_C}
|
1
|
+
_W='type_confidence'
|
2
|
+
_V='detected_type'
|
3
|
+
_U='commercial_app'
|
4
|
+
_T='version'
|
5
|
+
_S='django'
|
6
|
+
_R='fastapi'
|
7
|
+
_Q='streamlit'
|
8
|
+
_P='indicators'
|
9
|
+
_O='commercial_indicators'
|
10
|
+
_N='likely_commercial'
|
11
|
+
_M='KAGGLE_KERNEL_RUN_TYPE'
|
12
|
+
_L='machine_id'
|
13
|
+
_K='domain'
|
14
|
+
_J='.git'
|
15
|
+
_I='backtesting'
|
16
|
+
_H='commercial_probability'
|
17
|
+
_G='timestamp'
|
18
|
+
_F='business_hours_usage'
|
19
|
+
_E='google.colab'
|
20
|
+
_D='unknown'
|
21
|
+
_C=False
|
22
|
+
_B=None
|
23
|
+
_A=True
|
24
|
+
import os,sys,platform,uuid,hashlib,psutil,threading,time,importlib.metadata
|
25
|
+
from datetime import datetime
|
26
|
+
import subprocess
|
27
|
+
from pathlib import Path
|
28
|
+
class Inspector:
|
29
|
+
_instance=_B;_lock=_B
|
30
|
+
def __new__(cls):
|
31
|
+
import threading
|
32
|
+
if cls._lock is _B:cls._lock=threading.Lock()
|
33
|
+
with cls._lock:
|
34
|
+
if cls._instance is _B:cls._instance=super(Inspector,cls).__new__(cls);cls._instance._initialize()
|
35
|
+
return cls._instance
|
36
|
+
def _initialize(self):self.cache={};self.cache_ttl=3600;self.last_examination=0;self.machine_id=_B;self._colab_auth_triggered=_C;self.home_dir=Path.home();self.project_dir=self.home_dir/'.vnstock';self.project_dir.mkdir(exist_ok=_A);self.id_dir=self.project_dir/'id';self.id_dir.mkdir(exist_ok=_A);self.machine_id_path=self.id_dir/'machine_id.txt';self.examine()
|
37
|
+
def examine(self,force_refresh=_C):
|
38
|
+
D='script';C='terminal';B='hosting_service';A='environment';current_time=time.time()
|
39
|
+
if not force_refresh and current_time-self.last_examination<self.cache_ttl:return self.cache
|
40
|
+
info={_G:datetime.now().isoformat(),'python_version':platform.python_version(),'os_name':platform.system(),'platform':platform.platform()};info[_L]=self.fingerprint()
|
41
|
+
try:
|
42
|
+
import importlib.util;ipython_spec=importlib.util.find_spec('IPython')
|
43
|
+
if ipython_spec:
|
44
|
+
from IPython import get_ipython;ipython=get_ipython()
|
45
|
+
if ipython is not _B:
|
46
|
+
info[A]='jupyter'
|
47
|
+
if _E in sys.modules:info[B]='colab'
|
48
|
+
elif _M in os.environ:info[B]='kaggle'
|
49
|
+
else:info[B]='local_jupyter'
|
50
|
+
elif sys.stdout.isatty():info[A]=C
|
51
|
+
else:info[A]=D
|
52
|
+
elif sys.stdout.isatty():info[A]=C
|
53
|
+
else:info[A]=D
|
54
|
+
except:info[A]=_D
|
55
|
+
try:info['cpu_count']=os.cpu_count();info['memory_gb']=round(psutil.virtual_memory().total/1024**3,1)
|
56
|
+
except:pass
|
57
|
+
is_colab=_E in sys.modules
|
58
|
+
if is_colab:info['is_colab']=_A;self.detect_colab_with_delayed_auth()
|
59
|
+
try:info['commercial_usage']=self.enhanced_commercial_detection();info['project_context']=self.analyze_project_structure();info['git_info']=self.analyze_git_info();info['usage_pattern']=self.detect_usage_pattern();info['dependencies']=self.analyze_dependencies()
|
60
|
+
except Exception as e:info['detection_error']=str(e)
|
61
|
+
self.cache=info;self.last_examination=current_time;return info
|
62
|
+
def fingerprint(self):
|
63
|
+
if self.machine_id:return self.machine_id
|
64
|
+
if self.machine_id_path.exists():
|
65
|
+
try:
|
66
|
+
with open(self.machine_id_path,'r')as f:self.machine_id=f.read().strip();return self.machine_id
|
67
|
+
except:pass
|
68
|
+
is_colab=self.detect_colab_with_delayed_auth()
|
69
|
+
try:system_info=platform.node()+platform.platform()+platform.machine();self.machine_id=hashlib.md5(system_info.encode()).hexdigest()
|
70
|
+
except:self.machine_id=str(uuid.uuid4())
|
71
|
+
try:
|
72
|
+
with open(self.machine_id_path,'w')as f:f.write(self.machine_id)
|
73
|
+
except:pass
|
74
|
+
return self.machine_id
|
75
|
+
def detect_hosting(self):
|
76
|
+
A='Google Colab';hosting_markers={'COLAB_GPU':A,_M:'Kaggle','BINDER_SERVICE_HOST':'Binder','CODESPACE_NAME':'GitHub Codespaces','STREAMLIT_SERVER_HEADLESS':'Streamlit Cloud','CLOUD_SHELL':'Cloud Shell'}
|
77
|
+
for(env_var,host_name)in hosting_markers.items():
|
78
|
+
if env_var in os.environ:return host_name
|
79
|
+
if _E in sys.modules:return A
|
80
|
+
return'local'
|
81
|
+
def detect_commercial_usage(self):
|
82
|
+
F='client';E='enterprise';D='dir_patterns';C='env_vars';B='file_patterns';A='env_domains';commercial_indicators={A:['.com','.io','.co',E,'corp','inc'],B:['invoice','payment','customer',F,'product','sale'],C:['COMPANY','BUSINESS','ENTERPRISE','CORPORATE'],D:['company','business',E,'corporate',F]};env_values=' '.join(os.environ.values()).lower();domain_match=any(domain in env_values for domain in commercial_indicators[A]);env_var_match=any(var in os.environ for var in commercial_indicators[C]);current_dir=os.getcwd().lower();dir_match=any(pattern in current_dir for pattern in commercial_indicators[D])
|
83
|
+
try:files=[f.lower()for f in os.listdir()if os.path.isfile(f)];file_match=any(any(pattern in f for pattern in commercial_indicators[B])for f in files)
|
84
|
+
except:file_match=_C
|
85
|
+
indicators=[domain_match,env_var_match,dir_match,file_match];commercial_probability=sum(indicators)/len(indicators);return{_N:commercial_probability>.3,_H:commercial_probability,_O:{'domain_match':domain_match,'env_var_match':env_var_match,'dir_match':dir_match,'file_match':file_match}}
|
86
|
+
def scan_packages(self):
|
87
|
+
A='financetoolkit';package_groups={'vnstock_family':['vnstock','vnstock3','vnstock_ezchart','vnstock_data_pro','vnstock_market_data_pipeline','vnstock_ta','vnii','vnai'],'analytics':['openbb','pandas_ta'],'static_charts':['matplotlib','seaborn','altair'],'dashboard':[_Q,'voila','panel','shiny','dash'],'interactive_charts':['mplfinance','plotly','plotline','bokeh','pyecharts','highcharts-core','highcharts-stock','mplchart'],'datafeed':['yfinance','alpha_vantage','pandas-datareader','investpy'],'official_api':['ssi-fc-data','ssi-fctrading'],'risk_return':['pyfolio','empyrical','quantstats',A],'machine_learning':['scipy','sklearn','statsmodels','pytorch','tensorflow','keras','xgboost'],_P:['stochastic','talib','tqdm','finta',A,'tulipindicators'],_I:['vectorbt',_I,'bt','zipline','pyalgotrade','backtrader','pybacktest','fastquant','lean','ta','finmarketpy','qstrader'],'server':[_R,'flask','uvicorn','gunicorn'],'framework':['lightgbm','catboost',_S]};installed={}
|
88
|
+
for(category,packages)in package_groups.items():
|
89
|
+
installed[category]=[]
|
90
|
+
for pkg in packages:
|
91
|
+
try:version=importlib.metadata.version(pkg);installed[category].append({'name':pkg,_T:version})
|
92
|
+
except:pass
|
93
|
+
return installed
|
94
|
+
def setup_vnstock_environment(self):
|
95
|
+
env_file=self.id_dir/'environment.json';env_data={'accepted_agreement':_A,_G:datetime.now().isoformat(),_L:self.fingerprint()}
|
96
|
+
try:
|
97
|
+
with open(env_file,'w')as f:import json;json.dump(env_data,f)
|
98
|
+
return _A
|
99
|
+
except Exception as e:print(f"Failed to set up vnstock environment: {e}");return _C
|
100
|
+
def detect_colab_with_delayed_auth(self,immediate=_C):
|
101
|
+
is_colab=_E in sys.modules
|
102
|
+
if is_colab and not self._colab_auth_triggered:
|
103
|
+
if immediate:
|
104
|
+
self._colab_auth_triggered=_A;user_id=self.get_or_create_user_id()
|
105
|
+
if user_id and user_id!=self.machine_id:
|
106
|
+
self.machine_id=user_id
|
107
|
+
try:
|
108
|
+
with open(self.machine_id_path,'w')as f:f.write(user_id)
|
109
|
+
except:pass
|
110
|
+
else:
|
111
|
+
def delayed_auth():
|
112
|
+
time.sleep(300);user_id=self.get_or_create_user_id()
|
113
|
+
if user_id and user_id!=self.machine_id:
|
114
|
+
self.machine_id=user_id
|
115
|
+
try:
|
116
|
+
with open(self.machine_id_path,'w')as f:f.write(user_id)
|
117
|
+
except:pass
|
118
|
+
thread=threading.Thread(target=delayed_auth,daemon=_A);thread.start()
|
119
|
+
return is_colab
|
120
|
+
def get_or_create_user_id(self):
|
121
|
+
if self._colab_auth_triggered:return self.machine_id
|
122
|
+
try:
|
123
|
+
from google.colab import drive;print('\n📋 Kết nối tài khoản Google Drive để lưu các thiết lập của dự án.');print('Dữ liệu phiên làm việc với Colab của bạn sẽ bị xóa nếu không lưu trữ vào Google Drive.\n');self._colab_auth_triggered=_A;drive.mount('/content/drive');id_path='/content/drive/MyDrive/.vnstock/user_id.txt'
|
124
|
+
if os.path.exists(id_path):
|
125
|
+
with open(id_path,'r')as f:return f.read().strip()
|
126
|
+
else:
|
127
|
+
user_id=str(uuid.uuid4());os.makedirs(os.path.dirname(id_path),exist_ok=_A)
|
128
|
+
with open(id_path,'w')as f:f.write(user_id)
|
129
|
+
return user_id
|
130
|
+
except Exception as e:return self.machine_id
|
131
|
+
def analyze_project_structure(self):
|
132
|
+
E='root_dirs';D='manage.py';C='wsgi.py';B='data_science';A='app.py';current_dir=os.getcwd();project_indicators={_U:['app','services','products','customers','billing'],'financial_tool':['portfolio',_I,'trading','strategy'],B:['models','notebooks','datasets','visualization'],'educational':['examples','lectures','assignments','slides']};project_type={}
|
133
|
+
for(category,markers)in project_indicators.items():
|
134
|
+
match_count=0
|
135
|
+
for marker in markers:
|
136
|
+
if os.path.exists(os.path.join(current_dir,marker)):match_count+=1
|
137
|
+
if len(markers)>0:project_type[category]=match_count/len(markers)
|
138
|
+
try:
|
139
|
+
root_files=[f for f in os.listdir(current_dir)if os.path.isfile(os.path.join(current_dir,f))];root_dirs=[d for d in os.listdir(current_dir)if os.path.isdir(os.path.join(current_dir,d))];file_markers={'python_project':['setup.py','pyproject.toml','requirements.txt'],B:['notebook.ipynb','.ipynb_checkpoints'],'web_app':[A,C,D,'server.py'],'finance_app':['portfolio.py','trading.py','backtest.py']};file_project_type=_D
|
140
|
+
for(ptype,markers)in file_markers.items():
|
141
|
+
if any(marker in root_files for marker in markers):file_project_type=ptype;break
|
142
|
+
frameworks=[];framework_markers={_S:[D,'settings.py'],'flask':[A,C],_Q:['streamlit_app.py',A],_R:['main.py',A]}
|
143
|
+
for(framework,markers)in framework_markers.items():
|
144
|
+
if any(marker in root_files for marker in markers):frameworks.append(framework)
|
145
|
+
except Exception as e:root_files=[];root_dirs=[];file_project_type=_D;frameworks=[]
|
146
|
+
return{'project_dir':current_dir,_V:max(project_type.items(),key=lambda x:x[1])[0]if project_type else _D,'file_type':file_project_type,'is_git_repo':_J in(root_dirs if E in locals()else[]),'frameworks':frameworks,'file_count':len(root_files)if'root_files'in locals()else 0,'directory_count':len(root_dirs)if E in locals()else 0,_W:project_type}
|
147
|
+
def analyze_git_info(self):
|
148
|
+
I='license_type';H='has_license';G='repo_path';F='rev-parse';E='/';D='has_git';C=':';B='git';A='@'
|
149
|
+
try:
|
150
|
+
result=subprocess.run([B,F,'--is-inside-work-tree'],capture_output=_A,text=_A)
|
151
|
+
if result.returncode!=0:return{D:_C}
|
152
|
+
repo_root=subprocess.run([B,F,'--show-toplevel'],capture_output=_A,text=_A);repo_path=repo_root.stdout.strip()if repo_root.stdout else _B;repo_name=os.path.basename(repo_path)if repo_path else _B;has_license=_C;license_type=_D
|
153
|
+
if repo_path:
|
154
|
+
license_files=[os.path.join(repo_path,'LICENSE'),os.path.join(repo_path,'LICENSE.txt'),os.path.join(repo_path,'LICENSE.md')]
|
155
|
+
for license_file in license_files:
|
156
|
+
if os.path.exists(license_file):
|
157
|
+
has_license=_A
|
158
|
+
try:
|
159
|
+
with open(license_file,'r')as f:
|
160
|
+
content=f.read().lower()
|
161
|
+
if'mit license'in content:license_type='MIT'
|
162
|
+
elif'apache license'in content:license_type='Apache'
|
163
|
+
elif'gnu general public'in content:license_type='GPL'
|
164
|
+
elif'bsd 'in content:license_type='BSD'
|
165
|
+
except:pass
|
166
|
+
break
|
167
|
+
remote=subprocess.run([B,'config','--get','remote.origin.url'],capture_output=_A,text=_A);remote_url=remote.stdout.strip()if remote.stdout else _B
|
168
|
+
if remote_url:
|
169
|
+
remote_url=remote_url.strip();domain=_B
|
170
|
+
if remote_url:
|
171
|
+
if remote_url.startswith('git@')or A in remote_url and C in remote_url.split(A)[1]:domain=remote_url.split(A)[1].split(C)[0]
|
172
|
+
elif remote_url.startswith('http'):
|
173
|
+
url_parts=remote_url.split('//')
|
174
|
+
if len(url_parts)>1:
|
175
|
+
auth_and_domain=url_parts[1].split(E,1)[0]
|
176
|
+
if A in auth_and_domain:domain=auth_and_domain.split(A)[-1]
|
177
|
+
else:domain=auth_and_domain
|
178
|
+
else:
|
179
|
+
import re;domain_match=re.search('@([^:/]+)|https?://(?:[^@/]+@)?([^/]+)',remote_url)
|
180
|
+
if domain_match:domain=domain_match.group(1)or domain_match.group(2)
|
181
|
+
owner=_B;repo_name=_B
|
182
|
+
if domain:
|
183
|
+
if'github'in domain:
|
184
|
+
if C in remote_url and A in remote_url:
|
185
|
+
parts=remote_url.split(C)[-1].split(E)
|
186
|
+
if len(parts)>=2:owner=parts[0];repo_name=parts[1].replace(_J,'')
|
187
|
+
else:
|
188
|
+
url_parts=remote_url.split('//')
|
189
|
+
if len(url_parts)>1:
|
190
|
+
path_parts=url_parts[1].split(E)
|
191
|
+
if len(path_parts)>=3:
|
192
|
+
domain_part=path_parts[0]
|
193
|
+
if A in domain_part:owner_index=1
|
194
|
+
else:owner_index=1
|
195
|
+
if len(path_parts)>owner_index:owner=path_parts[owner_index]
|
196
|
+
if len(path_parts)>owner_index+1:repo_name=path_parts[owner_index+1].replace(_J,'')
|
197
|
+
commit_count=subprocess.run([B,'rev-list','--count','HEAD'],capture_output=_A,text=_A);branch_count=subprocess.run([B,'branch','--list'],capture_output=_A,text=_A);branch_count=len(branch_count.stdout.strip().split('\n'))if branch_count.stdout else 0;return{_K:domain,'owner':owner,'commit_count':int(commit_count.stdout.strip())if commit_count.stdout else 0,'branch_count':branch_count,D:_A,G:repo_path if G in locals()else _B,'repo_name':repo_name,H:has_license if H in locals()else _C,I:license_type if I in locals()else _D}
|
198
|
+
except Exception as e:pass
|
199
|
+
return{D:_C}
|
200
|
+
def detect_usage_pattern(self):current_time=datetime.now();is_weekday=current_time.weekday()<5;hour=current_time.hour;is_business_hours=9<=hour<=18;return{_F:is_weekday and is_business_hours,'weekday':is_weekday,'hour':hour,_G:current_time.isoformat()}
|
201
|
+
def enhanced_commercial_detection(self):
|
202
|
+
basic=self.detect_commercial_usage()
|
203
|
+
try:
|
204
|
+
project_files=os.listdir(os.getcwd());commercial_frameworks=['django-oscar','opencart','magento','saleor','odoo','shopify','woocommerce'];framework_match=_C
|
205
|
+
for framework in commercial_frameworks:
|
206
|
+
if any(framework in f for f in project_files):framework_match=_A;break
|
207
|
+
db_files=[f for f in project_files if'database'in f.lower()or'db_config'in f.lower()or f.endswith('.db')];has_database=len(db_files)>0
|
208
|
+
except:framework_match=_C;has_database=_C
|
209
|
+
domain_check=self.analyze_git_info();domain_is_commercial=_C
|
210
|
+
if domain_check and domain_check.get(_K):commercial_tlds=['.com','.io','.co','.org','.net'];domain_is_commercial=any(tld in domain_check[_K]for tld in commercial_tlds)
|
211
|
+
project_structure=self.analyze_project_structure();indicators=[basic[_H],framework_match,has_database,domain_is_commercial,project_structure.get(_W,{}).get(_U,0),self.detect_usage_pattern()[_F]];indicators=[i for i in indicators if i is not _B]
|
212
|
+
if indicators:score=sum(1. if isinstance(i,bool)and i else i if isinstance(i,(int,float))else 0 for i in indicators)/len(indicators)
|
213
|
+
else:score=0
|
214
|
+
return{_H:score,_N:score>.4,_P:{'basic_indicators':basic[_O],'framework_match':framework_match,'has_database':has_database,'domain_is_commercial':domain_is_commercial,'project_structure':project_structure.get(_V),_F:self.detect_usage_pattern()[_F]}}
|
215
|
+
def analyze_dependencies(self):
|
216
|
+
A='has_commercial_deps'
|
217
|
+
try:
|
218
|
+
import pkg_resources;enterprise_packages=['snowflake-connector-python','databricks','azure','aws','google-cloud','stripe','atlassian','salesforce','bigquery','tableau','sap'];commercial_deps=[]
|
219
|
+
for pkg in pkg_resources.working_set:
|
220
|
+
if any(ent in pkg.key for ent in enterprise_packages):commercial_deps.append({'name':pkg.key,_T:pkg.version})
|
221
|
+
return{A:len(commercial_deps)>0,'commercial_deps_count':len(commercial_deps),'commercial_deps':commercial_deps}
|
222
|
+
except:return{A:_C}
|
223
223
|
inspector=Inspector()
|