added as_segments to ::IO and ::GxG::ByteArray

This commit is contained in:
G. Gibson 2023-04-02 06:21:15 -07:00
parent b1e71001cc
commit 132fc22671
6 changed files with 543 additions and 471 deletions

View file

@ -1,6 +1,6 @@
Gem::Specification.new do |s| Gem::Specification.new do |s|
s.name = 'gxg-framework' s.name = 'gxg-framework'
s.version = '0.0.54' s.version = '0.0.55'
s.licenses = ['UNLICENSED'] s.licenses = ['UNLICENSED']
s.summary = "GxG Framework" s.summary = "GxG Framework"
s.description = "GxG Framework" s.description = "GxG Framework"
@ -38,31 +38,31 @@ Gem::Specification.new do |s|
s.add_runtime_dependency 'state_machines' s.add_runtime_dependency 'state_machines'
s.add_runtime_dependency 'net-ldap' s.add_runtime_dependency 'net-ldap'
# ### Database Adapters: # ### Database Adapters:
# if ::RUBY_ENGINE == "jruby" if ::RUBY_ENGINE == "jruby"
# s.add_runtime_dependency 'jdbc-sqlite3' s.add_runtime_dependency 'jdbc-sqlite3'
# s.add_runtime_dependency 'jdbc-mysql' s.add_runtime_dependency 'jdbc-mysql'
# s.add_runtime_dependency 'jdbc-postgresql' s.add_runtime_dependency 'jdbc-postgresql'
# s.add_runtime_dependency 'jdbc-as400' s.add_runtime_dependency 'jdbc-as400'
# s.add_runtime_dependency 'jdbc-cassandra' s.add_runtime_dependency 'jdbc-cassandra'
# s.add_runtime_dependency 'jdbc-crate' s.add_runtime_dependency 'jdbc-crate'
# s.add_runtime_dependency 'jdbc-derby' s.add_runtime_dependency 'jdbc-derby'
# s.add_runtime_dependency 'jdbc-filemaker' s.add_runtime_dependency 'jdbc-filemaker'
# s.add_runtime_dependency 'jdbc-firebird' s.add_runtime_dependency 'jdbc-firebird'
# s.add_runtime_dependency 'jdbc-h2' s.add_runtime_dependency 'jdbc-h2'
# s.add_runtime_dependency 'jdbc-hive2' s.add_runtime_dependency 'jdbc-hive2'
# s.add_runtime_dependency 'jdbc-hsqldb' s.add_runtime_dependency 'jdbc-hsqldb'
# s.add_runtime_dependency 'jdbc-jt400' s.add_runtime_dependency 'jdbc-jt400'
# s.add_runtime_dependency 'jdbc-jtds' s.add_runtime_dependency 'jdbc-jtds'
# s.add_runtime_dependency 'jdbc-luciddb' s.add_runtime_dependency 'jdbc-luciddb'
# s.add_runtime_dependency 'jdbc-mssql' s.add_runtime_dependency 'jdbc-mssql'
# s.add_runtime_dependency 'jdbc-nuodb' s.add_runtime_dependency 'jdbc-nuodb'
# ### FIX : openedge requires external .jars in classpath -- exclude for now. ### FIX : openedge requires external .jars in classpath -- exclude for now.
# s.add_runtime_dependency 'jdbc-openedge' s.add_runtime_dependency 'jdbc-openedge'
# s.add_runtime_dependency 'jdbc-orientdb' s.add_runtime_dependency 'jdbc-orientdb'
# s.add_runtime_dependency 'jdbc-phoenix' s.add_runtime_dependency 'jdbc-phoenix'
# ### FIX : redshift is missing the RedshiftJDBC4.jar file -- exclude for now. ### FIX : redshift is missing the RedshiftJDBC4.jar file -- exclude for now.
# s.add_runtime_dependency 'jdbc-redshift' s.add_runtime_dependency 'jdbc-redshift'
# s.add_runtime_dependency 'jdbc-splice' s.add_runtime_dependency 'jdbc-splice'
# s.add_runtime_dependency 'jdbc-vertica' s.add_runtime_dependency 'jdbc-vertica'
# end end
end end

View file

