labimotion 2.0.0.rc2 → 2.0.0.rc6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,169 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'time'
5
+ require 'zip'
6
+ require 'labimotion/constants'
7
+
8
+ module Labimotion
9
+ class MapperUtils
10
+ class << self
11
+ def load_config(config_json)
12
+ JSON.parse(config_json)
13
+ rescue JSON::ParserError => e
14
+ Rails.logger.error "Error parsing JSON: #{e.message}"
15
+ nil
16
+ rescue Errno::ENOENT => e
17
+ Rails.logger.error "Config file not found at #{Constants::Mapper::NMR_CONFIG}: #{e.message}"
18
+ nil
19
+ rescue StandardError => e
20
+ Rails.logger.error "Unexpected error loading config: #{e.message}"
21
+ nil
22
+ end
23
+
24
+ def load_brucker_config
25
+ config = load_config(File.read(Constants::Mapper::NMR_CONFIG))
26
+ return if config.nil? || config['sourceMap'].nil?
27
+
28
+ source_selector = config['sourceMap']['sourceSelector']
29
+ return if source_selector.blank?
30
+
31
+ parameters = config['sourceMap']['parameters']
32
+ return if parameters.blank?
33
+
34
+ config
35
+ end
36
+
37
+ def extract_data_from_zip(zip_file_url, source_map)
38
+ return nil if zip_file_url.nil?
39
+
40
+ process_zip_file(zip_file_url, source_map)
41
+ rescue Zip::Error => e
42
+ Rails.logger.error "Zip file error: #{e.message}"
43
+ nil
44
+ rescue StandardError => e
45
+ Rails.logger.error "Unexpected error extracting metadata: #{e.message}"
46
+ nil
47
+ end
48
+
49
+ def extract_parameters(file_content, parameter_names)
50
+ return nil if file_content.blank? || parameter_names.blank?
51
+
52
+ patterns = {
53
+ standard: build_parameter_pattern(parameter_names, :standard),
54
+ parm: build_parameter_pattern(parameter_names, :parm)
55
+ }
56
+
57
+ extracted_parameters = {}
58
+ begin
59
+ file_content.each_line do |line|
60
+ if (match = match_parameter(line, patterns))
61
+ value = clean_value(match[:value])
62
+ extracted_parameters[match[:param_name]] = value
63
+ end
64
+ end
65
+ rescue StandardError => e
66
+ Rails.logger.error "Error reading file content: #{e.message}"
67
+ return nil
68
+ end
69
+ extracted_parameters.compact_blank!
70
+ extracted_parameters
71
+ end
72
+
73
+ def format_timestamp(timestamp_str, give_format = nil)
74
+ return nil if timestamp_str.blank?
75
+
76
+ begin
77
+ timestamp = Integer(timestamp_str)
78
+ time_object = Time.at(timestamp).in_time_zone(Constants::DateTime::TIME_ZONE)
79
+ case give_format
80
+ when 'date'
81
+ time_object.strftime(Constants::DateTime::DATE_FORMAT)
82
+ when 'time'
83
+ time_object.strftime(Constants::DateTime::TIME_FORMAT)
84
+ else
85
+ time_object.strftime(Constants::DateTime::DATETIME_FORMAT)
86
+ end
87
+ rescue ArgumentError, TypeError => e
88
+ Rails.logger.error "Error parsing timestamp '#{timestamp_str}': #{e.message}"
89
+ nil
90
+ end
91
+ end
92
+
93
+ private
94
+
95
+ def build_parameter_pattern(parameter_names, format)
96
+ pattern = case format
97
+ when :standard
98
+ '^\\s*##?\\s*[$]?(?<param_name>%s)\\s*(?:=\\s*)?(?<value>.*?)\\s*$'
99
+ when :parm
100
+ '^\\s*(?<param_name>%s)\\s+(?<value>[^\\s].*?)(?:\\s+[A-Za-z]+)?\\s*$'
101
+ end
102
+
103
+ param_regex = parameter_names.map { |p| "\\b#{Regexp.escape(p)}\\b" }.join('|')
104
+ Regexp.new(pattern % param_regex)
105
+ end
106
+
107
+ def match_parameter(line, patterns)
108
+ patterns.each_value do |pattern|
109
+ match = line.match(pattern)
110
+ return match if match
111
+ end
112
+ nil
113
+ end
114
+
115
+ def clean_value(value)
116
+ value = value.strip
117
+ value = value[1..-2].strip if value.start_with?('<') && value.end_with?('>')
118
+ value
119
+ end
120
+
121
+ def process_zip_file(zip_file_url, source_map)
122
+ final_parameters = {}
123
+
124
+ Zip::File.open(zip_file_url) do |zip_file|
125
+ source_map['sourceSelector'].each do |source_name|
126
+ process_source(zip_file, source_map[source_name], final_parameters)
127
+ end
128
+ end
129
+
130
+ return { is_bagit: false, metadata: final_parameters } if final_parameters.present?
131
+
132
+ nil
133
+ end
134
+
135
+ def process_source(zip_file, source_config, final_parameters)
136
+ return if invalid_source_config?(source_config)
137
+
138
+ zip_file.each do |entry|
139
+ if source_file?(entry, source_config)
140
+ process_file_entry(entry, source_config['parameters'], final_parameters)
141
+ elsif bagit_metadata_file?(entry)
142
+ return { is_bagit: true, metadata: nil }
143
+ end
144
+ end
145
+ end
146
+
147
+ def process_file_entry(entry, parameters, final_parameters)
148
+ file_content = entry.get_input_stream.read.force_encoding(Constants::File::ENCODING)
149
+ extracted_parameters = extract_parameters(file_content, parameters)
150
+ final_parameters.merge!(extracted_parameters) if extracted_parameters.present?
151
+ end
152
+
153
+ def invalid_source_config?(source_config)
154
+ source_config.nil? ||
155
+ source_config['file'].nil? ||
156
+ source_config['parameters'].nil?
157
+ end
158
+
159
+ def source_file?(entry, source_config)
160
+ entry.name.include?(source_config['file'])
161
+ end
162
+
163
+ def bagit_metadata_file?(entry)
164
+ entry.name.include?('metadata/') &&
165
+ entry.name.include?('converter.json')
166
+ end
167
+ end
168
+ end
169
+ end
@@ -80,6 +80,28 @@ module Labimotion
80
80
  current_version
