11import pandas as pd
2- import numpy as np
32from pathlib import Path
43import lib .common as cmn
54from lib .bigFileWriter import BigFileWriter
109import logging
1110import gc
1211import time
13- from datetime import datetime
12+ from datetime import datetime , date
1413import lib .zipping as zp
1514
1615class ConversionManager (SystemManager ):
@@ -20,12 +19,23 @@ def __init__(self, dataDir: Path, datasetID: str, location: str, database: str,
2019 super ().__init__ (dataDir .parent , self .stepName , "tasks" )
2120
2221 self .conversionDir = dataDir / self .stepName
23- self . location = location
22+
2423 self .datasetID = datasetID
24+ self .location = location
25+ self .database = database
26+ self .subsection = subsection
2527
26- self .output = StackedFile (self .conversionDir / (f"{ location } -{ database } " + (f"-{ subsection } " if subsection else "" )))
28+ self .file = None
29+
30+ def _generateFileName (self , withTimestamp : bool ) -> StackedFile :
31+ sourceName = f"{ self .location } -{ self .database } "
32+ if self .subsection :
33+ sourceName += f"-{ self .subsection } "
34+
35+ if withTimestamp :
36+ sourceName += date .today ().strftime ("-%Y-%m-%d" )
2737
28- self .fileLoaded = False
38+ return StackedFile ( self .conversionDir / sourceName )
2939
3040 def loadFile (self , file : File , properties : dict , mapDir : Path ) -> None :
3141 self .file = file
@@ -40,13 +50,16 @@ def loadFile(self, file: File, properties: dict, mapDir: Path) -> None:
4050 self .skipRemap = properties .pop ("skipRemap" , [])
4151 self .preserveDwC = properties .pop ("preserveDwC" , False )
4252 self .prefixUnmapped = properties .pop ("prefixUnmapped" , True )
53+
54+ timestamp = properties .pop ("timestamp" , True )
55+ self .output = self ._generateFileName (timestamp )
56+
4357 self .augments = [FunctionScript (self .baseDir , augProperties ) for augProperties in properties .pop ("augment" , [])]
4458
4559 self .remapper = Remapper (mapDir , self .mapID , self .customMapID , self .customMapPath , self .location , self .preserveDwC , self .prefixUnmapped )
46- self .fileLoaded = True
4760
4861 def convert (self , overwrite : bool = False , verbose : bool = True , ignoreRemapErrors : bool = True , forceRetrieve : bool = False ) -> bool :
49- if not self .fileLoaded :
62+ if self .file is None :
5063 logging .error ("No file loaded for conversion, exiting..." )
5164 return False
5265
@@ -145,8 +158,14 @@ def applyAugments(self, df: pd.DataFrame) -> pd.DataFrame | None:
145158
146159 return df
147160
148- def package (self , compressLocation : Path ) -> Path :
149- renamedFile = self .metadataPath .rename (self .output .filePath / self .metadataPath .name )
150- outputPath = zp .compress (self .output .filePath , compressLocation )
161+ def package (self , compressLocation : Path ) -> Path | None :
162+ outputFileName = self .getMetadata (- 1 , Metadata .OUTPUT )
163+ if outputFileName is None :
164+ return
165+
166+ outputFilePath = self .conversionDir / outputFileName
167+ renamedFile = self .metadataPath .rename (outputFilePath / self .metadataPath .name )
168+ outputPath = zp .compress (outputFilePath , compressLocation )
151169 renamedFile .rename (self .metadataPath )
170+
152171 return outputPath
0 commit comments