@ -635,6 +635,42 @@ class IO
result result
end end
# #
def as_segments(segment_size=65536, as_bytearrays=false, &block)
result = []
unless self.closed?
if segment_size.is_any?(::TrueClass, ::FalseClass, NilClass)
as_bytearrays = segment_size
segment_size = 65536
end
saved_position = self.pos
self.seek(0, :END)
current_size = self.pos + 1
#
self.rewind
if block.respond_to?(:call)
GxG::apportioned_ranges(current_size, segment_size).each do |portion_range|
self.seek(portion_range.first)
if as_bytearrays
block.call(bytes self.read(portion_range.size))
else
block.call(self.read(portion_range.size))
end
end
else
GxG::apportioned_ranges(current_size, segment_size).each do |portion_range|
self.seek(portion_range.first)
if as_bytearrays
result << bytes self.read(portion_range.size)
else
result << self.read(portion_range.size)
end
end
end
#
self.pos = saved_position
end
result
end
end end
# #
class StringIO class StringIO
@ -1297,467 +1333,467 @@ module Fcntl
end end
end end
# ### URI alterations # ### URI alterations
module URI # module URI
# # #
def self.parse(*args) # def self.parse(*args)
#::URI::Generic.new(*(::URI::split(*args))) # #::URI::Generic.new(*(::URI::split(*args)))
::URI::Generic::parse(*args) # ::URI::Generic::parse(*args)
end # end
# # #
class Generic # class Generic
# # #
def set_scheme(v) # def set_scheme(v)
if v.is_a?(String) # if v.is_a?(String)
@scheme = v # @scheme = v
end # end
v # v
end # end
def scheme=(scheme="") # def scheme=(scheme="")
set_scheme(scheme) # set_scheme(scheme)
scheme # scheme
end # end
def scheme() # def scheme()
@scheme # @scheme
end # end
def set_userinfo(user, password = nil) # def set_userinfo(user, password = nil)
unless password # unless password
user, password = split_userinfo(user) # user, password = split_userinfo(user)
end # end
if user.is_a?(String) # if user.is_a?(String)
@user = URI::escape(user) # @user = URI::escape(user)
else # else
@user = user # @user = user
end # end
if password.is_a?(String) # if password.is_a?(String)
@password = URI::escape(password) # @password = URI::escape(password)
else # else
@password = password # @password = password
end # end
result = [] # result = []
if @user.is_a?(String) # if @user.is_a?(String)
result << URI::unescape(@user) # result << URI::unescape(@user)
else # else
result << @user # result << @user
end # end
if @password.is_a?(String) # if @password.is_a?(String)
result << URI::unescape(@password) # result << URI::unescape(@password)
else # else
result << @password # result << @password
end # end
result # result
end # end
def userinfo # def userinfo
if @user.nil? # if @user.nil?
nil # nil
elsif @password.nil? # elsif @password.nil?
URI::unescape(@user) # URI::unescape(@user)
else # else
URI::unescape(@user) + ':' + URI::unescape(@password) # URI::unescape(@user) + ':' + URI::unescape(@password)
end # end
end # end
def user=(user) # def user=(user)
if user.is_a?(String) # if user.is_a?(String)
user = (URI::escape(user)) # user = (URI::escape(user))
end # end
set_user(user) # set_user(user)
self.user # self.user
# returns user # # returns user
end # end
def user # def user
if @user.is_a?(String) # if @user.is_a?(String)
URI::unescape(@user) # URI::unescape(@user)
else # else
@user # @user
end # end
end # end
def set_password(v) # def set_password(v)
if v.is_a?(String) # if v.is_a?(String)
@password = URI::escape(v) # @password = URI::escape(v)
end # end
if @password.is_a?(String) # if @password.is_a?(String)
URI::unescape(@password) # URI::unescape(@password)
else # else
@password # @password
end # end
end # end
def password=(password) # def password=(password)
if password.is_a?(String) # if password.is_a?(String)
password = (URI::escape(password)) # password = (URI::escape(password))
end # end
set_password(password) # set_password(password)
# returns password # # returns password
end # end
def password # def password
if @password.is_a?(String) # if @password.is_a?(String)
URI::unescape(@password) # URI::unescape(@password)
else # else
@password # @password
end # end
end # end
def set_host(v) # def set_host(v)
if v.is_a?(String) # if v.is_a?(String)
@host = URI::escape(v) # @host = URI::escape(v)
else # else
@host = v # @host = v
end # end
end # end
# # #
def host=(v) # def host=(v)
if v.is_a?(String) # if v.is_a?(String)
v = (URI::escape(v)) # v = (URI::escape(v))
end # end
set_host(v) # set_host(v)
self.host # self.host
end # end
# # #
def host() # def host()
if @host.is_a?(String) # if @host.is_a?(String)
URI::unescape(@host) # URI::unescape(@host)
else # else
@host # @host
end # end
end # end
# # #
def hostname() # def hostname()
if @host.to_s.size > 0 # if @host.to_s.size > 0
self.host().to_s.dup.gsub("[","").gsub("]","") # self.host().to_s.dup.gsub("[","").gsub("]","")
else # else
nil # nil
end # end
end # end
# # #
def hostname=(v) # def hostname=(v)
if (v.to_s.include?(":") || (v.to_s.include?("[") && v.to_s.include?("]"))) # if (v.to_s.include?(":") || (v.to_s.include?("[") && v.to_s.include?("]")))
# ipv6 ?? # # ipv6 ??
if v.to_s.include?("[") && v.to_s.include?("]") # if v.to_s.include?("[") && v.to_s.include?("]")
self.host = v.to_s # self.host = v.to_s
else # else
self.host = "[" << v.to_s << "]" # self.host = "[" << v.to_s << "]"
end # end
else # else
self.host = v.to_s # self.host = v.to_s
end # end
self.host() # self.host()
end # end
# # #
# def set_registry(v) # # def set_registry(v)
# if v.is_a?(String) # # if v.is_a?(String)
# @registry = URI::escape(v) # # @registry = URI::escape(v)
# else # # else
# @registry = v # # @registry = v
# end # # end
# end # # end
# # # # #
# def registry=(v) # # def registry=(v)
# if v.is_a?(String) # # if v.is_a?(String)
# v = (URI::escape(v)) # # v = (URI::escape(v))
# end # # end
# set_registry(v) # # set_registry(v)
# self.registry # # self.registry
# end # # end
# def registry # # def registry
# if @registry.is_a?(String) # # if @registry.is_a?(String)
# URI::unescape(@registry) # # URI::unescape(@registry)
# else # # else
# @registry # # @registry
# end # # end
# end # # end
# def set_path(v) # # def set_path(v)
# if v.is_a?(String) # # if v.is_a?(String)
# @path = URI::escape(v) # # @path = URI::escape(v)
# else # # else
# @path = v # # @path = v
# end # # end
# end # # end
# def path=(v) # # def path=(v)
# if v.is_a?(String) # # if v.is_a?(String)
# v = (URI::escape(v)) # # v = (URI::escape(v))
# end # # end
# set_path(v) # # set_path(v)
# self.path # # self.path
# end # # end
# def path # # def path
# if @path.is_a?(String) # # if @path.is_a?(String)
# URI::unescape(@path) # # URI::unescape(@path)
# else # # else
# @path # # @path
# end # # end
# end # # end
# def set_opaque(v) # # def set_opaque(v)
# if v.is_a?(String) # # if v.is_a?(String)
# @opaque = URI::escape(v) # # @opaque = URI::escape(v)
# else # # else
# @opaque = v # # @opaque = v
# end # # end
# end # # end
# def opaque=(v) # # def opaque=(v)
# if v.is_a?(String) # # if v.is_a?(String)
# v = (URI::escape(v)) # # v = (URI::escape(v))
# end # # end
# set_opaque(v) # # set_opaque(v)
# self.opaque # # self.opaque
# end # # end
# def opaque # # def opaque
# if @opaque.is_a?(String) # # if @opaque.is_a?(String)
# URI::unescape(@opaque) # # URI::unescape(@opaque)
# else # # else
# @opaque # # @opaque
# end # # end
# end # # end
# Review - query modifications bugger MatrixClient logins. # # Review - query modifications bugger MatrixClient logins.
# def set_query(v) # # def set_query(v)
# if v.is_a?(String) # # if v.is_a?(String)
# @query = URI::escape(v) # # @query = URI::escape(v)
# else # # else
# @query = v # # @query = v
# end # # end
# end # # end
# def query=(v) # # def query=(v)
# if v.is_a?(String) # # if v.is_a?(String)
# v = (URI::escape(v)) # # v = (URI::escape(v))
# end # # end
# set_query(v) # # set_query(v)
# self.query # # self.query
# end # # end
# def query # # def query
# if @query.is_a?(String) # # if @query.is_a?(String)
# URI::unescape(@query) # # URI::unescape(@query)
# else # # else
# @query # # @query
# end # # end
# end # # end
# def set_fragment(v) # # def set_fragment(v)
# if v.is_a?(String) # # if v.is_a?(String)
# @fragment = URI::escape(v) # # @fragment = URI::escape(v)
# else # # else
# @fragment = v # # @fragment = v
# end # # end
# end # # end
# def fragment=(v) # # def fragment=(v)
# if v.is_a?(String) # # if v.is_a?(String)
# v = (URI::escape(v)) # # v = (URI::escape(v))
# end # # end
# set_fragment(v) # # set_fragment(v)
# self.fragment # # self.fragment
# end # # end
# def fragment # # def fragment
# if @fragment.is_a?(String) # # if @fragment.is_a?(String)
# URI::unescape(@fragment) # # URI::unescape(@fragment)
# else # # else
# @fragment # # @fragment
# end # # end
# end # # end
# # #
# def to_s # # def to_s
# str = '' # # str = ''
# if @scheme # # if @scheme
# str << @scheme # # str << @scheme
# str << ':' # # str << ':'
# end # # end
# if @opaque # # if @opaque
# str << @opaque # # str << @opaque
# else # # else
# if @registry # # if @registry
# str << @registry # # str << @registry
# else # # else
# if @host # # if @host
# str << '//' # # str << '//'
# end # # end
# if self.userinfo # # if self.userinfo
# str << self.userinfo # # str << self.userinfo
# str << '@' # # str << '@'
# end # # end
# if @host # # if @host
# str << @host # # str << @host
# end # # end
# if @port && @port != self.default_port # # if @port && @port != self.default_port
# str << ':' # # str << ':'
# str << @port.to_s # # str << @port.to_s
# end # # end
# end # # end
# str << @path # # str << @path
# if @query # # if @query
# str << '?' # # str << '?'
# str << @query # # str << @query
# end # # end
# end # # end
# if @fragment # # if @fragment
# str << '#' # # str << '#'
# str << @fragment # # str << @fragment
# end # # end
# URI::escape(str) # # URI::escape(str)
# end # # end
# # #
def from_hash(the_hash={}) # def from_hash(the_hash={})
if the_hash[:scheme] # if the_hash[:scheme]
self.scheme = the_hash[:scheme] # self.scheme = the_hash[:scheme]
else # else
@scheme = nil # @scheme = nil
end # end
if the_hash[:opaque] # if the_hash[:opaque]
self.opaque = the_hash[:opaque] # self.opaque = the_hash[:opaque]
@registry = nil # @registry = nil
@user = nil # @user = nil
@password = nil # @password = nil
@host = nil # @host = nil
@port = nil # @port = nil
@path = nil # @path = nil
@query = nil # @query = nil
else # else
@opaque = nil # @opaque = nil
if the_hash[:registry] # if the_hash[:registry]
self.registry = the_hash[:registry] # self.registry = the_hash[:registry]
@user = nil # @user = nil
@password = nil # @password = nil
@host = nil # @host = nil
@port = nil # @port = nil
else # else
@registry = nil # @registry = nil
if the_hash[:user] # if the_hash[:user]
self.user = the_hash[:user] # self.user = the_hash[:user]
else # else
@user = nil # @user = nil
end # end
if the_hash[:password] # if the_hash[:password]
self.password = the_hash[:password] # self.password = the_hash[:password]
else # else
@password = nil # @password = nil
end # end
if the_hash[:host] # if the_hash[:host]
self.host = the_hash[:host] # self.host = the_hash[:host]
else # else
@host = nil # @host = nil
end # end
if the_hash[:port] && the_hash[:port] != self.default_port # if the_hash[:port] && the_hash[:port] != self.default_port
self.port = the_hash[:port].to_i # self.port = the_hash[:port].to_i
else # else
@port = nil # @port = nil
end # end
end # end
if the_hash[:path] # if the_hash[:path]
self.path = the_hash[:path] # self.path = the_hash[:path]
else # else
@path = nil # @path = nil
end # end
if the_hash[:query] # if the_hash[:query]
self.query = the_hash[:query] # self.query = the_hash[:query]
else # else
@query = nil # @query = nil
end # end
end # end
if the_hash[:fragment] # if the_hash[:fragment]
self.fragment = the_hash[:fragment] # self.fragment = the_hash[:fragment]
else # else
@fragment = nil # @fragment = nil
end # end
self # self
end # end
# # #
def to_hash() # def to_hash()
result = {} # result = {}
if @scheme # if @scheme
result[:scheme] = @scheme # result[:scheme] = @scheme
end # end
if @opaque # if @opaque
result[:opaque] = URI::unescape(@opaque) # result[:opaque] = URI::unescape(@opaque)
else # else
if @registry # if @registry
result[:registry] = URI::unescape(@registry) # result[:registry] = URI::unescape(@registry)
else # else
if @user # if @user
result[:user] = URI::unescape(@user) # result[:user] = URI::unescape(@user)
end # end
if @password # if @password
result[:password] = URI::unescape(@password) # result[:password] = URI::unescape(@password)
end # end
if @host # if @host
result[:host] = URI::unescape(@host) # result[:host] = URI::unescape(@host)
end # end
if @port && @port != self.default_port # if @port && @port != self.default_port
result[:port] = URI::unescape(@port).to_i # result[:port] = URI::unescape(@port).to_i
end # end
end # end
if @path # if @path
result[:path] = URI::unescape(@path) # result[:path] = URI::unescape(@path)
end # end
if @query # if @query
result[:query] = URI::unescape(@query) # result[:query] = URI::unescape(@query)
end # end
end # end
if @fragment # if @fragment
result[:fragment] = URI::unescape(@fragment) # result[:fragment] = URI::unescape(@fragment)
end # end
result # result
end # end
# # #
def address_info() # def address_info()
::Addrinfo::parse(self) # ::Addrinfo::parse(self)
end # end
# # #
def resolve_host() # def resolve_host()
case @scheme # case @scheme
when "inproc" # when "inproc"
# no-op # # no-op
when "ipc", "unix" # when "ipc", "unix"
# no-op # # no-op
when "tcp", "tcp4", "tcp6", "udp", "udp4", "udp6" # when "tcp", "tcp4", "tcp6", "udp", "udp4", "udp6"
self.hostname = ::TCPSocket.getaddress(self.hostname) # self.hostname = ::TCPSocket.getaddress(self.hostname)
end # end
end # end
# # #
end # end
def self.parse(uri) # def self.parse(uri)
# Alterations: # # Alterations:
# leading/trailing whitespace removal - intended to reduce false exception conditions (missing regexp bits) # # leading/trailing whitespace removal - intended to reduce false exception conditions (missing regexp bits)
# I think first reported in 2007, but still not fixed. dunno why not. # # I think first reported in 2007, but still not fixed. dunno why not.
while uri[0] == 32 # while uri[0] == 32
uri[0] = "" # uri[0] = ""
end # end
if uri.size > 0 # if uri.size > 0
while uri[-1] == 32 # while uri[-1] == 32
uri[-1] = "" # uri[-1] = ""
end # end
end # end
# attempt to escape all strings going INTO the URI object? # # attempt to escape all strings going INTO the URI object?
uri = URI::escape(uri) # uri = URI::escape(uri)
# End Alterations # # End Alterations
scheme, userinfo, host, port, # scheme, userinfo, host, port,
registry, path, opaque, query, fragment = self.split(uri) # registry, path, opaque, query, fragment = self.split(uri)
if scheme && @@schemes.include?(scheme.upcase) # if scheme && @@schemes.include?(scheme.upcase)
@@schemes[scheme.upcase].new(scheme, userinfo, host, port, # @@schemes[scheme.upcase].new(scheme, userinfo, host, port,
registry, path, opaque, query, # registry, path, opaque, query,
fragment) # fragment)
else # else
Generic.new(scheme, userinfo, host, port, # Generic.new(scheme, userinfo, host, port,
registry, path, opaque, query, # registry, path, opaque, query,
fragment) # fragment)
end # end
end # end
end # end
# #
class Addrinfo class Addrinfo
# #

