applocale 0.1.6 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,79 +12,88 @@ module Applocale
12
12
  OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'
13
13
  APPLICATION_NAME = 'AppLocale'
14
14
  CLIENT_SECRETS_PATH = 'client_secret.json'
15
- CREDENTIALS_PATH = File.join(Dir.home, '.applan_credentials',
16
- 'drive-ruby-applocale.yaml')
17
15
  SCOPE = [Google::Apis::DriveV3::AUTH_DRIVE_METADATA_READONLY, Google::Apis::DriveV3::AUTH_DRIVE, Google::Apis::DriveV3::AUTH_DRIVE_FILE]
18
16
 
19
- def self.is_googlelink(link)
20
- if !link.nil? && link.length > 0
21
- if link.match(/https:\/\/docs.google.com\/spreadsheets\/d\/([^\/]*)/i)
22
- if $1.length > 0
23
- return $1
24
- end
25
- end
17
+ attr_accessor :download_link, :credential_path, :xlsx_path,:spreadsheet_id
18
+
19
+ def initialize(link, credential_path, xlsx_path)
20
+ self.download_link = link.to_s.strip
21
+ self.credential_path = credential_path.to_s.strip
22
+ self.xlsx_path = xlsx_path.to_s.strip
23
+ self.spreadsheet_id = GoogleHelper.get_spreadsheet_id(link)
24
+
25
+ if self.download_link.length <= 0
26
+ ErrorUtil::ConfigFileInValid.new('[link] is missing in config file ').raise
27
+ end
28
+ if self.credential_path.length <= 0
29
+ ErrorUtil::ConfigFileInValid.new('[credential_path] is missing in config file ').raise
30
+ end
31
+ if self.xlsx_path.length <= 0
32
+ ErrorUtil::ConfigFileInValid.new('[xlsx_path] is missing in config file ').raise
26
33
  end
27
34
  end
28
35
 
29
- def self.reset_loginacc
30
- if File.exist? CREDENTIALS_PATH
31
- File.delete(CREDENTIALS_PATH)
36
+ private
37
+ def removeOldExcel
38
+ if File.exist? self.xlsx_path
39
+ FileUtils.rm(self.xlsx_path)
32
40
  end
33
- puts "Account Reseted!"
34
41
  end
35
42
 
36
- def self.download_spreadsheet(spreadsheet_Id, filename)
37
- puts "Start download from google, fileId: #{spreadsheet_Id} ...".green
43
+ public
44
+ def download
45
+ removeOldExcel
38
46
  service = Google::Apis::DriveV3::DriveService.new
39
47
  service.client_options.application_name = APPLICATION_NAME
40
- service.authorization = self.authorize
48
+ service.authorization = authorize
41
49
  begin
42
- service.export_file(spreadsheet_Id,
43
- 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
44
- download_dest: filename)
45
- if File.exist? filename
50
+ service.export_file(self.spreadsheet_id,
51
+ 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
52
+ download_dest: self.xlsx_path)
53
+ if File.exist? self.xlsx_path
46
54
  puts 'Download from google finished'.green
47
55
  else
48
56
  ErrorUtil::DownloadFromGoogleFail.new.raise
49
57
  end
50
58
  rescue Google::Apis::AuthorizationError => e
51
- self.failauth(spreadsheet_Id, filename)
59
+ failauth
52
60
  rescue Google::Apis::ClientError => e
53
- self.failauth(spreadsheet_Id, filename)
61
+ failauth
54
62
  rescue Google::Apis::ServerError => e
55
- self.failauth( spreadsheet_Id, filename)
63
+ failauth
56
64
  rescue
57
65
  ErrorUtil::DownloadFromGoogleFail.new.raise
58
66
  end
59
67
  end
60
68
 
61
- def self.failauth(spreadsheet_Id, filename)
69
+ private
70
+ def failauth
62
71
  ErrorUtil::DownloadFromGoogleFail.new.to_warn
63
- self.askfor_relogin(true, spreadsheet_Id, filename)
72
+ askfor_relogin(true)
64
73
  end
65
74
 
