22# Used in the creation and editing of extract records. Used in conjunction with process tracking.
33from datetime import datetime
44import logging
5+ import os
56from pathlib import Path
67
78from sqlalchemy .orm import aliased
1112from process_tracker .utilities import utilities
1213from process_tracker .models .extract import (
1314 Extract ,
15+ ExtractCompressionType ,
16+ ExtractFileType ,
1417 ExtractDatasetType ,
1518 ExtractDependency ,
1619 ExtractProcess ,
@@ -27,6 +30,8 @@ def __init__(
2730 location_name = None ,
2831 location_path = None ,
2932 status = None ,
33+ compression_type = None ,
34+ filetype = None ,
3035 config_location = None ,
3136 ):
3237 """
@@ -44,6 +49,10 @@ def __init__(
4449 :type location_name: string
4550 :param status: Optional if status does not need to be 'initializing', which is default.
4651 :type status: string
52+ :param compression_type: Optional compression format of the extract.
53+ :type compression_type: String
54+ :param filetype: Optional file type of the extract. Will try to be derived from the filename if not provided.
55+ :type filetype: string
4756 :param config_location: Optional location for the process_tracker configuration file.
4857 :type config_location: string
4958 """
@@ -74,12 +83,54 @@ def __init__(
7483 else :
7584 raise Exception ("A location object or location_path must be provided." )
7685
86+ if compression_type is not None :
87+ self .logger .info ("Finding compression type." )
88+ try :
89+ self .compression_type = self .data_store .get_or_create_item (
90+ model = ExtractCompressionType ,
91+ create = False ,
92+ extract_compression_type = compression_type ,
93+ )
94+ except Exception :
95+ error_msg = "%s is not a valid compression type." % compression_type
96+ self .logger .error (error_msg )
97+ raise Exception (error_msg )
98+
99+ self .compression_type_id = self .compression_type .extract_compression_type_id
100+ else :
101+ self .compression_type_id = None
102+
103+ if filetype is not None :
104+ self .logger .info ("File type provided. Verifying it is a valid filetype." )
105+ try :
106+ self .filetype = self .data_store .get_or_create_item (
107+ model = ExtractFileType , create = False , extract_filetype = filetype
108+ )
109+ except Exception :
110+ error_msg = "%s is not a valid file type." % filetype
111+ self .logger .error (error_msg )
112+ raise Exception (error_msg )
113+ else :
114+ # Need to try to determine the filetype based on the extension of the filename.
115+ file_extension = os .path .splitext (filename )[1 ]
116+ file_extension = file_extension .replace ("." , "" )
117+ self .logger .info (
118+ "Trying to find record for file extension: %s" % file_extension
119+ )
120+ self .filetype = self .data_store .get_or_create_item (
121+ model = ExtractFileType ,
122+ create = False ,
123+ extract_filetype_code = file_extension ,
124+ )
125+
77126 self .logger .info ("Registering extract." )
78127
79128 self .extract = self .data_store .get_or_create_item (
80129 model = Extract ,
81130 extract_filename = filename ,
82131 extract_location_id = self .location .location .location_id ,
132+ extract_compression_type_id = self .compression_type_id ,
133+ extract_filetype_id = self .filetype .extract_filetype_id ,
83134 )
84135
85136 if location_path is not None :
0 commit comments