81
81
  end
82
82
 
83
+ def self.find_field(properties, field_path, separator = '.')
84
+ return if properties.nil? || field_path.nil?
85
+
86
+ layer_name, field_name = field_path.split(separator)
87
+ fields = properties.dig(Labimotion::Prop::LAYERS, layer_name, Labimotion::Prop::FIELDS)
88
+ return unless fields
89
+
90
+ fields.find { |f| f['field'] == field_name }
91
+ end
92
+
93
+ def self.find_options_val(field, properties)
94
+ return if field.nil? || properties.nil? || field['option_layers'].nil? || field['value'].nil?
95
+
96
+ option_layers = properties.dig(Labimotion::Prop::SEL_OPTIONS, field['option_layers'])
97
+ options = option_layers && option_layers['options']
98
+ return if options.nil?
99
+
100
+ sel_option = options.find { |o| o['key'] = field['value'] }
101
+ sel_option && sel_option['label']
102
+
103
+ end
104
+
83
105
  def self.pkg(pkg)
84
106
  pkg = {} if pkg.nil?
85
107
  pkg['eln'] = Chemotion::Application.config.version
@@ -2,5 +2,5 @@
2
2
 
3
3
  ## Labimotion Version
4
4
  module Labimotion
5
- VERSION = '2.0.0.rc2'
5
+ VERSION = '2.0.0.rc6'
6
6
  end
data/lib/labimotion.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  # In your_gem_name.rb or main Ruby file
2
2
  module Labimotion
3
-
4
3
  autoload :CONF, 'labimotion/conf'
5
4
  autoload :VERSION, 'labimotion/version'
5
+ autoload :Constants, 'labimotion/constants'
6
6
 
7
7
  def self.logger
8
8
  @@labimotion_logger ||= Logger.new(Rails.root.join('log/labimotion.log')) # rubocop:disable Style/ClassVars
@@ -13,6 +13,7 @@ module Labimotion
13
13
  Labimotion.logger.error(exception.backtrace.join("\n"))
14
14
  end
15
15
 
16
+ autoload :MapperUtils, 'labimotion/utils/mapper_utils'
16
17
  autoload :Utils, 'labimotion/utils/utils'
17
18
 
18
19
  ######## APIs
@@ -62,6 +63,7 @@ module Labimotion
62
63
 
63
64
  ######## Libs
64
65
  autoload :Converter, 'labimotion/libs/converter'
66
+ autoload :DatasetBuilder, 'labimotion/libs/dataset_builder'
65
67
  autoload :NmrMapper, 'labimotion/libs/nmr_mapper'
66
68
  autoload :NmrMapperRepo, 'labimotion/libs/nmr_mapper_repo' ## for Chemotion Repository
67
69
  autoload :TemplateHub, 'labimotion/libs/template_hub'
@@ -114,6 +116,4 @@ module Labimotion
114
116
  autoload :Datasetable, 'labimotion/models/concerns/datasetable'
115
117
  autoload :AttachmentConverter, 'labimotion/models/concerns/attachment_converter.rb'
116
118
  autoload :LinkedProperties, 'labimotion/models/concerns/linked_properties'
117
-
118
-
119
119
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: labimotion
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.rc2
4
+ version: 2.0.0.rc6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chia-Lin Lin
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2024-12-18 00:00:00.000000000 Z
12
+ date: 2025-02-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -47,6 +47,7 @@ files:
47
47
  - lib/labimotion/collection/export.rb
48
48
  - lib/labimotion/collection/import.rb
49
49
  - lib/labimotion/conf.rb
50
+ - lib/labimotion/constants.rb
50
51
  - lib/labimotion/entities/application_entity.rb
51
52
  - lib/labimotion/entities/dataset_entity.rb
52
53
  - lib/labimotion/entities/dataset_klass_entity.rb
@@ -75,8 +76,11 @@ files:
75
76
  - lib/labimotion/helpers/vocabulary_helpers.rb
76
77
  - lib/labimotion/libs/attachment_handler.rb
77
78
  - lib/labimotion/libs/converter.rb
79
+ - lib/labimotion/libs/data/mapper/Chemwiki.json
80
+ - lib/labimotion/libs/data/mapper/Source.json
78
81
  - lib/labimotion/libs/data/vocab/Standard.json
79
82
  - lib/labimotion/libs/data/vocab/System.json
83
+ - lib/labimotion/libs/dataset_builder.rb
80
84
  - lib/labimotion/libs/export_dataset.rb
81
85
  - lib/labimotion/libs/export_element.rb
82
86
  - lib/labimotion/libs/nmr_mapper.rb
@@ -114,6 +118,7 @@ files:
114
118
  - lib/labimotion/utils/export_utils.rb
115
119
  - lib/labimotion/utils/field_type.rb
116
120
  - lib/labimotion/utils/import_utils.rb
121
+ - lib/labimotion/utils/mapper_utils.rb
117
122
  - lib/labimotion/utils/prop.rb
118
123
  - lib/labimotion/utils/search.rb
119
124
  - lib/labimotion/utils/serializer.rb