Skip to content

Commit

Permalink
Results output, args rearrangement (v0.2.0 candidate) (#1)
Browse files Browse the repository at this point in the history
* Adds results output
* Adds console logging/results display, rearranges arguments
* `--verbose` and `--debug` features now provide some console logging
* The initial (optionally unflagged) argument now designates the patterns file, not the source ingest path. The patterns file can still optionally be designated with `-p` or `--patterns`, but the source directory can no longer be indicated by listing it as the first argument. The source directory may optionally be indicated with `-s PATH` or `--source PATH`, but it defaults to the current directory (`pwd`). A patterns file is required, either as the first argument or as a `-p`/`--patterns PATH` somewhere.
* Add gem update instruction to -v
* Bump version to 0.2.0
  • Loading branch information
briandominick authored Jun 7, 2018
1 parent 7e97b3d commit 4aa2d9f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 26 deletions.
76 changes: 52 additions & 24 deletions lib/subtxt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'optparse'
require 'logger'
require 'fileutils'
require 'yaml'

@ingestdir_def = "."
@filext_def = "*"
Expand Down Expand Up @@ -43,25 +44,32 @@ def load_patterns pfile
def subtexts opts
patterns = load_patterns(opts[:patterns])
@logger.info "Reading patterns from #{@options[:patterns]}"
patterns_display = ""
patterns.each do |rec|
fndsize = rec['fnd'].size
fndgap = 90 - fndsize
if fndgap > 5
gapspaces = " "
fndgap.times do gapspaces += "." end
else
gapspaces = fndgap
end
patterns_display += "\n#{rec['fnd']}#{gapspaces}=> #{rec['rep']}"
end
@logger.info "Using patterns:\n#{patterns_display}\n"
routine = {}
routine['files_count'] = 0
routine['files_changed'] = []
routine['files_changed_count'] = 0
routine['log'] = []
Dir.glob(opts[:ingestpath]) do |f|
text = File.read(f)
@logger.debug "Processing file: #{File.basename(f)}"
sandr = []
patterns.each do |rec|
replace = rec['rep'].gsub(/\\n/, "\n")
text.gsub!(/#{rec['fnd']}/, replace)
fnd = rec['fnd']
rep = rec['rep'].gsub(/\\n/, "\n")
if opts[:verbose] or opts[:debug]
matches = text.gsub(/#{fnd}/).count
syms = text.gsub(/#{fnd}/) {|sym| "-#{sym}-"}
if matches > 0
sandr << {:pattern => fnd, :matches => matches, :syms => syms}
unless routine['files_changed'].include? f
routine['files_changed'] << f
end
end
end
text.gsub!(/#{fnd}/, rep)
end
if opts[:verbose] or opts[:debug]
routine['log'] << {:file => f, :matchlog => sandr }
end
unless opts[:expext]
outfile = File.basename f
Expand All @@ -73,10 +81,26 @@ def subtexts opts
FileUtils::mkdir_p(opts[:expath]) unless File.exists?(opts[:expath])
File.open("#{opts[:expath]}/#{outfile}", 'w') { |file| file.write(text) }
@logger.debug "File saved (#{outfile})"
routine['files_count'] += 1
rescue Exception => ex
raise "Failure: #{ex}"
end
end
@logger.info display_results(routine)
end

def display_results routine={}
raise "NoRecordsFound" unless routine['log'].count
output = "Files processed: #{routine['files_count']}"
output << "\nFiles changed: #{routine['files_changed'].size}"
routine['log'].each do |doc|
output << "\nFile: #{doc[:file]}"
output << "\nMatches:"
doc[:matchlog].each do |mch|
output << "\n#{mch[:matches]}: #{mch[:pattern]}"
end
end
output
end

parser = OptionParser.new do|opts|
Expand All @@ -88,7 +112,7 @@ def subtexts opts
Check out http://refiddle.com/ and http://www.rexegg.com/regex-quickstart.html
Pattern files are formatted in 3-row sets. The first row is the find pattern,
the second row is the replace pattern, and he third row delimits the set for
the second row is the replace pattern, and the third row delimits the set for
the convenience of your eyeballs. Like so:
\t---------------------------------------
\tfind pattern
Expand All @@ -102,6 +126,10 @@ def subtexts opts
\t
\tEOF
\t---------------------------------------\n
This procedure generates a copy of each file in a separate directory
(#{@expath_def}/ by default) after replacing each matched pattern with
its pair.
Usage: subtxt [path/to/ingest/dir] [options]
Options:
"""
Expand All @@ -112,31 +140,31 @@ def subtexts opts
end

if ARGV[0].split("").first == "-"
opts.on('-i', '--ingestdir', "Ingest files from this directory. Defaults to current directory. Superceded if a path is passed as\n\t\t\t\t\tthe first argument (subtxt path/to/files -p patterns.rgx). Ex: -i path/to/ingest/dir") do |n|
@options[:ingestdir] = n;
opts.on('-p PATH', '--patterns PATH', "Full (relative or absolute) path to a text file\n\t\t\t\t\tcontaining find & replace patterns in the designated format.\n\t\t\t\t\tREQUIRED. Ex: -p path/to/patterns.rgxp") do |n|
@options[:patterns] = n;
end
else # the first arg has no leading - or --, it must be our path
@options[:ingestdir] = ARGV[0]
@options[:patterns] = ARGV[0]
end

opts.on('-p PATH', '--patterns PATH', "Full (relative or absolute) path to a text file containing find & replace patterns in the\n\t\t\t\t\tdesignated format. REQUIRED. Ex: -p path/to/patterns.rgxp") do |n|
@options[:patterns] = n;
opts.on('-s PATH', '--source PATH', "Ingest files from this directory. Defaults to current directory.\n\t\t\t\t\tSuperceded if a path is passed as the first argument\n\t\t\t\t\t(subtxt path/to/files -p patterns.rgx). Ex: -i path/to/ingest/dir") do |n|
@options[:ingestdir] = n;
end

## TODO recursion
# opts.on('-r', '--recursive', 'Whether to process the input directory recursively (traverse subdirectories).') do
# @options[:recursive] = true
# end

opts.on('-f STRING', '--filext STRING', 'Restrict ingested files to this extension. The first dot (.) is implied. Ex: -f htm') do |n|
opts.on('-f STRING', '--filext STRING', "Restrict ingested files to this extension. The first dot (.) is implied.\n\t\t\t\t\tEx: -f htm") do |n|
@options[:filext] = n;
end

opts.on('-x PATH', '--expath PATH', 'Location for saving the converted files. Ex: -x processed/files/dir') do |n|
@options[:expath] = n;
end

opts.on('--expext STRING', "The export file\'s extension to reassign for all files. The first dot (.) is implied. Defaults to same\n\t\t\t\t\textension as original. Defaults to #{@expath_def} Ex: --expext htm") do |n|
opts.on('--expext STRING', "The export file\'s extension to reassign for all files. The first dot (.)\n\t\t\t\t\tis implied. Defaults to same extension as original. Ex: --expext htm") do |n|
@options[:expext] = n;
end

Expand All @@ -154,7 +182,7 @@ def subtexts opts
end

opts.on_tail('-v', 'Show Subtxt release version') do
puts "You're using Subtxt v#{Subtxt::VERSION}"
puts "You're using Subtxt v#{Subtxt::VERSION}. Get the latest with gem update subtxt"
exit
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/subtxt/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Subtxt
VERSION = "0.1.0"
VERSION = "0.2.0"
end
2 changes: 1 addition & 1 deletion subtxt.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Gem::Specification.new do |spec|
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
# to allow pushing to a single host or delete this section to allow pushing to any host.
if spec.respond_to?(:metadata)
spec.metadata["allowed_push_host"] = "'https://rubygems.org'"
spec.metadata["allowed_push_host"] = "https://rubygems.org"
else
raise "RubyGems 2.0 or newer is required to protect against " \
"public gem pushes."
Expand Down

0 comments on commit 4aa2d9f

Please sign in to comment.