my_scripts 0.0.13 → 0.0.17

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.
@@ -1,72 +1,71 @@
1
- module MyScripts
2
- # This script converts given QIF file (with downloaded citibank statement)
3
- # into Quicken 2008 compatible format, and outputs it into new file for
4
- # import into Quicken
5
- #
6
- class Citi < Script
7
- INFILE_ENCODING = 'CP1251:UTF-8' # Encoding pair of input file ('File external:Ruby internal')
8
- OUTFILE_ENCODING = 'CP1252:UTF-8' # Encoding pair of output file ('File external:Ruby internal')
9
- # STDOUT_ENCODING = 'CP866:UTF-8' # Encoding pair of stdout ('Stdout external:Ruby internal')
10
-
11
- REPLACE = {
12
- 'Universam '=> '',
13
- 'Supermarket '=> '',
14
- 'Pokupka Masterkard ' => '',
15
- /.*(Detskiy Mir).*/ => "Detski Mir\nLChildcare",
16
- /.*(Legioner).*/ => "Legioner\nLBooks & Media:DVDs",
17
- /.*(Gudman).*/ => "Goodman\nLDining",
18
- /.*(Sushi Vesla).*/ => "\\1\nLDining",
19
- /.*(Starbucks).*/ => "\\1\nLDining",
20
- /.*(Medicina).*/ => "AO Medicina\nLMedical:Medicine",
21
- /.*(Pharmacy).*/ => "\\1\nLMedical:Medicine",
22
- /.*(Alye Parusa).*/ => "\\1\nLGroceries",
23
- /.*(Perekrestok).*/ => "\\1\nLGroceries",
24
- /.*(Ile De Beaute).*/ => "\\1\nLPersonal Care",
25
- /.*(Beeline).*/ => "\\1\nLCommunications:Telephone",
26
- /(.+) (Moscow Ru.*|Moskva Ru.*)/ => "\\1\nM\\2\nLHousehold",
27
- /Vkhodyashchij Platezh(.+Bank Moskvy)/ => "Incoming transfer\nM\\1\nL[BM 2yr Prestige]",
28
- /Platezh Cherez Citibank Online/ => "Incoming transfer\nL[Citi RUB]",
29
- /Oplata Dolg Kr Karta(.+)/ => "Transfer to Credit card\nM\\1\nL[Citi MC]",
30
- /Snyatie Nalichnykh(.+)/ => "Cash withdrawal\nM\\1\nL[Cash RUB]",
31
- /Vznos Nalichnykh(.+)/ => "Cash deposit\nM\\1\nL[Cash RUB]",
32
- /Kom Za Obsluzhivanie/ => "Citibank\nMService fee\nLFinance:Bank Charge",
33
- /Komissiya Za Snyatie Nalichnykh/ => "Citibank\nMCash withdrawal fee\nLFinance:Bank Charge"
34
-
35
- }
36
-
37
- def run
38
- usage "in_file.qif [out_file.qif]" if @argv.empty?
39
-
40
- in_file = @argv.first
41
-
42
- # If 2nd Arg is given, it is out_file name, otherwise derive from in_file name
43
- out_file = @argv[1] ? @argv[1] : in_file.sub(/\.qif|$/i, '_out.qif')
44
-
45
- # $stdout.set_encoding(STDOUT_ENCODING, :undef=>:replace)
46
- File.open(out_file, 'w:'+ (OUTFILE_ENCODING), :undef => :replace) do |outfile|
47
- File.open(in_file, 'r:'+ (INFILE_ENCODING), :undef => :replace).each_line do |line|
48
- type = line[0]
49
- text = line[1..-1]
50
- case type # Indicates type of field
51
- when 'D' # Date field - convert to MM/DD/YYYY format expected by Quicken
52
- text.gsub! /(\d+)\/(\d+)\/(\d+)/, '\2/\1/\3'
53
- when 'P' # Payee field
54
- # Convert payee from Cyrillic to Latin translit
55
- text.translit!
56
- # Capitalize each word and remove extra whitespaces (leaves first char intact)
57
- text = text.scan(/[\w&]+/).map{|word| word.capitalize}.join(' ')
58
- # Preprocess Payee field making pre-defined replacements
59
- REPLACE.each {|key, value| text.gsub!(key, value)}
60
- when 'M' # Memo field - drop if empty
61
- if text.rstrip.empty?
62
- text.clear
63
- type.clear
64
- end
65
- end
66
- line = type+text
67
- outfile.puts line unless line.empty?
68
- end
69
- end
70
- end
71
- end
1
+ module MyScripts
2
+ # This script converts given QIF file (with downloaded citibank statement)
3
+ # into Quicken 2008 compatible format, and outputs it into new file for
4
+ # import into Quicken
5
+ #
6
+ class Citi < Script
7
+ INFILE_ENCODING = 'CP1251:UTF-8' # Encoding pair of input file ('File external:Ruby internal')
8
+ OUTFILE_ENCODING = 'CP1252:UTF-8' # Encoding pair of output file ('File external:Ruby internal')
9
+ # STDOUT_ENCODING = 'CP866:UTF-8' # Encoding pair of stdout ('Stdout external:Ruby internal')
10
+
11
+ REPLACE = {
12
+ 'Universam '=> '',
13
+ 'Supermarket '=> '',
14
+ 'Pokupka Masterkard ' => '',
15
+ /.*(Detskiy Mir).*/ => "Detski Mir\nLChildcare",
16
+ /.*(Legioner).*/ => "Legioner\nLBooks & Media:DVDs",
17
+ /.*(Gudman).*/ => "Goodman\nLDining",
18
+ /.*(Sushi Vesla).*/ => "\\1\nLDining",
19
+ /.*(Starbucks).*/ => "\\1\nLDining",
20
+ /.*(Medicina).*/ => "AO Medicina\nLMedical:Medicine",
21
+ /.*(Pharmacy).*/ => "\\1\nLMedical:Medicine",
22
+ /.*(Alye Parusa).*/ => "\\1\nLGroceries",
23
+ /.*(Perekrestok).*/ => "\\1\nLGroceries",
24
+ /.*(Ile De Beaute).*/ => "\\1\nLPersonal Care",
25
+ /.*(Beeline).*/ => "\\1\nLCommunications:Telephone",
26
+ /(.+) (Moscow Ru.*|Moskva Ru.*)/ => "\\1\nM\\2\nLHousehold",
27
+ /Vkhodyashchij Platezh(.+Bank Moskvy)/ => "Incoming transfer\nM\\1\nL[BM 2yr Prestige]",
28
+ /Platezh Cherez Citibank Online/ => "Incoming transfer\nL[Citi RUB]",
29
+ /Oplata Dolg Kr Karta(.+)/ => "Transfer to Credit card\nM\\1\nL[Citi MC]",
30
+ /Snyatie Nalichnykh(.+)/ => "Cash withdrawal\nM\\1\nL[Cash RUB]",
31
+ /Vznos Nalichnykh(.+)/ => "Cash deposit\nM\\1\nL[Cash RUB]",
32
+ /Kom Za Obsluzhivanie/ => "Citibank\nMService fee\nLFinance:Bank Charge",
33
+ /Komissiya Za Snyatie Nalichnykh/ => "Citibank\nMCash withdrawal fee\nLFinance:Bank Charge"
34
+ }
35
+
36
+ def run
37
+ usage "in_file.qif [out_file.qif]" if @argv.empty?
38
+
39
+ in_file = @argv.first
40
+
41
+ # If 2nd Arg is given, it is out_file name, otherwise derive from in_file name
42
+ out_file = @argv[1] ? @argv[1] : in_file.sub(/\.qif|$/i, '_out.qif')
43
+
44
+ # $stdout.set_encoding(STDOUT_ENCODING, :undef=>:replace)
45
+ File.open(out_file, 'w:'+ OUTFILE_ENCODING, :undef => :replace) do |outfile|
46
+ File.open(in_file, 'r:'+ INFILE_ENCODING, :undef => :replace).each_line do |line|
47
+ type = line[0]
48
+ text = line[1..-1]
49
+ case type # Indicates type of field
50
+ when 'D' # Date field - convert to MM/DD/YYYY format expected by Quicken
51
+ text.gsub! /(\d+)\/(\d+)\/(\d+)/, '\2/\1/\3'
52
+ when 'P' # Payee field
53
+ # Convert payee from Cyrillic to Latin translit
54
+ text.translit!
55
+ # Capitalize each word and remove extra whitespaces (leaves first char intact)
56
+ text = text.scan(/[\w&]+/).map{|word| word.capitalize}.join(' ')
57
+ # Preprocess Payee field making pre-defined replacements
58
+ REPLACE.each {|key, value| text.gsub!(key, value)}
59
+ when 'M' # Memo field - drop if empty
60
+ if text.rstrip.empty?
61
+ text.clear
62
+ type.clear
63
+ end
64
+ end
65
+ line = type+text
66
+ outfile.puts line unless line.empty?
67
+ end
68
+ end
69
+ end
70
+ end
72
71
  end