66
75
  private
67
- def self.askfor_relogin(is_firsttime, spreadsheet_Id, filename)
76
+ def askfor_relogin(is_firsttime)
68
77
  unless is_firsttime
69
78
  puts "Invalid Command. Please input [Y/N]".red
70
79
  end
71
80
  puts "login again? [Y/N]".red
72
81
  code = STDIN.gets.chomp.downcase
73
82
  if code == 'y'
74
- self.reset_loginacc
75
- self.download_spreadsheet(spreadsheet_Id, filename)
83
+ reset_loginacc
84
+ self.download
76
85
  elsif code == 'n'
77
86
  exit(0)
78
87
  else
79
- self.askfor_relogin(false, spreadsheet_Id, filename)
88
+ askfor_relogin(false)
80
89
  end
81
90
  end
82
91
 
83
- def self.authorize
84
-
85
- FileUtils.mkdir_p(File.dirname(CREDENTIALS_PATH))
92
+ private
93
+ def authorize
94
+ FileUtils.mkdir_p(File.dirname(self.credential_path))
86
95
  client_id = Google::Auth::ClientId.from_file(File.expand_path(CLIENT_SECRETS_PATH, File.dirname(__FILE__)))
87
- token_store = Google::Auth::Stores::FileTokenStore.new(file: CREDENTIALS_PATH)
96
+ token_store = Google::Auth::Stores::FileTokenStore.new(file: self.credential_path)
88
97
  authorizer = Google::Auth::UserAuthorizer.new(
89
98
  client_id, SCOPE, token_store)
90
99
  user_id = 'default'
@@ -102,5 +111,37 @@ module Applocale
102
111
  credentials
103
112
  end
104
113
 
114
+ private
115
+ def reset_loginacc
116
+ if File.exist? self.credential_path
117
+ File.delete(self.credential_path)
118
+ end
119
+ puts "Account Reseted!"
120
+ end
121
+
122
+ public
123
+ def self.is_googlelink(link)
124
+ if !link.nil? && link.length > 0
125
+ if link.match(/https:\/\/docs.google.com\/spreadsheets\/d\/([^\/]*)/i)
126
+ if $1.length > 0
127
+ return true
128
+ end
129
+ end
130
+ end
131
+ return false
132
+ end
133
+
134
+ public
135
+ def self.get_spreadsheet_id(link)
136
+ if !link.nil? && link.length > 0
137
+ if link.match(/https:\/\/docs.google.com\/spreadsheets\/d\/([^\/]*)/i)
138
+ if $1.strip.length > 0
139
+ return $1.strip
140
+ end
141
+ end
142
+ end
143
+ ErrorUtil::DownloadFromGoogleFail.new.raise
144
+ end
145
+
105
146
  end
106
147
  end
@@ -5,19 +5,30 @@ require File.expand_path('../../../Util/regex_util.rb', __FILE__)
5
5
 
6
6
  require 'rubyXL'
7
7
  require 'colorize'
8
+ module Applocale
9
+ class Injeust
10
+
11
+ end
12
+ end
8
13
 
9
14
  module Applocale
10
15
  class ParseXLSX
11
16
 
12
- @xlsx = nil
13
17
  @sheetcontent_list = nil
14
18
  @allkey_dict = {}
15
19
  @all_error = nil
16
- @setting = Setting
17
20
 
18
- def initialize
19
- @setting = Setting
20
- puts "Start to Parse XLSX: \"#{@setting.xlsxpath}\" ...".green
21
+ @platform
22
+ @xlsxpath
23
+ @langlist
24
+ @sheetobj_list
25
+
26
+ def initialize(platfrom, xlsxpath, langlist, sheetobj_list)
27
+ @platform = platfrom
28
+ @xlsxpath = xlsxpath
29
+ @langlist = langlist
30
+ @sheetobj_list = sheetobj_list
31
+ puts "Start to Parse XLSX: \"#{@xlsxpath}\" ...".green
21
32
  @sheetcontent_list = Array.new
22
33
  @allkey_dict = {}
23
34
  @all_error = Array.new
