Class: File

Inherits:
Object show all
Defined in:
lib/openc3/core_ext/file.rb

Overview

OpenC3 specific additions to the Ruby File class

Direct Known Subclasses

OpenC3::BufferedFile

Constant Summary collapse

NON_ASCII_PRINTABLE =

Non printable ASCII characters

/[^\x21-\x7e\s]/

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_timestamped_filename(tags = nil, extension = '.txt', time = Time.now.sys) ⇒ String

Builds a String for use in creating a file. The time is formatted as YYYY_MM_DD_HH_MM_SS. The tags and joined with an underscore and appended to the date before appending the extension.

For example:

File.build_timestamped_filename(['test','only'], '.bin', Time.now.sys)
# result is YYYY_MM_DD_HH_MM_SS_test_only.bin

Parameters:

  • tags (Array<String>) (defaults to: nil)

    An array of strings to be joined by underscores after the date. Pass nil or an empty array to use no tags.

  • extension (String) (defaults to: '.txt')

    The filename extension

  • time (Time) (defaults to: Time.now.sys)

    The time to format into the filename

Returns:

  • (String)

    The filename string containing the timestamp, tags, and extension



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/openc3/core_ext/file.rb', line 58

def self.build_timestamped_filename(tags = nil, extension = '.txt', time = Time.now.sys)
  timestamp = sprintf("%04u_%02u_%02u_%02u_%02u_%02u", time.year, time.month, time.mday, time.hour, time.min, time.sec)
  tags ||= []
  tags.compact!
  combined_tags = tags.join("_")
  if combined_tags.length > 0
    filename = timestamp + "_" + combined_tags + extension
  else
    filename = timestamp + extension
  end
  return filename
end

.find_in_search_path(filename) ⇒ String

Returns The full path to the filename if it was found in the Ruby search path. nil if the fild was not found.

Parameters:

  • filename (String)

    The file to search for

Returns:

  • (String)

    The full path to the filename if it was found in the Ruby search path. nil if the fild was not found.



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/openc3/core_ext/file.rb', line 74

def self.find_in_search_path(filename)
  $:.each do |load_path|
    Find.find(load_path) do |path|
      Find.prune if /\.svn/.match?(path)
      return path if File.basename(path) == filename
    end
  rescue Errno::ENOENT
    # Ignore non-existent folders
    next
  end
  return nil
end

.is_ascii?(filename) ⇒ Boolean

Returns Whether the file only contains ASCII characters.

Returns:

  • (Boolean)

    Whether the file only contains ASCII characters



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/openc3/core_ext/file.rb', line 31

def self.is_ascii?(filename)
  return_value = true
  File.open(filename) do |file|
    while buf = file.read(1024)
      if NON_ASCII_PRINTABLE.match?(buf)
        return_value = false
        break
      end
    end
  end
  return return_value
end

Instance Method Details

#deleteObject



87
88
89
90
# File 'lib/openc3/core_ext/file.rb', line 87

def delete
  self.close
  File.delete(self.path)
end