@@ -1,39 +1,39 @@
1
- module MyScripts
2
- # This class encapsulates Command Line Interface for running scripts. It can be instantiated
3
- # with stubbed stdin and stdout if you want to run tests on your scripts
4
- class CLI
5
- class ScriptNameError < NameError # :nodoc:
6
- def initialize(message=nil)
7
- message ? super(message) : super
8
- end
9
- end
10
-
11
- attr_accessor :stdout, :stdin
12
-
13
- # Instantiates new CLI(command line interface) and runs script with given name (token)
14
- # and argv inside new CLI instance
15
- def self.run( script_name, argv )
16
- new.run script_name, argv
17
- end
18
-
19
- # Creates new command line interface
20
- def initialize( stdin=$stdin, stdout=$stdout )
21
- @stdin = stdin
22
- @stdout = stdout
23
- end
24
-
25
- # Runs a script with given name (token) and argv inside this CLI instance
26
- def run( script_name, argv )
27
- script = script_class_name(script_name).to_class
28
- raise ScriptNameError.new("Script #{script_class_name(script_name)} not found") unless script
29
-
30
- script.new(script_name, argv, self).run
31
- end
32
-
33
- private
34
-
35
- def script_class_name(script_name)
36
- "MyScripts::#{script_name.to_s.camel_case}"
37
- end
38
- end
39
- end
1
+ module MyScripts
2
+ # This class encapsulates Command Line Interface for running scripts. It can be instantiated
3
+ # with stubbed stdin and stdout if you want to run tests on your scripts
4
+ class CLI
5
+ class ScriptNameError < NameError # :nodoc:
6
+ def initialize(message=nil)
7
+ message ? super(message) : super
8
+ end
9
+ end
10
+
11
+ attr_accessor :stdout, :stdin
12
+
13
+ # Instantiates new CLI(command line interface) and runs script with given name (token)
14
+ # and argv inside new CLI instance
15
+ def self.run( script_name, argv )
16
+ new.run script_name, argv
17
+ end
18
+
19
+ # Creates new command line interface
20
+ def initialize( stdin=$stdin, stdout=$stdout )
21
+ @stdin = stdin
22
+ @stdout = stdout
23
+ end
24
+
25
+ # Runs a script with given name (token) and argv inside this CLI instance
26
+ def run( script_name, argv )
27
+ script = script_class_name(script_name).to_class
28
+ raise ScriptNameError.new("Script #{script_class_name(script_name)} not found") unless script
29
+
30
+ script.new(script_name, argv, self).run
31
+ end
32
+
33
+ private
34
+
35
+ def script_class_name(script_name)
36
+ "MyScripts::#{script_name.to_s.camel_case}"
37
+ end
38
+ end
39
+ end
@@ -1,17 +1,17 @@
1
- module MyScripts
2
- # Dummy script skeleton that is ready for your usage. Just put your script code
3
- # inside run method (if you use ARGV, change it to @argv). Done!
4
- # You can immediately run your script anywhere using following command:
5
- # $ dummy Whatever arguments your code expects and processes
6
- #
7
- class Dummy < Script
8
- def run
9
- # here you do all actual work for your script
10
- # you have following instance vars at your disposal:
11
- # @name - your script name,
12
- # @argv - your ARGV (Array of argument Strings passed at command line),
13
- # @cli - CLI runner (holds references to stdin and stdout)
14
- #...
15
- end
16
- end
17
- end
1
+ module MyScripts
2
+ # Dummy script skeleton that is ready for your usage. Just put your script code
3
+ # inside run method (if you use ARGV, change it to @argv). Done!
4
+ # You can immediately run your script anywhere using following command:
5
+ # $ dummy Whatever arguments your code expects and processes
6
+ #
7
+ class Dummy < Script
8
+ def run
9
+ # here you do all actual work for your script
10
+ # you have following instance vars at your disposal:
11
+ # @name - your script name,
12
+ # @argv - your ARGV (Array of argument Strings passed at command line),
13
+ # @cli - CLI runner (holds references to stdin and stdout)
14
+ #...
15
+ end
16
+ end
17
+ end
@@ -1,43 +1,43 @@
1
- # encoding: UTF-8
2
-
3
- class String
4
- TRANSLIT_CYRILLIC = %Q{АБВГДЕЁЗИЙКЛМНОПРСТУФЪЫЬЭабвгдеёзийклмнопрстуфъыьэ}
5
- TRANSLIT_LATIN = %Q{ABVGDEEZIJKLMNOPRSTUF"Y'Eabvgdeezijklmnoprstuf"y'e}
6
- TRANSLIT_DOUBLES = {'Ж'=>'ZH', 'Х'=>'KH', 'Ц'=>'TS', 'Ч'=>'CH', 'Ш'=>'SH', 'Щ'=>'SHCH', 'Ю'=>'YU', 'Я'=>'YA',
7
- 'ж'=>'zh', 'х'=>'kh', 'ц'=>'ts', 'ч'=>'ch', 'ш'=>'sh', 'щ'=>'shch', 'ю'=>'yu', 'я'=>'ya'}
8
-
9
- # Performs basic transliteration from Cyrillic to Latin characters and standard character combinations
10
- def translit!
11
- TRANSLIT_DOUBLES.each {|key, value| self.gsub!(key, value)}
12
- self.tr!(TRANSLIT_CYRILLIC, TRANSLIT_LATIN)
13
- self
14
- end
15
-
16
- # Turns string into appropriate class constant, returns nil if class not found
17
- def to_class
18
- klass = self.split("::").inject(Kernel) do |namespace, const|
19
- const == '' ? namespace : namespace.const_get(const)
20
- end
21
- klass.is_a?(Class) ? klass : nil
22
- rescue NameError
23
- nil
24
- end
25
-
26
- # Turns string into snake_case
27
- def snake_case
28
- gsub(/([a-z])([A-Z0-9])/, '\1_\2' ).downcase
29
- end
30
-
31
- # Turns string into CamelCase
32
- def camel_case
33
- if self.include? '_'
34
- self.split('_').map{|e| e.capitalize}.join
35
- else
36
- unless self =~ (/^[A-Z]/)
37
- self.capitalize
38
- else
39
- self
40
- end
41
- end
42
- end
43
- end
1
+ # encoding: UTF-8
2
+
3
+ class String
4
+ TRANSLIT_CYRILLIC = %Q{АБВГДЕЁЗИЙКЛМНОПРСТУФЪЫЬЭабвгдеёзийклмнопрстуфъыьэ}
5
+ TRANSLIT_LATIN = %Q{ABVGDEEZIJKLMNOPRSTUF"Y'Eabvgdeezijklmnoprstuf"y'e}
6
+ TRANSLIT_DOUBLES = {'Ж'=>'ZH', 'Х'=>'KH', 'Ц'=>'TS', 'Ч'=>'CH', 'Ш'=>'SH', 'Щ'=>'SHCH', 'Ю'=>'YU', 'Я'=>'YA',
7
+ 'ж'=>'zh', 'х'=>'kh', 'ц'=>'ts', 'ч'=>'ch', 'ш'=>'sh', 'щ'=>'shch', 'ю'=>'yu', 'я'=>'ya'}
8
+
9
+ # Performs basic transliteration from Cyrillic to Latin characters and standard character combinations
10
+ def translit!
11
+ TRANSLIT_DOUBLES.each {|key, value| self.gsub!(key, value)}
12
+ self.tr!(TRANSLIT_CYRILLIC, TRANSLIT_LATIN)
13
+ self
14
+ end
15
+
16
+ # Turns string into appropriate class constant, returns nil if class not found
17
+ def to_class
18
+ klass = self.split("::").inject(Kernel) do |namespace, const|
19
+ const == '' ? namespace : namespace.const_get(const)
20
+ end
21
+ klass.is_a?(Class) ? klass : nil
22
+ rescue NameError
23
+ nil
24
+ end
25
+
26
+ # Turns string into snake_case
27
+ def snake_case
28
+ gsub(/([a-z])([A-Z0-9])/, '\1_\2' ).downcase
29
+ end
30
+
31
+ # Turns string into CamelCase
32
+ def camel_case
33
+ if self.include? '_'
34
+ self.split('_').map{|e| e.capitalize}.join
35
+ else
36
+ unless self =~ (/^[A-Z]/)
37
+ self.capitalize
38
+ else
39
+ self
40
+ end
41
+ end
42
+ end
43
+ end
@@ -1,33 +1,33 @@
1
- module MyScripts
2
- # This script wraps up all work done on project in current directory,
3
- # adds all new(unversioned) files to git, commits all work done so far,
4
- # optionally bumps project version and pushes changes to remote repo
5
- #
6
- class Gitto < Script
7
- def run
8
- usage "[bump: 1 - patch, 10 - minor, 100 - major] Commit message goes here" if @argv.empty?
9
-
10
- # If first Arg is a number, it indicates version bump
11
- bump = @argv[0].to_i > 0 ? @argv.shift.to_i : 0
12
-
13
- # All the other args lumped into message, or default message
14
- message = @argv.empty? ? 'Commit' : @argv.join(' ')
15
- # Timestamp added to message
16
- message += " #{Time.now.to_s[0..-6]}"
17
-
18
- puts "Committing (versionup =#{bump}) with message: #{message}"
19
-
20
- case bump
21
- when 1..9
22
- system %Q[rake version:bump:patch]
23
- when 10..99
24
- system %Q[rake version:bump:minor]
25
- when 10..99
26
- system %Q[rake version:bump:major]
27
- end
28
- system %Q[git add --all]
29
- system %Q[git commit -a -m "#{message}" --author arvicco]
30
- system %Q[git push]
31
- end
32
- end
33
- end
1
+ module MyScripts
2
+ # This script wraps up all work done on project in current directory,
3
+ # adds all new(unversioned) files to git, commits all work done so far,
4
+ # optionally bumps project version and pushes changes to remote repo
5
+ #
6
+ class Gitto < Script
7
+ def run
8
+ usage "[bump: 1 - patch, 10 - minor, 100 - major] Commit message goes here" if @argv.empty?
9
+
10
+ # If first Arg is a number, it indicates version bump
11
+ bump = @argv[0].to_i > 0 ? @argv.shift.to_i : 0
12
+
13
+ # All the other args lumped into message, or default message
14
+ message = @argv.empty? ? 'Commit' : @argv.join(' ')
15
+ # Timestamp added to message
16
+ message += " #{Time.now.to_s[0..-6]}"
17
+
18
+ puts "Committing (versionup =#{bump}) with message: #{message}"
19
+
20
+ case bump
21
+ when 1..9
22
+ system %Q[rake version:bump:patch]
23
+ when 10..99
24
+ system %Q[rake version:bump:minor]
25
+ when 10..99
26
+ system %Q[rake version:bump:major]
27
+ end
28
+ system %Q[git add --all]
29
+ system %Q[git commit -a -m "#{message}" --author arvicco]
30
+ system %Q[git push]
31
+ end
32
+ end
33
+ end
@@ -1,20 +1,20 @@
1
- module MyScripts
2
- # This script uses Jeweler to create new project skeleton, local git repo and
3
- # initiate remote repo on github
4
- #
5
- class Jew < Script
6
- def run
7
- usage "project_name Summary or description goes here" if @argv.empty?
8
-
9
- # First Arg should be project name
10
- project = @argv.shift
11
-
12
- # All the other args lumped into summary, or default summary
13
- summary = @argv.empty? ? "New project #{project}" : @argv.join(' ')
14
-
15
- puts "Creating Jeweler project #{project} with summary/description: #{summary}"
16
-
17
- system %Q[jeweler --rspec --cucumber --create-repo --summary "#{summary}" --description "#{summary}" #{project}]
18
- end
19
- end
1
+ module MyScripts
2
+ # This script uses Jeweler to create new project skeleton, local git repo and
3
+ # initiate remote repo on github
4
+ #
5
+ class Jew < Script
6
+ def run
7
+ usage "project_name Summary or description goes here" if @argv.empty?
8
+
9
+ # First Arg should be project name
10
+ project = @argv.shift
11
+
12
+ # All the other args lumped into summary, or default summary
13
+ summary = @argv.empty? ? "New project #{project}" : @argv.join(' ')
14
+
15
+ puts "Creating Jeweler project #{project} with summary/description: #{summary}"
16
+
17
+ system %Q[jeweler --rspec --cucumber --create-repo --summary "#{summary}" --description "#{summary}" #{project}]
18
+ end
19
+ end
20
20
  end
@@ -1,20 +1,20 @@
1
- module MyScripts
2
- class Mybones < Script
3
- # This script uses Mr.Bones gem to create new project skeleton, local git repo and
4
- # initiate remote repo on github
5
- #
6
- def run
7
- usage "project_name Summary or description goes here" if @argv.empty?
8
-
9
- # First Arg should be project name
10
- project = @argv.shift
11
-
12
- # All the other args lumped into summary, or default summary
13
- summary = @argv.empty? ? "New project #{project}" : @argv.join(' ')
14
-
15
- puts "Creating Bones project #{project} with summary: #{summary}"
16
-
17
- system %Q[bones create --github "#{summary}" -s basic #{project}]
18
- end
19
- end
1
+ module MyScripts
2
+ # This script uses Mr.Bones gem to create new project skeleton, local git repo and
3
+ # initiate remote repo on github
4
+ #
5
+ class Mybones < Script
6
+ def run
7
+ usage "project_name Summary or description goes here" if @argv.empty?
8
+
9
+ # First Arg should be project name
10
+ project = @argv.shift
11
+
12
+ # All the other args lumped into summary, or default summary
13
+ summary = @argv.empty? ? "New project #{project}" : @argv.join(' ')
14
+
15
+ puts "Creating Bones project #{project} with summary: #{summary}"
16
+
17
+ system %Q[bones create --github "#{summary}" -s basic #{project}]
18
+ end
19
+ end
20
20
  end
@@ -1,17 +1,19 @@
1
- module MyScripts
2
- # Starts and controls rabbitmq server
3
- class Rabbit < Script
4
- def run
5
- case @argv.shift
6
- when /start/
7
- system "%ERLANG_HOME%/lib/rabbitmq_server-1.7.0/sbin/rabbitmq-server.bat #{ARGV.join(' ')}"
8
- when /stop/
9
- system "%ERLANG_HOME%/lib/rabbitmq_server-1.7.0/sbin/rabbitmqctl.bat stop #{ARGV.join(' ')}"
10
- when /ctl/
11
- system "%ERLANG_HOME%/lib/rabbitmq_server-1.7.0/sbin/rabbitmqctl.bat #{ARGV.join(' ')}"
12
- else
13
- usage ["start [args] - starts rabbitmq node", "mqctl [args] - controls rabbitmq node"]
14
- end
15
- end
16
- end
17
- end
1
+ module MyScripts
2
+ # Starts and controls rabbitmq server
3
+ class Rabbit < Script
4
+ def run
5
+ rabbit_hole = ENV['ERLANG_HOME'] + '/lib/rabbitmq_server-1.7.0/sbin'
6
+ case @argv.shift
7
+ when /start/
8
+ system "#{rabbit_hole}/rabbitmq-server.bat #{@argv.join(' ')}"
9
+ when /stop/
10
+ system "#{rabbit_hole}/rabbitmqctl.bat stop #{@argv.join(' ')}"
11
+ when /ctl/
12
+ system "#{rabbit_hole}/rabbitmqctl.bat #{@argv.join(' ')}"
13
+ else
14
+ usage ["start [args] - starts rabbitmq node", "stop [args] - stops running rabbitmq node",
15
+ "ctl [args] - controls rabbitmq node"]
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,29 +1,29 @@
1
- module MyScripts
2
- # Base class for all scripts. Subclass it and override run method with actual
3
- # work your script will be doing
4
- class Script
5
- def initialize( name, argv, cli )
6
- @name = name
7
- @argv = argv
8
- @cli = cli
9
- end
10
-
11
- def run
12
- end
13
-
14
- def puts *args
15
- @cli.stdout.puts *args
16
- end
17
-
18
- def usage examples, explanation = nil
19
- puts "Usage:"
20
- puts (examples.respond_to?(:split) ? examples.split("\n") : examples).map {|line| " #{@name} #{line}"}
21
- puts explanation if explanation
22
- exit 1
23
- end
24
-
25
- def to_s
26
- "#{@name} #{@argv.join(' ')} -> #{self.class}"
27
- end
28
- end
29
- end
1
+ module MyScripts
2
+ # Base class for all scripts. Subclass it and override run method with actual
3
+ # work your script will be doing
4
+ class Script
5
+ def initialize( name, argv, cli )
6
+ @name = name
7
+ @argv = argv
8
+ @cli = cli
9
+ end
10
+
11
+ def run
12
+ end
13
+
14
+ def puts *args
15
+ @cli.stdout.puts *args
16
+ end
17
+
18
+ def usage examples, explanation = nil
19
+ puts "Usage:"
20
+ puts (examples.respond_to?(:split) ? examples.split("\n") : examples).map {|line| " #{@name} #{line}"}
21
+ puts explanation if explanation
22
+ exit 1
23
+ end
24
+
25
+ def to_s
26
+ "#{@name} #{@argv.join(' ')} -> #{self.class}"
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,40 @@
1
+ module MyScripts
2
+ # This script prevents screen saver from locking down Windows by randomly moving
3
+ # mouse pointer a bit every 4 minutes or so. Why would you need this? Well, if your
4
+ # XP box is in a domain with security-crazy admins who have immutable policy of
5
+ # forced screen saver after 5 minutes of inactivity you'd feel the pain...
6
+ #
7
+ class Wake < Script
8
+ SLEEP_TIME = 4 * 60 # seconds
9
+
10
+ def initialize( name, argv, cli )
11
+
12
+ require 'win/gui/input'
13
+
14
+ define_method :move_mouse_randomly do
15
+ x, y = Win::Gui::Input::get_cursor_pos
16
+ x1, y1 = x+rand(3)-1, y+rand(3)-1
17
+ Win::Gui::Input::mouse_event(Win::Gui::Input::MOUSEEVENTF_ABSOLUTE, x1, y1, 0, 0)
18
+ puts "Cursor positon set to #{x1}, #{y1}"
19
+ end
20
+
21
+ super
22
+ end
23
+
24
+ def run
25
+ case @argv.size
26
+ when 0
27
+ sleep_time = SLEEP_TIME
28
+ when 1
29
+ sleep_time = @argv.first * 60
30
+ else
31
+ usage "[minutes] - prevents screen auto lock-up by moving mouse pointer every (4) [minutes]"
32
+ end
33
+
34
+ loop do
35
+ move_mouse_randomly
36
+ sleep sleep_time
37
+ end
38
+ end
39
+ end
40
+ end