1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2025-02-21 02:10:04 +00:00

code factoring

This commit is contained in:
nishiki 2014-12-06 18:20:43 +01:00
parent c3d484a85e
commit 291d68a1f7

View file

@ -3,14 +3,13 @@
# mail: nishiki@yaegashi.fr # mail: nishiki@yaegashi.fr
# info: a simple script who manage your passwords # info: a simple script who manage your passwords
module MPW require 'rubygems'
require 'gpgme'
require 'rubygems' require 'csv'
require 'gpgme' require 'i18n'
require 'csv' require 'fileutils'
require 'i18n'
require 'fileutils'
module MPW
class MPW class MPW
attr_accessor :error_msg attr_accessor :error_msg
@ -182,25 +181,10 @@ module MPW
csv << [r['name'], r['group'], r['protocol'], r['host'], r['login'], r['password'], r['port'], r['comment']] csv << [r['name'], r['group'], r['protocol'], r['host'], r['login'], r['password'], r['port'], r['comment']]
end end
end end
when :yaml when :yaml
data = {} File.open(file, 'w') {|f| f << @data.to_yaml}
@data.each do |id, r|
data.merge!({id => {'id' => r['id'],
'name' => r['name'],
'group' => r['group'],
'protocol' => r['protocol'],
'host' => r['host'],
'login' => r['login'],
'password' => r['password'],
'port' => r['port'],
'comment' => r['comment'],
}
}
)
end
File.open(file, 'w') {|f| f << data.to_yaml}
else else
@error_msg = "#{I18n.t('error.export.unknown_type', type: type)}" @error_msg = "#{I18n.t('error.export.unknown_type', type: type)}"
return false return false
@ -219,17 +203,19 @@ module MPW
def import(file, type=:csv) def import(file, type=:csv)
case type case type
when :csv when :csv
CSV.foreach(file, {headers: true, header_converters: :symbol}) do |row| CSV.foreach(file, {headers: true}) do |row|
if not update(row[:name], row[:group], row[:host], row[:protocol], row[:login], row[:password], row[:port], row[:comment]) if not update(row['name'], row['group'], row['host'], row['protocol'], row['login'], row['password'], row['port'], row['comment'])
return false return false
end end
end end
when :yaml when :yaml
YAML::load_file(file).each do |k, row| YAML::load_file(file).each do |k, row|
if not update(row['name'], row['group'], row['host'], row['protocol'], row['login'], row['password'], row['port'], row['comment']) if not update(row['name'], row['group'], row['host'], row['protocol'], row['login'], row['password'], row['port'], row['comment'])
return false return false
end end
end end
else else
@error_msg = "#{I18n.t('error.export.unknown_type', type: type)}" @error_msg = "#{I18n.t('error.export.unknown_type', type: type)}"
return false return false
@ -271,37 +257,39 @@ module MPW
# last_update -> last update # last_update -> last update
# @rtrn: false if data_remote is nil # @rtrn: false if data_remote is nil
def sync(data_remote, last_update) def sync(data_remote, last_update)
if not data_remote.instance_of?(Array) if not data_remote.instance_of?(Hash)
@error_msg = I18n.t('error.sync.hash')
return false return false
else not data_remote.nil? and not data_remote.empty?
@data.each do |l| else not data_remote.to_s.empty?
@data.each do |lk, l|
j = 0 j = 0
update = false update = false
# Update item # Update item
data_remote.each do |r| data_remote.each do |rk, r|
if l[:id] == r[:id] if l['id'] == r['id']
if l[:date].to_i < r[:date].to_i if l['date'].to_i < r['date'].to_i
update(r[:name], r[:group], r[:host], r[:protocol], r[:login], r[:password], r[:port], r[:comment], l[:id]) update(r['name'], r['group'], r['host'], r['protocol'], r['login'], r['password'], r['port'], r['comment'], l['id'])
end end
update = true update = true
data_remote.delete_at(j) data_remote.delete(rk)
break break
end end
j += 1 j += 1
end end
# Delete an old item # Delete an old item
if not update and l[:date].to_i < last_update if not update and l['date'].to_i < last_update
remove(l[:id]) remove(l['id'])
end end
end end
end end
# Add item # Add item
data_remote.each do |r| data_remote.each do |rk, r|
if r[:date].to_i > last_update if r['date'].to_i > last_update
update(r[:name], r[:group], r[:host], r[:protocol], r[:login], r[:password], r[:port], r[:comment], r[:id]) update(r['name'], r['group'], r['host'], r['protocol'], r['login'], r['password'], r['port'], r['comment'], r['id'])
end end
end end
@ -327,6 +315,6 @@ module MPW
return result return result
end end
end
end
end end