22import pickle
33from enum import Enum
44from typing import Any
5-
65import utils
76from push_file_to_lz import push_file_to_lz , get_file_from_lz , delete_file_from_lz
87
@@ -23,25 +22,48 @@ class FileType(Enum):
2322}
2423
2524
25+ # def read_from_file(table_name: str, file_name: str, file_type: FileType):
26+ # table_path = utils.get_table_dir(table_name)
27+ # file_full_path = os.path.join(table_path, file_name)
28+ # print("File path ; ", file_full_path)
29+ # # always read from LZ first
30+ # if get_file_from_lz(table_name, file_name) : # and os.path.exists(file_full_path):
31+ # with open(
32+ # file_full_path, FILETYPE_TO_READ_MODE_MAP.get(file_type, "r")
33+ # ) as file:
34+ # if file_type == FileType.PICKLE:
35+ # obj = pickle.load(file)
36+ # elif file_type == FileType.TEXT:
37+ # obj = file.read()
38+ # else:
39+ # obj = None
40+ # return obj
41+ # else:
42+ # return None
43+
44+
2645def read_from_file (table_name : str , file_name : str , file_type : FileType ):
2746 table_path = utils .get_table_dir (table_name )
2847 file_full_path = os .path .join (table_path , file_name )
2948 # always read from LZ first
30- if get_file_from_lz (table_name , file_name ) and os .path .exists (file_full_path ):
31- with open (
32- file_full_path , FILETYPE_TO_READ_MODE_MAP .get (file_type , "r" )
33- ) as file :
34- if file_type == FileType .PICKLE :
35- obj = pickle .load (file )
36- elif file_type == FileType .TEXT :
37- obj = file .read ()
38- else :
39- obj = None
49+ response_status_code , file_content = get_file_from_lz (table_name , file_name )
50+ if response_status_code == 200 :
51+ if file_type == FileType .PICKLE :
52+ obj = pickle .loads (file_content .content )
53+ print ("Type of object: " , isinstance (obj , bytes ))
54+ # Check if the result is itself a pickled object (nested)
55+ if isinstance (obj , bytes ):
56+ obj = pickle .loads (obj )
57+ print ("Unpickled object: " , obj )
4058 return obj
59+
60+ elif file_type == FileType .TEXT :
61+ return file_content .content .decode ('utf-8' )
62+ else :
63+ return None
4164 else :
4265 return None
4366
44-
4567def write_to_file (obj : Any , table_name : str , file_name : str , file_type : FileType ):
4668 table_path = utils .get_table_dir (table_name )
4769 file_full_path = os .path .join (table_path , file_name )
@@ -54,6 +76,25 @@ def write_to_file(obj: Any, table_name: str, file_name: str, file_type: FileType
5476 push_file_to_lz (file_full_path , table_name )
5577
5678
79+ def append_to_file (obj : Any , table_name : str , file_name : str , file_type : FileType ):
80+ table_path = utils .get_table_dir (table_name )
81+ file_full_path = os .path .join (table_path , file_name )
82+ append_mode = "ab" if file_type == FileType .PICKLE else "a"
83+
84+ # Create file if it doesn't exist
85+ if not os .path .exists (file_full_path ):
86+ with open (file_full_path , FILETYPE_TO_WRITE_MODE_MAP .get (file_type , "w" )) as f :
87+ f .write (f"\n { 'Column Name' :<20} | { 'Original Value' :<20} | { 'Converting Value' :<20} \n { '-' * 70 } \n " )
88+
89+ with open (file_full_path , append_mode ) as file :
90+ if file_type == FileType .PICKLE :
91+ pickle .dump (obj , file )
92+ elif file_type == FileType .TEXT :
93+ file .write (obj )
94+ # write to LZ
95+ # push_file_to_lz(file_full_path, table_name)
96+
97+
5798def delete_file (table_name : str , file_name : str ):
5899 file_full_path = os .path .join (utils .get_table_dir (table_name ), file_name )
59100 os .remove (file_full_path )
0 commit comments