22from pathlib import Path
33
44import pandas as pd
5- from lir .aggregation import Aggregation , ContextAwareDict , config_parser , pop_field
5+ from lir .aggregation import Aggregation , ContextAwareDict , config_parser , partial , pop_field
6+ from lir .util import check_type
67
78
89class CopyCSV (Aggregation ):
@@ -11,20 +12,20 @@ class CopyCSV(Aggregation):
1112 Attributes
1213 ----------
1314 source_file (str): The path to the source CSV file that should be copied.
14- target_path (str): The directory where the new CSV file will be saved. Given by the config_parser, meaning
15+ target_dir (str): The directory where the new CSV file will be saved. Given by the config_parser, meaning
1516 it is not set by the user in the configuration.
1617 columns (list[str]): A list of column names to copy from the source CSV. If empty, all columns will be copied.
1718 new_file_name (str): The name of the new CSV file. If empty, the original file name will be used.
1819 """
1920
20- def __init__ (self , source_file : str , target_path : str , columns : list [str ], new_file_name : str ):
21+ def __init__ (self , source_file : str , target_dir : str , columns : list [str ], new_file_name : str | None ):
2122 self .source_file = Path (source_file )
22- self .target_path = Path (target_path )
23+ self .target_dir = Path (target_dir )
2324 self .columns = columns
24- if new_file_name == "" :
25- self .new_file_name = self .target_path / self .source_file .name
25+ if new_file_name is None :
26+ self .new_file_name = self .target_dir / self .source_file .name
2627 else :
27- self .new_file_name = self .target_path / new_file_name
28+ self .new_file_name = self .target_dir / new_file_name
2829
2930 def report (self , data ) -> None :
3031 """Do nothing. Required by parent class."""
@@ -47,6 +48,6 @@ def close(self):
4748def copy_csv (config : ContextAwareDict , output_dir : str ) -> CopyCSV :
4849 """Parse the configuration for the CopyCSV aggregation and return an instance of it."""
4950 source_file = pop_field (config , "file" )
50- columns = pop_field (config , "columns" , default = [])
51- new_file_name = pop_field (config , "new_file_name" , default = "" )
51+ columns = pop_field (config , "columns" , default = [], validate = partial ( check_type , list ) )
52+ new_file_name = pop_field (config , "new_file_name" , required = False )
5253 return CopyCSV (source_file , output_dir , columns = columns , new_file_name = new_file_name )
0 commit comments