55import re
66import zipfile
77from lxml import etree
8+ from io import BytesIO
89
910from lxml .etree import (
1011 Element ,
6162from etptypes .energistics .etp .v12 .protocol .discovery .get_resources import (
6263 GetResources ,
6364)
65+ from etptypes .energistics .etp .v12 .protocol .discovery .get_deleted_resources import (
66+ GetDeletedResources ,
67+ )
6468from etptypes .energistics .etp .v12 .protocol .store .put_data_objects import (
6569 PutDataObjects ,
6670)
6771from etptypes .energistics .etp .v12 .protocol .store .get_data_objects import (
6872 GetDataObjects ,
6973)
74+ from etptypes .energistics .etp .v12 .protocol .store .delete_data_objects import (
75+ DeleteDataObjects ,
76+ )
7077from etptypes .energistics .etp .v12 .datatypes .object .data_object import (
7178 DataObject ,
7279)
8895 GetDataArrayMetadata ,
8996)
9097
98+
99+ from etptypes .energistics .etp .v12 .protocol .supported_types .get_supported_types import (
100+ GetSupportedTypes ,
101+ )
102+ from etptypes .energistics .etp .v12 .protocol .supported_types .get_supported_types_response import (
103+ GetSupportedTypesResponse ,
104+ )
105+
91106from etptypes .energistics .etp .v12 .datatypes .data_value import DataValue
92107from etptypes .energistics .etp .v12 .datatypes .object .resource import Resource
93108from etptypes .energistics .etp .v12 .datatypes .object .dataspace import Dataspace
100115
101116from etpclient .etp .h5_handler import generate_put_data_arrays
102117
118+ from etpclient .utils import (
119+ xml_get_type ,
120+ get_xml_tree_string ,
121+ )
122+
103123
104124ENERGYML_NAMESPACES = {
105125 "eml" : "http://www.energistics.org/energyml/data/commonv2" ,
@@ -194,6 +214,16 @@ def find_uuid_in_xml(xml_content: bytes) -> str:
194214 return None
195215
196216
217+ def get_root_type_in_xml (xml_content : bytes ) -> str :
218+ try :
219+ tree = ElementTree (fromstring (xml_content ))
220+ root = tree .getroot ()
221+ return root .tag
222+ except etree .XMLSyntaxError :
223+ print ("Error reading xml" )
224+ return None
225+
226+
197227def request_session ():
198228 return RequestSession (
199229 applicationName = "Geosiris etp client" ,
@@ -288,7 +318,7 @@ def put_dataspace(dataspace_names: list):
288318 return PutDataspaces (dataspaces = ds_map )
289319
290320
291- def delete_dataspace (dataspace_names : str ):
321+ def delete_dataspace (dataspace_names : list ):
292322 ds_map = {}
293323 for ds_name in dataspace_names :
294324 ds_map [str (len (ds_map ))] = (
@@ -299,7 +329,36 @@ def delete_dataspace(dataspace_names: str):
299329 return DeleteDataspaces (uris = ds_map )
300330
301331
302- def put_data_object_by_path (path : str , dataspace_name : str = None ):
332+ def delete_data_object (uris : list ):
333+ print (
334+ "Sending delete_data_object : " , {i : uris [i ] for i in range (len (uris ))}
335+ )
336+ return DeleteDataObjects (
337+ uris = {i : uris [i ] for i in range (len (uris ))},
338+ prune_contained_objects = False ,
339+ )
340+
341+
342+ def get_deleted_resources (
343+ dataspace_names : str ,
344+ delete_time_filter : int = None ,
345+ data_object_types : list = [],
346+ ):
347+ ds_uri = (
348+ "eml:///dataspace('" + dataspace_names + "')"
349+ if "eml:///" not in dataspace_names
350+ else dataspace_names
351+ )
352+ return GetDeletedResources (
353+ dataspace_uri = ds_uri ,
354+ delete_time_filter = delete_time_filter ,
355+ data_object_types = data_object_types ,
356+ )
357+
358+
359+ def put_data_object_by_path (
360+ path : str , dataspace_name : str = None , uuids_filter : list = None
361+ ):
303362 result = []
304363 # try:
305364 if path .endswith (".xml" ):
@@ -317,9 +376,14 @@ def put_data_object_by_path(path: str, dataspace_name: str = None):
317376 # print('%s (%s --> %s)' % (zinfo.filename, zinfo.file_size, zinfo.compress_size))
318377 with zfile .open (zinfo .filename ) as myfile :
319378 file_content = myfile .read ()
320- if (
321- findUuid (zinfo .filename ) is not None
322- or find_uuid_in_xml (file_content ) is not None
379+ uuid = findUuid (zinfo .filename )
380+ if uuid is None :
381+ uuid = find_uuid_in_xml (file_content )
382+ print (f"UUID { uuid } " )
383+ if uuid is not None and (
384+ uuids_filter is None
385+ or len (uuids_filter ) == 0
386+ or uuid in uuids_filter
323387 ):
324388 do_lst [len (do_lst )] = _create_data_object (
325389 file_content .decode ("utf-8" ),
@@ -388,6 +452,37 @@ def get_close_session(reason="We have finished"):
388452 return CloseSession (reason = reason )
389453
390454
455+ # _____ __ ________
456+ # / ___/__ ______ ____ ____ _____/ /____ ____/ /_ __/_ ______ ___ _____
457+ # \__ \/ / / / __ \/ __ \/ __ \/ ___/ __/ _ \/ __ / / / / / / / __ \/ _ \/ ___/
458+ # ___/ / /_/ / /_/ / /_/ / /_/ / / / /_/ __/ /_/ / / / / /_/ / /_/ / __(__ )
459+ # /____/\__,_/ .___/ .___/\____/_/ \__/\___/\__,_/ /_/ \__, / .___/\___/____/
460+ # /_/ /_/ /____/_/
461+
462+
463+ def get_supported_types (
464+ uri : str ,
465+ count : bool = True ,
466+ return_empty_types : bool = True ,
467+ scope : str = "self" ,
468+ ):
469+ if not uri .startswith ("eml:///" ):
470+ uri = f"eml:///dataspace('{ uri } ')"
471+ if isinstance (count , str ):
472+ count = count .lower () == "true"
473+ if isinstance (return_empty_types , str ):
474+ return_empty_types = return_empty_types .lower () == "true"
475+ print (
476+ f"==> uri={ uri } , count={ count } , return_empty_types={ return_empty_types } "
477+ )
478+ return GetSupportedTypes (
479+ uri = uri ,
480+ count_objects = count ,
481+ return_empty_types = return_empty_types ,
482+ scope = get_scope (scope ),
483+ )
484+
485+
391486# ____ __ ___
392487# / __ \____ _/ /_____ _/ | ______________ ___ __
393488# / / / / __ `/ __/ __ `/ /| | / ___/ ___/ __ `/ / / /
@@ -433,6 +528,7 @@ def put_data_array(
433528 h5_file_path : str ,
434529 dataspace_name : str ,
435530):
531+ print ("FILE " , epc_or_xml_file_path )
436532 result = []
437533 if epc_or_xml_file_path .endswith (".epc" ):
438534 zfile = zipfile .ZipFile (epc_or_xml_file_path , "r" )
@@ -447,13 +543,13 @@ def put_data_array(
447543 or len (uuids_filter ) == 0
448544 or uuid in uuids_filter
449545 ):
450- # print("Uuid : ", uuid)
451- with zfile .open (zinfo .filename ) as myfile :
452- result += generate_put_data_arrays (
453- myfile .read ().decode ("utf-8" ),
454- h5_file_path ,
455- dataspace_name ,
456- )
546+ print ("> Uuid filtered : " , uuid )
547+ # with zfile.open(zinfo.filename) as myfile:
548+ # result += generate_put_data_arrays(
549+ # myfile.read().decode("utf-8"),
550+ # h5_file_path,
551+ # dataspace_name,
552+ # )
457553 else :
458554 pass
459555 # print("Not imported ", uuid)
@@ -472,9 +568,10 @@ async def put_data_array_sender(
472568 epc_or_xml_file_path : str ,
473569 h5_file_path : str ,
474570 dataspace_name : str ,
571+ type_filter : str = None ,
475572):
476573 print (
477- f"uuids_filter : { uuids_filter } epc_or_xml_file_path : { epc_or_xml_file_path } h5_file_path : { h5_file_path } dataspace_name : { dataspace_name } "
574+ f"uuids_filter : { uuids_filter } epc_or_xml_file_path : { epc_or_xml_file_path } h5_file_path : { h5_file_path } dataspace_name : { dataspace_name } type_filter : { type_filter } "
478575 )
479576 if epc_or_xml_file_path .endswith (".epc" ):
480577 zfile = zipfile .ZipFile (epc_or_xml_file_path , "r" )
@@ -484,26 +581,37 @@ async def put_data_array_sender(
484581 and findUuid (zinfo .filename ) is not None
485582 ):
486583 uuid = findUuid (zinfo .filename )
487- if (
584+ accept_file = (
488585 uuids_filter is None
489586 or len (uuids_filter ) == 0
490587 or uuid in uuids_filter
491- ):
492- print ("Uuid : " , uuid )
588+ )
589+ if type_filter is not None :
590+ with zfile .open (zinfo .filename ) as myfile :
591+ file_content = myfile .read ()
592+ file_type = xml_get_type (
593+ get_xml_tree_string (file_content )
594+ )
595+ accept_file = accept_file or re .match (
596+ type_filter , file_type
597+ )
598+
599+ if accept_file :
600+ print (" > accept_file Uuid : " , uuid )
493601 with zfile .open (zinfo .filename ) as myfile :
494602 for pda in generate_put_data_arrays (
495603 myfile .read ().decode ("utf-8" ),
496604 h5_file_path ,
497605 dataspace_name ,
498606 ):
499- print (type (pda ), pda )
607+ # print(type(pda), pda)
500608 try :
501609 yield await websocket .send_no_wait (pda )
502610 except Exception as e :
503611 print ("ERROR : " , e )
504612 else :
613+ print ("Not imported " , uuid , " -- " , uuid in uuids_filter )
505614 pass
506- # print("Not imported ", uuid)
507615 zfile .close ()
508616 else :
509617 with open (epc_or_xml_file_path ) as f :
0 commit comments