1+ # Copyright (c) 2025, Salesforce, Inc.
2+ # SPDX-License-Identifier: Apache-2
3+ #
4+ # Licensed under the Apache License, Version 2.0 (the "License");
5+ # you may not use this file except in compliance with the License.
6+ # You may obtain a copy of the License at
7+ #
8+ # http://www.apache.org/licenses/LICENSE-2.0
9+ #
10+ # Unless required by applicable law or agreed to in writing, software
11+ # distributed under the License is distributed on an "AS IS" BASIS,
12+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ # See the License for the specific language governing permissions and
14+ # limitations under the License.
15+ from __future__ import annotations
16+
17+ import io
18+ import os
19+
20+ from abc import abstractmethod
21+ from pathlib import Path
22+ from datacustomcode .file .base import BaseDataAccessLayer
23+
24+ class BaseFileReader (BaseDataAccessLayer ):
25+ def file_open (self , file_name : str ) -> io .TextIOWrapper :
26+ default_code_pacakge = 'payload'
27+ file_folder = 'files' # hardcoded folder name
28+
29+ file_path = None
30+ if os .path .exists (default_code_pacakge ):
31+ relative = f"{ default_code_pacakge } /{ file_folder } /{ file_name } "
32+ file_path = Path .cwd ().joinpath (relative )
33+ else :
34+ # find config.json, files folder is right next to the config.json
35+ config_abs_path = Path (self .find_file_pathlib ("config.json" , Path .cwd ()))
36+ relative = f"{ file_folder } /{ file_name } "
37+ file_path = config_abs_path .parent .joinpath (relative )
38+
39+ file_handle = open (file_path , 'r' )
40+ #print(f"chuy file type: {type(foo), file_path}")
41+ return file_handle
42+
43+ def find_file_pathlib (self , filename , search_path ):
44+ """
45+ Finds a file within a directory and its subdirectories using pathlib.
46+ Returns the full path of the first match found, or None if not found.
47+ """
48+ path_obj = Path (search_path )
49+ for file_path in path_obj .rglob (filename ):
50+ return str (file_path ) # Convert Path object to string
51+ return None
0 commit comments