1- #/usr/bin/env python3
1+ # /usr/bin/env python3
22import argparse
3- import sys
43import datetime as dt
4+ import sys
55from textwrap import dedent
66
77sys .path .append ("." )
88from query .tables import product_tags_list
99
10-
1110DESCRIPTION = """
1211Generate a script to export some data from openfoodfacts-query database,
1312starting from the event table.
2625
2726def generate_export_script (from_date , to_date , export_dir ):
2827
29- outputs = []
28+ outputs = []
3029
31- def cmd (sql ):
32- outputs .append (dedent (sql ))
30+ def cmd (sql ):
31+ outputs .append (dedent (sql ))
3332
34- cmd (
35- f"""
33+ cmd (
34+ f"""
3635 \\ ! mkdir -p { export_dir }
3736 -- enable writing by postgres process
3837 \\ ! chmod a+rwX { export_dir }
3938 \\ set export_dir '{ export_dir } '
4039 \\ set from_date '{ from_date } '
4140 \\ set to_date '{ to_date } '
4241 """
43- )
42+ )
4443
45- cmd (
46- """
44+ cmd (
45+ """
4746 \\ set export_path :export_dir/product_update_event.csv
4847
4948 COPY (
@@ -53,10 +52,10 @@ def cmd(sql):
5352 )
5453 TO :'export_path' DELIMITER ',' CSV HEADER;
5554 """
56- )
55+ )
5756
58- cmd (
59- """
57+ cmd (
58+ """
6059 \\ set export_path :export_dir/product_update.csv
6160
6261 COPY (
@@ -68,9 +67,9 @@ def cmd(sql):
6867 )
6968 TO :'export_path' DELIMITER ',' CSV HEADER;
7069 """
71- )
72- cmd (
73- """
70+ )
71+ cmd (
72+ """
7473 \\ set export_path :export_dir/product.csv
7574 COPY (
7675 select * from product
@@ -84,11 +83,11 @@ def cmd(sql):
8483 )
8584 TO :'export_path' DELIMITER ',' CSV HEADER;
8685 """
87- )
88- # should use product_tags.TAG_TABLES
89- for table_name in product_tags_list .TAG_TABLES .values ():
90- cmd (
91- f"""
86+ )
87+ # should use product_tags.TAG_TABLES
88+ for table_name in product_tags_list .TAG_TABLES .values ():
89+ cmd (
90+ f"""
9291 \\ set table_name { table_name }
9392 \\ set export_path :export_dir/:table_name.csv
9493
@@ -104,44 +103,49 @@ def cmd(sql):
104103 )
105104 TO :'export_path' DELIMITER ',' CSV HEADER;
106105 """
107- )
108- return outputs
106+ )
107+ return outputs
109108
110109
111110def get_parser ():
112- now = dt .datetime .now ()
113- default_start = (now - dt .timedelta (minutes = 20 )).strftime ("%Y-%m-%d %H:%M:%S" )
114- default_end = now .strftime ("%Y-%m-%d %H:%M:%S" )
115- default_folder = "/opt/data/exports/" + now .strftime ("%Y-%m-%d_%H:%M:%S" )
116- parser = argparse .ArgumentParser (description = DESCRIPTION )
117- parser .add_argument (
118- 'from_date' , default = default_start , type = str , nargs = "?" ,
119- help = (
120- "Start date in iso format, e.g. 2025-11-21 11:00:00\n " +
121- "If empty, it's now - 20 minutes"
111+ now = dt .datetime .now ()
112+ default_start = (now - dt .timedelta (minutes = 20 )).strftime ("%Y-%m-%d %H:%M:%S" )
113+ default_end = now .strftime ("%Y-%m-%d %H:%M:%S" )
114+ default_folder = "/opt/data/exports/" + now .strftime ("%Y-%m-%d_%H:%M:%S" )
115+ parser = argparse .ArgumentParser (description = DESCRIPTION )
116+ parser .add_argument (
117+ "from_date" ,
118+ default = default_start ,
119+ type = str ,
120+ nargs = "?" ,
121+ help = (
122+ "Start date in iso format, e.g. 2025-11-21 11:00:00\n "
123+ + "If empty, it's now - 20 minutes"
124+ ),
125+ )
126+ parser .add_argument (
127+ "to_date" ,
128+ default = default_end ,
129+ type = str ,
130+ nargs = "?" ,
131+ help = (
132+ "End date in iso format, e.g. 2025-11-21 11:20:00\n " + "If empty, it's now"
133+ ),
122134 )
123- )
124- parser .add_argument (
125- 'to_date' , default = default_end , type = str , nargs = "?" ,
126- help = (
127- "End date in iso format, e.g. 2025-11-21 11:20:00\n " +
128- "If empty, it's now"
135+ parser .add_argument (
136+ "--dest" ,
137+ default = default_folder ,
138+ type = str ,
139+ help = (
140+ "Target directory, it will be created if it does not exists,"
141+ + f"defaults to { default_folder } (according to current time)"
142+ ),
129143 )
130- )
131- parser .add_argument (
132- '--dest' , default = default_folder , type = str ,
133- help = (
134- "Target directory, it will be created if it does not exists," +
135- f"defaults to { default_folder } (according to current time)"
136- ),
137- )
138- return parser
144+ return parser
139145
140146
141147if __name__ == "__main__" :
142- parser = get_parser ()
143- args = parser .parse_args ()
144- outputs = generate_export_script (args .from_date , args .to_date , args .dest )
145- print ("\n " .join (outputs ))
146-
147-
148+ parser = get_parser ()
149+ args = parser .parse_args ()
150+ outputs = generate_export_script (args .from_date , args .to_date , args .dest )
151+ print ("\n " .join (outputs ))
0 commit comments