6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/localio/processors/google_drive_processor.rb', line 6
def self.load_localizables(platform_options, options)
spreadsheet = options[:spreadsheet]
raise ArgumentError, ':spreadsheet required for Google Drive source!' if spreadsheet.nil?
login = options[:login]
raise ArgumentError, ':login is deprecated. You should use :client_id and :client_secret for secure OAuth2 authentication.' unless login.nil?
password = options[:password]
raise ArgumentError, ':password is deprecated. You should use :client_id and :client_secret for secure OAuth2 authentication.' unless password.nil?
client_id = options[:client_id]
client_secret = options[:client_secret]
unless options[:client_token].is_a?(String) && File.file?(options[:client_token].to_s)
raise ArgumentError, ':client_id required for Google Drive. Check how to get it here: https://developers.google.com/drive/web/auth/web-server' if client_id.nil?
raise ArgumentError, ':client_secret required for Google Drive. Check how to get it here: https://developers.google.com/drive/web/auth/web-server' if client_secret.nil?
end
override_default = nil
override_default = platform_options[:override_default] unless platform_options.nil? or platform_options[:override_default].nil?
puts 'Logging in to Google Drive...'
begin
session = nil
if options[:client_token].is_a?(String) && File.file?(options[:client_token].to_s)
session = GoogleDrive::Session.from_service_account_key(options[:client_token])
else
config_path = File.join(Dir.home, '.localio_gdrive_config.json')
session = GoogleDrive::Session.from_config(
config_path,
client_id: client_id,
client_secret: client_secret
)
end
rescue => e
puts "Error: #{e.inspect}"
raise 'Couldn\'t access Google Drive. Check your values for :client_id and :client_secret.'
end
puts 'Logged in!'
matching_spreadsheets = []
session.spreadsheets.each do |s|
matching_spreadsheets << s if s.title.include? spreadsheet
end
case matching_spreadsheets.count
when 1
puts 'Spreadsheet found. Analyzing...'
when 0
abort "Unable to find any spreadsheet matching your criteria: #{spreadsheet}"
else
abort "More than one match found (#{matching_spreadsheets.join ', '}). You have to be more specific!"
end
sheet = options[:sheet]
worksheet = if sheet.is_a? Integer
matching_spreadsheets[0].worksheets[sheet]
elsif sheet.is_a? String
matching_spreadsheets[0].worksheets.detect { |s| s.title == sheet }
end
raise 'Unable to retrieve the first worksheet from the spreadsheet. Are there any pages?' if worksheet.nil?
first_valid_row_index = nil
last_valid_row_index = nil
for row in 1..worksheet.max_rows
first_valid_row_index = row if worksheet[row, 1].downcase == '[key]'
last_valid_row_index = row if worksheet[row, 1].downcase == '[end]'
end
raise IndexError, 'Invalid format: Could not find any [key] keyword in the A column of the worksheet' if first_valid_row_index.nil?
raise IndexError, 'Invalid format: Could not find any [end] keyword in the A column of the worksheet' if last_valid_row_index.nil?
raise IndexError, 'Invalid format: [end] must not be before [key] in the A column' if first_valid_row_index > last_valid_row_index
languages = Hash.new('languages')
default_language = nil
for column in 2..worksheet.max_cols
col_all = worksheet[first_valid_row_index, column]
col_all.each_line(' ') do |col_text|
default_language = col_text.gsub('*', '') if col_text.include? '*'
lang = col_text.gsub('*', '')
unless platform_options[:avoid_lang_downcase]
default_language = default_language.downcase
lang = lang.downcase
end
unless col_text.to_s == ''
languages.store lang, column
end
end
end
abort 'There are no language columns in the worksheet' if languages.count == 0
default_language = languages[0] if default_language.to_s == ''
default_language = override_default unless override_default.nil?
puts "Languages detected: #{languages.keys.join(', ')} -- using #{default_language} as default."
puts 'Building terminology in memory...'
terms = []
first_term_row = first_valid_row_index+1
last_term_row = last_valid_row_index-1
for row in first_term_row..last_term_row
key = worksheet[row, 1]
unless key.to_s == ''
term = Term.new(key)
languages.each do |lang, column_index|
term_text = worksheet[row, column_index]
term.values.store lang, term_text
end
terms << term
end
end
puts 'Loaded!'
res = Hash.new
res[:segments] = terms
res[:languages] = languages
res[:default_language] = default_language
res
end
|