View file

@ -569,6 +569,34 @@ module GxG
def data() def data()
@data @data
end end
#
def as_segments(segment_size=65536, as_bytearrays=false, &block)
result = []
if segment_size.is_any?(::TrueClass, ::FalseClass, NilClass)
as_bytearrays = segment_size
segment_size = 65536
end
#
if block.respond_to?(:call)
GxG::apportioned_ranges(self.size, segment_size).each do |portion_range|
if as_bytearrays
block.call(self[portion_range])
else
block.call(self[portion_range].to_s)
end
end
else
GxG::apportioned_ranges(self.size, segment_size).each do |portion_range|
if as_bytearrays
result << (self[portion_range])
else
result << (self[portion_range].to_s)
end
end
end
#
result
end
# #
# instance methods: # instance methods:
def initialize(*args,&block) def initialize(*args,&block)

View file

@ -1,5 +1,7 @@
# # See: https://github.com/crystal-lang/crystal-mysql
if defined?(::External::Database::Mysql) if defined?(::External::Database::Mysql)
else else
require "mysql2" unless ::RUBY_ENGINE == "jruby"
require "mysql2"
end
end end

View file

@ -1,5 +1,7 @@
# # See: https://github.com/will/crystal-pg
if defined?(::External::Database::Postgres) if defined?(::External::Database::Postgres)
else else
require "pg" unless ::RUBY_ENGINE == "jruby"
require "pg"
end
end end

View file

@ -1,5 +1,9 @@
# # See: https://github.com/crystal-lang/crystal-sqlite3
# See: http://sequel.jeremyevans.net/documentation.html
# See: https://github.com/jeremyevans/sequel
if defined?(::External::Database::Sqlite) if defined?(::External::Database::Sqlite)
else else
require "sqlite3" unless ::RUBY_ENGINE == "jruby"
require "sqlite3"
end
end end