Skip to content

Commit

Permalink
Merge pull request #1634 from tripal/tv4g4-1626-importerFileDescriptions
Browse files Browse the repository at this point in the history
Improve importer file descriptions (location + content)
  • Loading branch information
laceysanderson committed Sep 14, 2023
2 parents c34166d + a25e7f4 commit 4f81c10
Show file tree
Hide file tree
Showing 19 changed files with 1,658 additions and 302 deletions.
6 changes: 5 additions & 1 deletion tripal/src/Form/TripalImporterForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ public function buildForm(array $form, FormStateInterface $form_state, $plugin_i
$form['file'] = [
'#type' => 'fieldset',
'#title' => $importer_def['upload_title'],
'#description' => $importer_def['upload_description'],
'#weight' => -15,
];

$form['file']['upload_description'] = [
'#type' => 'markup',
'#markup' => $importer_def['upload_description'] . ' The following file extensions are supported: ' . implode(', ', $importer_def['file_types']) . '.',
];
}

if (array_key_exists('file_upload', $importer_def) and $importer_def['file_upload'] == TRUE) {
Expand Down
39 changes: 27 additions & 12 deletions tripal/src/TripalImporter/TripalImporterBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ public function setJob($job) {
* -file_remote: provides the remote URL for the file.
* This argument is optional if the loader does not use the built-in
* file loader.
*
* @return int
* Returns the import_id.
*/
public function create($run_args, $file_details = []) {

Expand Down Expand Up @@ -245,6 +246,7 @@ public function create($run_args, $file_details = []) {
->execute();

$this->import_id = $import_id;
return $import_id;
}
catch (\Exception $e) {
throw new \Exception('Cannot create importer: ' . $e->getMessage());
Expand Down Expand Up @@ -317,12 +319,6 @@ public function submitJob() {
*/
public function prepareFiles() {

// If no file is required then just indicate that all is good to go.
if ($this->plugin_definition['file_required'] == FALSE) {
$this->is_prepared = TRUE;
return;
}

try {
for ($i = 0; $i < count($this->arguments['files']); $i++) {
if (!empty($this->arguments['files'][$i]['file_remote'])) {
Expand All @@ -336,7 +332,7 @@ public function prepareFiles() {
$ext = '.gz';
}
// Create a temporary file.
$temp = tempnam("temporary://", 'import_') . $ext;
$temp = \Drupal::service('file_system')->tempnam("temporary://", 'import_') . $ext;
$this->logger->notice('Saving as: %file', ['%file' => $temp]);

$url_fh = fopen($file_remote, "r");
Expand Down Expand Up @@ -388,6 +384,13 @@ public function prepareFiles() {
catch (\Exception $e) {
throw new \Exception('Cannot prepare the importer: ' . $e->getMessage());
}


// If we get here and no exception has been thrown then either
// 1) files were added but none needed to be prepared.
// 2) files were not added (check for files being required happens elsewhere).
$this->is_prepared = TRUE;

}

/**
Expand Down Expand Up @@ -464,10 +467,6 @@ protected function setItemsHandled($total_handled) {
$percent = ($this->num_handled / $this->total_items) * 100;
$ipercent = (int) $percent;
}
else {
$percent = 0;
$ipercent = 0;
}

// If we've reached our interval then print update info.
if ($ipercent > 0 and $ipercent != $this->reported and $ipercent % $this->interval == 0) {
Expand All @@ -484,6 +483,22 @@ protected function setItemsHandled($total_handled) {
}
$this->reported = $ipercent;
}

// If we're done then indicate so.
if ($this->num_handled >= $this->total_items) {
$memory = number_format(memory_get_usage());
$spercent = sprintf("%.2f", 100);
$this->logger->notice(
t("Percent complete: " . $spercent . " %. Memory: " . $memory . " bytes.")
. "\r"
);

// If we have a job the update the job progress too.
if ($this->job) {
$this->job->setProgress(100);
}
$this->reported = 100;
}
}

/**
Expand Down
Binary file not shown.
104 changes: 0 additions & 104 deletions tripal/tests/src/Functional/TripalImporterTest.php

This file was deleted.

0 comments on commit 4f81c10

Please sign in to comment.