4
4
from pathlib import Path
5
5
from typing import Dict , List , Optional , Union
6
6
7
- from pydantic import validator
7
+ from pydantic import field_validator
8
8
9
9
from johnsnowlabs import settings
10
10
from johnsnowlabs .abstract_base .pydantic_model import WritableBaseModel
@@ -62,17 +62,17 @@ class JslSecrets(WritableBaseModel):
62
62
methods for reading/storing found_secrets and managing .jslhome folder
63
63
"""
64
64
65
- HC_SECRET : Secret = None
66
- HC_LICENSE : Secret = None
65
+ HC_SECRET : Optional [ str ] = None
66
+ HC_LICENSE : Optional [ str ] = None
67
67
HC_VERSION : Optional [LibVersionIdentifier ] = None
68
- OCR_SECRET : Secret = None
69
- OCR_LICENSE : Secret = None
68
+ OCR_SECRET : Optional [ str ] = None
69
+ OCR_LICENSE : Optional [ str ] = None
70
70
OCR_VERSION : Optional [LibVersionIdentifier ] = None
71
- AWS_ACCESS_KEY_ID : Secret = None
72
- AWS_SECRET_ACCESS_KEY : Secret = None
71
+ AWS_ACCESS_KEY_ID : Optional [ str ] = None
72
+ AWS_SECRET_ACCESS_KEY : Optional [ str ] = None
73
73
NLP_VERSION : Optional [LibVersionIdentifier ] = None
74
- JSL_LEGAL_LICENSE : Secret = None
75
- JSL_FINANCE_LICENSE : Secret = None
74
+ JSL_LEGAL_LICENSE : Optional [ str ] = None
75
+ JSL_FINANCE_LICENSE : Optional [ str ] = None
76
76
77
77
@staticmethod
78
78
def raise_invalid_version ():
@@ -82,7 +82,7 @@ def raise_invalid_version():
82
82
)
83
83
raise ValueError ("Invalid secrets" )
84
84
85
- @validator ("HC_SECRET" )
85
+ @field_validator ("HC_SECRET" )
86
86
def hc_version_check (cls , HC_SECRET ):
87
87
global hc_validation_logged
88
88
try :
@@ -114,7 +114,7 @@ def is_ocr_secret_correct_version(ocr_secret: Optional[str]) -> bool:
114
114
def is_hc_secret_correct_version (hc_secret : Optional [str ]) -> bool :
115
115
return hc_secret and hc_secret .split ("-" )[0 ] == settings .raw_version_medical
116
116
117
- @validator ("OCR_SECRET" )
117
+ @field_validator ("OCR_SECRET" )
118
118
def ocr_version_check (cls , OCR_SECRET ):
119
119
global ocr_validation_logged
120
120
try :
@@ -123,6 +123,8 @@ def ocr_version_check(cls, OCR_SECRET):
123
123
and not ocr_validation_logged
124
124
):
125
125
ocr_validation_logged = True
126
+ if not OCR_SECRET :
127
+ return OCR_SECRET
126
128
print (
127
129
f"🚨 Outdated OCR Secrets in license file. Version={ (OCR_SECRET .split ('-' )[0 ] if OCR_SECRET else None )} but should be Version={ settings .raw_version_ocr } "
128
130
)
@@ -424,6 +426,13 @@ def search_env_vars() -> Union["JslSecrets", bool]:
424
426
]
425
427
):
426
428
print ("👌 License detected in Environment Variables" )
429
+ if isinstance (hc_version ,str ):
430
+ hc_version = LibVersionIdentifier (hc_version )
431
+ if isinstance (ocr_version ,str ):
432
+ ocr_version = LibVersionIdentifier (ocr_version )
433
+ if isinstance (nlp_version ,str ):
434
+ nlp_version = LibVersionIdentifier (nlp_version )
435
+
427
436
return JslSecrets (
428
437
HC_SECRET = hc_secret ,
429
438
HC_LICENSE = hc_license ,
@@ -631,6 +640,13 @@ def from_json_dict(secrets, secrets_metadata: Optional = None) -> "JslSecrets":
631
640
secrets ["JSL_FINANCE_LICENSE" ] if "JSL_FINANCE_LICENSE" in secrets else None
632
641
)
633
642
643
+ if isinstance (hc_version ,str ):
644
+ hc_version = LibVersionIdentifier (hc_version )
645
+ if isinstance (ocr_version ,str ):
646
+ ocr_version = LibVersionIdentifier (ocr_version )
647
+ if isinstance (nlp_version ,str ):
648
+ nlp_version = LibVersionIdentifier (nlp_version )
649
+
634
650
return JslSecrets (
635
651
HC_SECRET = hc_secret ,
636
652
HC_LICENSE = hc_license ,
@@ -659,8 +675,9 @@ def from_jsl_home(
659
675
return False
660
676
661
677
try :
678
+
662
679
# Try/Catch incase we get validation errors from outdated files
663
- license_infos = LicenseInfos .parse_file ( settings . creds_info_file )
680
+ license_infos = LicenseInfos .from_home ( )
664
681
if log and not already_logged :
665
682
already_logged = True
666
683
print (
@@ -692,7 +709,7 @@ def update_outdated_lib_secrets(
692
709
for license in os .listdir (settings .license_dir ):
693
710
if license == "info.json" :
694
711
continue
695
- secrets = JslSecrets .parse_file (os .path .join (settings .license_dir , license ))
712
+ secrets = JslSecrets .from_json_file_path (os .path .join (settings .license_dir , license ))
696
713
if (
697
714
secrets .HC_SECRET
698
715
and hc_secrets
@@ -768,7 +785,7 @@ def are_credentials_known(found_secrets: "JslSecrets") -> bool:
768
785
# Return True, if secrets are already stored in JSL-Home, otherwise False
769
786
Path (settings .py_dir ).mkdir (parents = True , exist_ok = True )
770
787
if os .path .exists (settings .creds_info_file ):
771
- license_infos = LicenseInfos .parse_file ( settings . creds_info_file )
788
+ license_infos = LicenseInfos .from_home ( )
772
789
else :
773
790
# If license dir did not exist yet, secrets are certainly new
774
791
return False
@@ -786,13 +803,13 @@ def are_lib_secrets_an_upgrade(found_secrets: "JslSecrets") -> bool:
786
803
# Return True, if lib are newer than existing ones, if yes upgrade locally stored secrets
787
804
Path (settings .py_dir ).mkdir (parents = True , exist_ok = True )
788
805
if os .path .exists (settings .creds_info_file ):
789
- license_infos = LicenseInfos .parse_file ( settings . creds_info_file )
806
+ license_infos = LicenseInfos .from_home ( )
790
807
else :
791
808
# If license dir did not exist yet, secrets are certainly new
792
809
return False
793
810
794
811
# if any stored secrets equal to found_secrets, then we already know them
795
- # check OCR secrets
812
+
796
813
if found_secrets .HC_SECRET :
797
814
if any (
798
815
map (
@@ -837,7 +854,7 @@ def store_in_jsl_home_if_new(secrets: "JslSecrets") -> None:
837
854
file_name = file_name + "_" .join (products ) + f".json"
838
855
839
856
if os .path .exists (settings .creds_info_file ):
840
- license_infos = LicenseInfos .parse_file ( settings . creds_info_file )
857
+ license_infos = LicenseInfos .from_home ( )
841
858
file_name = file_name .format (number = str (len (license_infos .infos )))
842
859
license_info = LicenseInfo (
843
860
jsl_secrets = secrets , products = products , id = str (len (license_infos .infos ))
@@ -848,13 +865,16 @@ def store_in_jsl_home_if_new(secrets: "JslSecrets") -> None:
848
865
secrets .write (out_dir )
849
866
print (f"📋 Stored new John Snow Labs License in { out_dir } " )
850
867
else :
851
- file_name = file_name .format (number = "0" )
852
868
license_info = LicenseInfo (jsl_secrets = secrets , products = products , id = "0" )
853
- LicenseInfos (infos = {file_name : license_info }).write (
854
- settings .creds_info_file
855
- )
869
+ license_infos = LicenseInfos (infos = {file_name : license_info })
870
+ with open (settings .creds_info_file , "w" ) as f :
871
+ f .write (license_infos .model_dump_json ())
872
+
873
+ file_name = file_name .format (number = "0" )
856
874
out_dir = os .path .join (settings .license_dir , file_name )
857
- secrets .write (out_dir )
875
+ with open (out_dir , "w" ) as f :
876
+ f .write (secrets .model_dump_json ())
877
+ #secrets.write(out_dir)
858
878
print (f"📋 Stored John Snow Labs License in { out_dir } " )
859
879
# We might load again JSL-Secrets from local
860
880
already_logged = True
@@ -877,6 +897,7 @@ class LicenseInfo(WritableBaseModel):
877
897
products : List [ProductName ]
878
898
879
899
900
+
880
901
class LicenseInfos (WritableBaseModel ):
881
902
"""Representation of a LicenseInfo in ~/.johnsnowlabs/licenses/info.json
882
903
Maps file_name to LicenseInfo
@@ -886,6 +907,15 @@ class LicenseInfos(WritableBaseModel):
886
907
887
908
@staticmethod
888
909
def from_home () -> Optional ["LicenseInfos" ]:
889
- if os .path .exists (settings .creds_info_file ):
890
- return LicenseInfos .parse_file (settings .creds_info_file )
891
- return None
910
+ if not os .path .exists (settings .creds_info_file ):
911
+ return None
912
+ data = json .load (open (settings .creds_info_file ))
913
+ infos = {}
914
+ for info in data ['infos' ]:
915
+ secret = JslSecrets .from_json_dict (data ['infos' ][info ]['jsl_secrets' ])
916
+ i = LicenseInfo (id = info , jsl_secrets = secret ,
917
+ products = data ['infos' ][info ]['products' ],
918
+ )
919
+ infos [info ] = i
920
+ license_infos = LicenseInfos (infos = infos )
921
+ return license_infos
0 commit comments