@@ -30,29 +41,47 @@ module Applocale
30
41
 
31
42
  def parse
32
43
  begin
33
- workbook = RubyXL::Parser.parse(@setting.xlsxpath)
44
+ workbook = RubyXL::Parser.parse(@xlsxpath)
34
45
  rescue
35
- ErrorUtil::CannotOpenXlsxFile.new(@setting.xlsxpath).raise
46
+ ErrorUtil::CannotOpenXlsxFile.new(@xlsxpath).raise
36
47
  end
48
+ sheetnamelist = Applocale::Config::Sheet.get_sheetlist(@sheetobj_list)
37
49
  workbook.worksheets.each do |worksheet|
38
50
  sheetname = worksheet.sheet_name
51
+ sheetinfoobj = Applocale::Config::Sheet.get_sheetobj_by_sheetname(@sheetobj_list, sheetname)
52
+ if sheetinfoobj.nil?
53
+ next
54
+ end
55
+ sheetnamelist.delete(sheetname)
56
+
39
57
  sheetcontent = ParseXLSXModule::SheetContent.new(sheetname)
40
- rowno = -1
58
+ if sheetinfoobj.is_a? Applocale::Config::SheetInfoByRow
59
+ keycolno = Applocale::ParseXLSXModule::Helper.collabel_to_colno(sheetinfoobj.key_col)
60
+ sheetinfoobj.to_keyStrWithColNo(sheetcontent)
61
+ end
62
+
63
+ rowno = 0
41
64
  worksheet.each {|row|
42
65
  rowno += 1
43
66
  # colno = 0
44
- next if row.nil?
45
- cells = row && row.cells
46
67
  if sheetcontent.header_rowno.nil?
47
- headerinfo = find_header(cells)
48
- unless headerinfo.nil?
49
- sheetcontent.header_rowno = rowno
50
- sheetcontent.keyStr_with_colno = headerinfo[:keystr_colno]
51
- sheetcontent.lang_with_colno_list = headerinfo[:lang_colno]
68
+ if sheetinfoobj.is_a? Applocale::Config::SheetInfoByHeader
69
+ next if row.nil?
70
+ cells = row && row.cells
71
+ headerinfo = find_header(sheetinfoobj, cells)
72
+ unless headerinfo.nil?
73
+ sheetcontent.header_rowno = rowno
74
+ sheetcontent.keyStr_with_colno = headerinfo[:keystr_colno]
75
+ sheetcontent.lang_with_colno_list = headerinfo[:lang_colno]
76
+ end
52
77
  end
78
+ elsif sheetcontent.header_rowno > rowno
79
+ next
53
80
  else
81
+ next if row.nil?
82
+ cells = row && row.cells
54
83
  begin
55
- rowcontent = parse_row(sheetname, rowno, worksheet.sheet_data[rowno], sheetcontent.keyStr_with_colno, sheetcontent.lang_with_colno_list)
84
+ rowcontent = parse_row(sheetname, rowno, cells, sheetcontent.keyStr_with_colno, sheetcontent.lang_with_colno_list)
56
85
  unless rowcontent.nil?
57
86
  prev_rowcontent = @allkey_dict[rowcontent.key_str.downcase]
58
87
  if prev_rowcontent.nil?
@@ -82,7 +111,7 @@ module Applocale
82
111
 
83
112
  def parse_row(sheetname, rowno, cells, keystr_with_colno, lang_with_colno_list)
84
113
  begin
85
- cell = cells[keystr_with_colno.colno]
114
+ cell = cells[keystr_with_colno.colno - 1 ]
86
115
  val = cell && cell.value
87
116
  keystr = to_value_key(val)
88
117
  rescue ErrorUtil::ParseXlsxError::InValidKey => e
@@ -93,9 +122,8 @@ module Applocale
93
122
 
94
123
  unless keystr.nil?
95
124
  rowinfo = ParseXLSXModule::RowInfo.new(sheetname, rowno, keystr)
96
- (0..lang_with_colno_list.length-1).each do |k|
97
- lang_with_colno = lang_with_colno_list[k]
98
- cell = cells[lang_with_colno.colno]
125
+ lang_with_colno_list.each do |lang_with_colno|
126
+ cell = cells[lang_with_colno.colno - 1]
99
127
  val = cell && cell.value
100
128
  cell_value = val.to_s
101
129
  lang_name = lang_with_colno.lang
@@ -108,7 +136,7 @@ module Applocale
108
136
  def to_value_key(value)
109
137
  if !value.nil? && value != ''
110
138
  new_value = value.to_s
111
- if ValidKey.is_validkey(@setting.platform, new_value)
139
+ if ValidKey.is_validkey(@platform, new_value)
112
140
  return new_value
113
141
  else
114
142
  rowinfo = ParseXLSXModule::RowInfo.new(nil, nil, value)
@@ -125,20 +153,20 @@ module Applocale
125
153
  end
126
154
  end
127
155
 
128
- def find_header(cells)
156
+ def find_header(sheetinfoobj, cells)
129
157
  keystr_with_colno = nil
130
158
  lang_with_colno_list = Array.new
131
159
  k_header_lang_dict = []
132
- colno = 0
160
+ colno = 1
133
161
  cells.each{ |cell|
134
162
  value = cell && cell.value
135
163
  unless value.nil?
136
- if value == @setting.keystr && keystr_with_colno.nil?
164
+ if value == sheetinfoobj.key_header && keystr_with_colno.nil?
137
165
  keystr_with_colno = ParseXLSXModule::KeyStrWithColNo.new(value, colno)
138
166
  else
139
- @setting.langlist.each do |lang, info|
140
- if value == info[:xlsheader] && k_header_lang_dict.index(lang).nil?
141
- lang_with_colno_list.push(ParseXLSXModule::LangWithColNo.new(info[:xlsheader], lang, colno))
167
+ sheetinfoobj.lang_headers.each do |lang, keyforlang|
168
+ if value == keyforlang && k_header_lang_dict.index(lang).nil?
169
+ lang_with_colno_list.push(ParseXLSXModule::LangWithColNo.new(keyforlang, lang, colno))
142
170
  k_header_lang_dict.push(lang)
143
171
  end
144
172
  end
@@ -146,7 +174,7 @@ module Applocale
146
174
  end
147
175
  colno += 1
148
176
  }
149
- if !keystr_with_colno.nil? && lang_with_colno_list.length == @setting.langlist.length
177
+ if !keystr_with_colno.nil? && lang_with_colno_list.length == sheetinfoobj.lang_headers.length
150
178
  return {:keystr_colno => keystr_with_colno, :lang_colno => lang_with_colno_list}
151
179
  end
152
180
  end
@@ -1,6 +1,21 @@
1
+
2
+
1
3
  module Applocale
2
4
 
5
+
3
6
  module ParseXLSXModule
7
+ class Helper
8
+ def self.collabel_to_colno(collabel)
9
+ if collabel.match(/^(\d)+$/)
10
+ return collabel.to_i
11
+ end
12
+ number = collabel.tr("A-Z", "1-9a-q").to_i(27)
13
+ number -= 1 if number > 27
14
+ return number
15
+ end
16
+ end
17
+
18
+
4
19
  class SheetContent
5
20
  attr_accessor :sheetname, :header_rowno, :keyStr_with_colno, :lang_with_colno_list, :rowinfo_list, :comment
6
21
 
@@ -52,7 +67,7 @@ module Applocale
52
67
  end
53
68
 
54
69
  def to_s
55
- "sheetname = #{sheetname}, rowno = #{rowno+1}, key_str = #{key_str}, content_dict = #{content_dict}"
70
+ "sheetname = #{sheetname}, rowno = #{rowno}, key_str = #{key_str}, content_dict = #{content_dict}"
56
71
  end
57
72
 
58
73
  end
@@ -67,7 +82,7 @@ module Applocale
67
82
  end
68
83
 
69
84
  def to_s
70
- "{header_str => #{header_str}, colno => #{colno+1}}"
85
+ "{header_str => #{header_str}, colno => #{colno}}"
71
86
  end
72
87
 
73
88
  end
@@ -82,10 +97,30 @@ module Applocale
82
97
  end
83
98
 
84
99
  def to_s
85
- "{header_str => #{header_str}, lang => #{lang}, colno => #{colno+1}}"
100
+ "{header_str => #{header_str}, lang => #{lang}, colno => #{colno}}"
86
101
  end
87
102
 
88
103
  end
89
104
 
90
105
  end
91
- end
106
+ end
107
+
108
+
109
+ module Applocale
110
+ module Config
111
+ class SheetInfoByRow
112
+ def to_keyStrWithColNo(sheetcontent)
113
+ sheetcontent.header_rowno = self.row
114
+ keycolno = Applocale::ParseXLSXModule::Helper.collabel_to_colno(self.key_col)
115
+ sheetcontent.keyStr_with_colno = ParseXLSXModule::KeyStrWithColNo.new(nil, keycolno)
116
+ sheetcontent.lang_with_colno_list = Array.new
117
+ self.lang_cols.each do |lang, collabel|
118
+ colno = Applocale::ParseXLSXModule::Helper.collabel_to_colno(collabel)
119
+ obj = ParseXLSXModule::LangWithColNo.new(nil,lang, colno)
120
+ sheetcontent.lang_with_colno_list.push(obj)
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
126
+
@@ -3,6 +3,7 @@ require File.expand_path('../../../Util/platform.rb', __FILE__)
3
3
  require File.expand_path('../../ParseXLSX/parse_xlsx_module', __FILE__)
4
4
  require File.expand_path('../parse_strings_file', __FILE__)
5
5
  require File.expand_path('../parse_xml_file', __FILE__)
6
+ require File.expand_path('../../../Util/injection.rb', __FILE__)
6
7
 
7
8
  require 'rubyXL'
8
9
  require 'colorize'
@@ -10,32 +11,41 @@ require 'colorize'
10
11
  module Applocale
11
12
  class ParseLocalizedResource
12
13
  @skip_error = false
13
- @setting = Setting
14
+ @platform
15
+ @xlsxpath
16
+ @langpathobj_list
17
+ @sheetobj_list
18
+ @injectobj
14
19
 
15
- def initialize(skip_error = false)
20
+ def initialize(skip_error = false, platform, xlsxpath, langpathobj_list, sheetobj_list, rubycode)
16
21
  @skip_error = skip_error
17
- @setting = Setting
18
- FileUtils.mkdir_p(File.dirname(@setting.xlsxpath))
19
- FileUtils.rm(@setting.xlsxpath) if File.exist? @setting.xlsxpath
20
- keystr_with_colno = ParseXLSXModule::KeyStrWithColNo.new(@setting.keystr, 0)
21
- lang_with_colno_list = Array.new
22
- colno = 1
23
- @setting.langlist.each do |key, langinfo|
24
- langwith_colno = ParseXLSXModule::LangWithColNo.new(langinfo[:xlsheader], key, colno)
25
- colno+=1
26
- lang_with_colno_list.push(langwith_colno)
27
- end
28
- if @setting.platform == Platform::IOS
22
+ @platform = platform
23
+ @xlsxpath = xlsxpath
24
+ @langpathobj_list = langpathobj_list
25
+ @sheetobj_list = sheetobj_list
26
+ @injectobj = Applocale::Injection.load(rubycode)
27
+ startParse()
28
+ end
29
+
30
+ def startParse
31
+ puts "Start to Parse StringFile .... ".green
32
+
33
+ FileUtils.mkdir_p(File.dirname(@xlsxpath))
34
+ FileUtils.rm(@xlsxpath) if File.exist? @xlsxpath
35
+
36
+ sheetobj = @sheetobj_list[0]
37
+
38
+ if @platform == Platform::IOS
29
39
  result = self.parse_ios
30
- write_to_xlsx(@setting.xlsxpath, keystr_with_colno, lang_with_colno_list, result[:errorlist], result[:content], result[:keylist])
40
+ write_to_xlsx(@xlsxpath, sheetobj, result[:errorlist], result[:content], result[:keylist])
31
41
  else
32
42
  result = self.parse_android
33
- write_to_xlsx(@setting.xlsxpath, keystr_with_colno, lang_with_colno_list, result[:errorlist], result[:content], result[:keylist])
43
+ write_to_xlsx(@xlsxpath, sheetobj, result[:errorlist], result[:content], result[:keylist])
34
44
  end
35
45
  end
36
46
 
37
47
  def parse_ios
38
- result = ParseStringsFile.new
48
+ result = ParseStringsFile.new(@platform, @langpathobj_list, @injectobj)
39
49
  errorlist = result.errorlist
40
50
  content = result.strings_keys
41
51
  keylist = result.keys_list
@@ -43,30 +53,47 @@ module Applocale
43
53
  end
44
54
 
45
55
  def parse_android
46
- result = ParseXMLFile.new
56
+ result = ParseXMLFile.new(@platform, @langpathobj_list, @injectobj)
47
57
  errorlist = result.errorlist
48
58
  content = result.strings_keys
49
59
  keylist = result.keys_list
50
60
  return {:errorlist => errorlist, :content => content, :keylist => keylist}
51
61
  end
52
62
 
53
- def write_to_xlsx(path, keystrwithColNo, langwithColNolist, errorlist, content, keylist)
63
+ def write_to_xlsx(path, sheetobj, errorlist, content, keylist)
54
64
  ErrorUtil::ParseLocalizedError::ParseLocalizedError.raiseArr(errorlist, !@skip_error)
55
65
  puts "Start write to file: \"#{path}\" ...".green
56
66
  workbook = RubyXL::Workbook.new
57
67
  worksheet = workbook.worksheets[0]
68
+ worksheet.sheet_name = sheetobj.sheetname
58
69
  rowno = 0
59
- worksheet.add_cell(rowno, keystrwithColNo.colno, keystrwithColNo.header_str)
60
- langwithColNolist.each do |langwithColNo|
61
- worksheet.add_cell(rowno, langwithColNo.colno, langwithColNo.header_str)
70
+
71
+ keycolno = 0
72
+ langcolno_dict = {}
73
+
74
+ sheet_info_obj = sheetobj.obj
75
+ if sheet_info_obj.is_a? Applocale::Config::SheetInfoByHeader
76
+ worksheet.add_cell(rowno, keycolno, sheet_info_obj.key_header)
77
+ langcolno = keycolno + 1
78
+ sheet_info_obj.lang_headers.each do |key, header|
79
+ worksheet.add_cell(rowno, langcolno, header)
80
+ langcolno_dict[key] = langcolno
81
+ langcolno += 1
82
+ end
83
+ rowno+=1
84
+ elsif sheet_info_obj.is_a? Applocale::Config::SheetInfoByRow
85
+ rowno = sheet_info_obj.row - 1
86
+ keycolno = Applocale::ParseXLSXModule::Helper.collabel_to_colno(sheet_info_obj.key_col) - 1
87
+ sheet_info_obj.lang_cols.each do |lang, collabel|
88
+ langcolno_dict[lang] = Applocale::ParseXLSXModule::Helper.collabel_to_colno(collabel) - 1
89
+ end
62
90
  end
63
- rowno+=1
91
+
64
92
  keylist.each do |key|
65
- worksheet.add_cell(rowno, keystrwithColNo.colno, key)
93
+ worksheet.add_cell(rowno, keycolno, key)
66
94
  unless content[key].nil?
67
- langwithColNolist.each do |langwithColNo|
68
- lang = langwithColNo.lang.to_s
69
- worksheet.add_cell(rowno, langwithColNo.colno, content[key][lang][:value]) if !content[key][lang].nil? && !content[key][lang][:value].nil?
95
+ langcolno_dict.each do |lang, colno|
96
+ worksheet.add_cell(rowno, colno, content[key][lang][:value]) if !content[key][lang].nil? && !content[key][lang][:value].nil?
70
97
  end
71
98
  end
72
99
  rowno+=1