Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: more flexible exports #6483

Merged
merged 1 commit into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/ProductOpener/Config_off.pm
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,7 @@ $options{categories_exempted_from_nutrient_levels} = [qw(
stores
countries
ingredients_text
ingredients_tags
allergens
traces
serving_size
Expand Down
1 change: 1 addition & 0 deletions lib/ProductOpener/Export.pm
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ sub export_csv($) {
@sorted_populated_fields = sort ({ $populated_fields{$a} cmp $populated_fields{$b} } keys %populated_fields);

push @sorted_populated_fields, "data_sources";

if (not defined $populated_fields{"obsolete"}) {
# Always output the "obsolete" field so that obsolete products can be unobsoleted
push @sorted_populated_fields, "obsolete";
Expand Down
21 changes: 19 additions & 2 deletions scripts/export_csv_file.pl
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,16 @@

The --query-codes-from-file parameter allows to specify a file containing barcodes (one barcode per line).

--export-computed-fields : export fields such as Nutri-Score and NOVA fields that are computed by OFF

--export-canonicalized-tags-fields : export taxonomized fields in the main language of the product

Usage:

export_csv_file.pl --query field_name=field_value --query other_field_name=other_field_value
[--fields code,ingredients_texts_fr,categories_tags] [--extra_fields nova_group,nutrition_grade_fr]
[--include-images-paths] [--query-codes-from-file codes]

TXT
;

Expand All @@ -70,6 +75,8 @@
my $separator = "\t";
my $include_images_paths;
my $query_codes_from_file;
my $export_computed_fields;
my $export_canonicalized_tags_fields;

GetOptions (
"fields=s" => \$fields,
Expand All @@ -78,8 +85,10 @@
"separator=s" => \$separator,
"include-images-paths" => \$include_images_paths,
"query-codes-from-file=s" => \$query_codes_from_file,
)
or die("Error in command line arguments:\n$\nusage");
"export-computed-fields" => \$export_computed_fields,
"export-canonicalized-tags-fields" => \$export_canonicalized_tags_fields,
)
or die("Error in command line arguments:\n\n$usage");

print STDERR "export_csv_file.pl
- fields: $fields
Expand Down Expand Up @@ -132,6 +141,14 @@

my $args_ref = {filehandle=>*STDOUT, separator=>$separator, query=>$query_ref };

if ($export_computed_fields) {
$args_ref->{export_computed_fields} = 1;
}

if ($export_canonicalized_tags_fields) {
$args_ref->{export_canonicalized_tags_fields} = 1;
}

if ((defined $fields) and ($fields ne "")) {
$args_ref->{fields} = [split(/,/, $fields)];
}
Expand Down
16 changes: 15 additions & 1 deletion scripts/export_database.pl
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,21 @@ sub sanitize_field_content {

foreach my $field (@export_fields) {

my $field_value = ($product_ref->{$field} // "");
my $field_value;

# _tags field contain an array of values
if ($field =~ /_tags/) {
if (defined $product_ref->{$field}) {
$field_value = join(',', @{$product_ref->{$field}});
alexgarel marked this conversation as resolved.
Show resolved Hide resolved
}
else {
$field_value = "";
}
}
# other fields
else {
$field_value = ($product_ref->{$field} // "");
}

# Language specific field?
if ((defined $language_fields{$field}) and (defined $product_ref->{$field . "_" . $l}) and ($product_ref->{$field . "_" . $l} ne '')) {
Expand Down