Skip to content

Instantly share code, notes, and snippets.

@thomasjslone
Last active January 26, 2017 20:01
Show Gist options
  • Select an option

  • Save thomasjslone/03bfde2301f6a5e082f3507722b2f944 to your computer and use it in GitHub Desktop.

Select an option

Save thomasjslone/03bfde2301f6a5e082f3507722b2f944 to your computer and use it in GitHub Desktop.
ossy version w vdr and gmail api
def map_dir(dir)
if File.directory?(dir)
remaining = [dir] ; @found_files = [] ; @file_size = [] ; @found_folders = []
until remaining.length == 0
cur_dir = remaining[0].to_s ; remaining.delete_at(0)
cont = Dir.entries(cur_dir) ; cont.delete(".") ; cont.delete("..")
cont.each do |obj|
if File.file?(cur_dir.to_s + "/" + obj.to_s)
@found_files << cur_dir.to_s + "/" + obj.to_s ; @file_size << File.size?(@found_files[-1])
elsif File.directory?(cur_dir.to_s + "/" + obj.to_s)
@found_folders << cur_dir.to_s + "/" + obj.to_s ; remaining << cur_dir.to_s + "/" + obj.to_s
end
end
end
return [@file_size,@found_files,@found_folders]
else
"Invalid Directory: " + dir.to_s
end
end
def splice(str,b,e) ## for extracting tags, such as html
res = []
loop do
p = str[/#{b}(.*?)#{e}/m, 1].to_s
if p.to_s.length == 0
break
else
res << p
str = str.split(b + p + e)[-1].to_s
end
end
return res
end
def rands(l) ## returns a random string which can contain multicase letters and numbers
key = ""
l.times do
key << rand(10**8..10**9).to_s(36).to_s[rand(6).to_i].to_s
end
return key.to_s
end
def numeric?(s) ## string has numbers only then true
return s !~ /\D/
end
def fnum(n) ## put commas in large numbers for readability
str = ''
s = n.to_s.split("").reverse
i=0
s.each do |nc|
if i == 2
i=0
str << nc.to_s + ","
else
str << nc.to_s
i+=1
end
end
if str.to_s[-1].to_s == ","
str = str.reverse.to_s.split("")[1..-1].join("").to_s
else
str = str.reverse.to_s
end
return str
end
def list_spacer(spaces,left,right) ## give two arrays of words of varying lengths, and determine how far they should be spaced apart, they will descend down the sccreen next to each other at a set distance from eachother , use with the same array to build morw than two rows
width = spaces .to_i+ 5
str = ''
left.each do |l|
spacer = ""
t = width - l.length.to_i
t.to_i.times { spacer << " " }
str << l.to_s + spacer.to_s + right[left.index(l)].to_s + "\n"
end
return str.to_s
end
def numerize(str) ## turn any string into positive only numbers that never start with zero
if str == ""
return 0
else
chbytes = ["97", "98", "99", "100", "101", "102", "103", "104", "105", "106", "107", "108", "109", "110", "111", "112", "113", "114", "115", "116", "117", "118", "119", "120", "121", "122", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "126", "96", "33", "64", "35", "36", "37", "94", "38", "42", "40", "41", "45", "95", "61", "43", "91", "93", "123", "125", "59", "58", "34", "39", "92", "47", "124", "63", "60", "62", "44", "46", "32", "9", "10"]
ch_inds = []
str.to_s.split("").each { |ch| ch_inds << chbytes.index(ch.to_s.ord.to_s) }
enc = []
ch_inds.each do |ch|
code = ch.to_i + 1
if code.to_s.length == 1
code = "0" + code.to_s
else
code = code.to_s
end
enc << code.to_s
end
enc = enc.join("").to_s
if enc.to_s[0].to_s == "0"
enc = enc[1..-1].to_s
end
return enc.to_s
end
end
def denumerize(str) ## get your old string back from the numbers you generated
kbchars = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9","~","`","!","@","#","$","%","^","&","*","(",")","-","_","=","+","[","]","{","}",";",":","\"","'","\\","/","|","?","<",">",",","."," ","\t","\n"]
if str.to_s.length.odd?
str = "0" + str.to_s
end
str_codes = []
i = 0
hold = ""
str.to_s.split("").each do |num|
if i.to_i == 0
hold << num.to_s
i += 1
elsif i.to_i == 1
hold << num.to_s
i = 0
str_codes << hold.to_s
hold = ""
end
end
str = str_codes
str_codes = []
str.each do |c|
str_codes << c.to_i - 1
end
dec_str = ""
str_codes.each { |c| dec_str << kbchars[c.to_i].to_s }
return dec_str.to_s
end
def exponate(n) ## take the given number and calculate up for exponets which would result in it, stay below five digits
c=2
e=2
r = [0,0]
until c > n
e = 2
until e >= n
if c**e == n
return [c,e]
end
e += 1
end
c += 1
end
end
def factors(n) ## get two numbers that which multiplied together would result in the given value, stay below 4 digits
st = Time.now
p = [2]
vn = 2
until vn == n
vn += 1
p << vn
end
p.delete_at(-1)
f1 = 0
f2 = 0
pd = []
p.each do |pn|
s = n.to_f / pn.to_f
if s.to_s[-2..-1].to_s == ".0"
pd << pn
end
end
pd.each do |p|
if p * p == n
f1, f2 = p, p
else
cd = pd
cd.delete(p)
cd.each do |pr|
if p * pr == n
f1, f2 = p, pr
break
end
end
end
end
et = Time.now ; rt = et - st
return [f1,f2,rt.to_s]
end#;#;#
class VDrive
## V1.2.2
## Methods:
## - file_exist?(STRING) path returns boolean
## - folder_exist?(STRING) path returns boolean
## - ftype?(STRING) path returns "FILE" , "FOLDER" , "CORRUPTED OBJ"
## - read_file(STRING) path returns string contained in file
## - write_file(STRING,STRING) path, content returns string written, if file does not exist it will be created, otherwise it will be overwritten
## - copy_file(STRING,STRING) path, newpath returns true
## - create_folder(STRING) path returns true
## - copy_folder(STRING,STRING) path, newpatj returns true
## - delete_file(STRING) path returns true
## - delete_folder(STRING) path returns true
## - rename_file(STRING,STRING) path, newname returns true
## - rename_folder(STRING,STRING) path, newname returns true
## - import_file(STRING,STRING) vdrive_path, os_filepath return location of imported file
## - export_file(STRING,STRING) vdrive_path, os_filepath returns os path file exported to
## - import_folder(STRING,STRING) vdrive_path, os_folderpath returns location of folder imported
## - export_folder(STRING,STRING) vdrive_path, os_filepath
## - export_disk(path,password) os_filepath returns true
## - import_disk(path,password) os_filepath returns true
## - save_disk writes the file system disk down into storage in this source code
## - load_disk loads the file system disk from this source code
## - format_disk formats disk to brandnew then saves it
## - disk_size counts the amount of integers it takes to represent your disk
## - disk_label returns the disk label name
## - rename_disk(newname) change the disk label
## - mode? returns the disk access mode
## - set_mode(mode) sets the disk access mode
## - auto_save save any itme a change is made
## - dont_auto_save turn off auto save
## - generate_image(files,folders) for use by methods when in direct mode returns STRING
## - process_image(img) for use by methods in direct mode returns [@files,@folders]
def initialize
@default_disk = "v" ; @mounted_volumes = [] ; @mounted_disks = []
###DISK DATA BELOW###
@disks = ['6767672284',0,0,0,0]
@volumes = ['v','w','x','y','z']
###DISK DATA ABOVE###
@mode = "instance" # instance loads the file system from this source code and direct writes/reads to and from it
@auto_save = true # this tells vdrive to save after any change to a file or folder, not nessecary in direct mode obviously
# load_disk # file system auto loads when initialized
end
def file_exist?(loc)
if @mounted_volumes.include?(loc[0].to_s)
if @mode == "instance"
instance_file_exist?(loc)
elsif @mode == "direct"
direct_file_exist?(loc)
end
else
"Disk '" + vol.to_s + ":/' is not mounted or is an invalid volume label."
end
end
def instance_file_exist?(loc)
vol = loc[0].to_s ; found = false ; i = 0
@mounted_disks[@mounted_volumes.index(vol)]#.each do |f|
# if f.to_s.split("@")[0].to_s + "/" + f.to_s.split("@")[1].to_s == loc.to_s
# found = i ; break
# end
#i += 1
#end
#return found
end
def direct_file_exist(loc)
end
def folder_exist?(loc)
if @mounted_volumes.include?(loc[0].to_s)
if @mode == "instance"
instance_folder_exist?(loc)
elsif @mode == "direct"
direct_folder_exist?(loc)
end
else
"Disk '" + vol.to_s + ":/' is not mounted or is an invalid volume label."
end
end
def instance_folder_exist?(loc)
vol = loc[0].to_s ; if @mounted_disks[@mounted_volumes.index(vol)][1].include?(loc) ; @mounted_disks[@mounted_volumes.index(loc[0].to_s)][1].index(loc) ; else ; false ; end
end
def direct_folder_exist?(loc)
end
def ftype?(loc)
if @mounted_volumes.include?(loc[0].to_s)
if @mode == "instance"
instance_ftype?(loc)
elsif @mode == "direct"
direct_ftype?(loc)
end
else
"Disk '" + vol.to_s + ":/' is not mounted or is an invalid volume label."
end
end
def instance_ftype?(loc)
if instance_folder_exist?(loc).ia_a? Integer
"FOLDER"
elsif instance_file_exist?(loc).is_a? Integer
"FILE"
else
"FALSE"
end
end
def direct_ftype?(loc)
end
def read_file(loc)
if @mounted_volumes.include?(loc[0].to_s)
if @mode == "instance"
instance_read_file(loc)
elsif @mode == "direct"
direct_read_file(loc)
end
else
"Disk '" + vol.to_s + ":/' is not mounted or is an invalid volume label."
end
end
def instance_read_file(loc)
vol = loc[0].to_s ; i = instance_file_exist?(loc)
if i.is_a? Integer
denumerize(@mounted_disks[@mounted_volumes.index(vol)][0][i].to_s.split("@")[2].to_s).to_s
else
"FILE NO EXIST ERROR"
end
end
def direct_read_file(loc)
end
def write_file(loc,dat)
if @mounted_volumes.include?(loc[0].to_s)
if @mode == "instance"
instance_write_file(loc,dat)
elsif @mode == "direct"
direct_write_file(loc,dat)
end
else
"Disk '" + loc[0].to_s + ":/' is not mounted or is an invalid volume label."
end
end
def instance_write_file(loc,dat) ## enter valid folder path and append a file name, if it doesnt exist it will be created
vol = loc[0].to_s ; path = loc.to_s.split(":/")[-1].to_s ; i = instance_file_exist?(loc)
vol.to_s + ":/" + loc.split(":/")[-1].split("/")[0..-2].join("/").to_s
# rf = loc.to_s.split("/")[0..-2].join("/").to_s + "/"
#else
# rf = loc.to_s.split("/")[0..-2].join("/").to_s
# end
#if instance_folder_exist?(rf).is_a? Integer
# if i.is_a? Integer ## overwrite existing file
# @mounted_disks[@mounted_volumes.index(vol)][0][i] = @mounted_disks[@mounted_volumes.index(vol)][0][i].to_s.split("@")[0..1].join("@").to_s + "@" + numerize(dat.to_s).to_s
# else ## write to new file
# @mounted_disks[@mounted_volumes.index(vol)][0] << loc.to_s.split("/")[0..-2].join("/").to_s + "@" + loc.to_s.split("/")[-1].to_s + "@" + numerize(dat.to_s).to_s
# end
# else
# "ERROR ; DIR NO-EXIST: " + loc.to_s.split("/")[0..-2].join("/").to_s
#end
end
def direct_write_file(loc,dat)
end
def delete_file(loc)
if @mounted_volumes.include?(loc[0].to_s)
if @mode == "instance"
instance_delete_file(loc.to_s)
elsif @mode == "direct"
direct_delete_file(loc.to_s)
end
else
"Disk '" + loc[0].to_s + ":/' is not mounted or is an invalid volume label."
end
end
def instance_delete_file(loc)
found = false ; i = 0
vol = loc[0].to_s
i = instance_file_exist?(loc)
if i.is_a? Integer
@mounted_disks[@mounted_volumes.index(vol)][0].delete_at(i)
if @auto_save ; save_disk ; end
true
else
"FILE NON-EXISTENT"
end
end
def direct_delete_file(loc)
end
def folder_contents(loc)
if @mounted_volumes.inculde?(loc[0].to_s)
if @mode == "instance"
instance_folder_contents(loc)
elsif @mode == "direct"
direct_folder_contents(loc)
end
else
"Disk '" + vol.to_s + ":/' is not mounted or is an invalid volume label."
end
end
def instance_folder_contents(loc) ## returns list of paths of subfolders and files contained in given folder path
vol = loc[0].to_s
i = instance_folder_exist?(loc)
if i.is_a? Integer
@mounted_disks[@mounted_volumes.index(vol)][0].each do |f|
if f.to_s.split("@")[0].to_s == loc.to_s
contents << f.to_s.split("@")[0].to_s + "/" + f.to_s.split("@")[1].to_s
end
end
@mounted_disks[@mounted_volumes.index(loc[0].to_s)][1].each do |f|
if f.to_s.split("/")[0..-2].join("/").to_s == loc.to_s
contents << f.to_s
end
end
if contents.length == 0
return "empty"
else
return contents
end
else
"FOLDER NON-EXISTENT"
end
end
def direct_folder_contents(loc)
end
def create_folder(loc)
if @mode == "instance"
instance_create_folder(loc)
elsif @mode == "direct"
direct_create_folder(loc)
end
end
def instance_create_folder(loc)
if loc.to_s[0..2] == ("v:/")
if @mounted_disks[@mounted_volumes.index(loc[0].to_s)][1].include?(loc.to_s.split("/")[0..-2].join("/").to_s)
@mounted_disks[@mounted_volumes.index(loc[0].to_s)][1] << loc.to_s
if @auto_save ; save_disk ; end ; true
else
"ROOT FOLDER DOES NOT EXIST: " + loc.to_s.split("/")[0..-2].join("/").to_s
end
else
"MUST CREATE FOLDERS WITHIN V:/"
end
end
def direct_create_folder(loc)
end
def delete_folder(loc)
if @mode == "instance"
instance_delete_folder(loc)
elsif @mode == "direct"
direct_delete_folder(loc)
end
end
def instance_delete_folder(loc)
cfiles = [] ; cfolders = [] ; remaining_folders = [loc]
until remaining_folders.length == 0
cont = read_folder(remaining_folders[0]) ; cfolders << remaining_folders[0] ; remaining_folders.delete_at(0)
unless cont.to_s == "empty"
unless cont[0].to_s == "empty"
cont[0].each { |c| cfiles << c.to_s }
end
unless cont[1].to_s == "empty"
cont[1].each { |c| remaining_folders << c.to_s }
end
end
end
unless cfiles.length < 1
cfiles.each { |f| @mounted_disks[@mounted_volumes.index(loc.to_s.split(":")[0])][0].delete(f.to_s) }
end
unless cfolders.length < 1
cfolders.each { |f| @mounted_disks[@mounted_volumes.index(loc[0].to_s)][1].delete(f.to_s) }
end
if @auto_save ; save_disk ; end
true
end
def rename_file(loc,newname)
if @mode == "instance"
instance_rename_file(loc,newname)
elsif @mode == "direct"
direct_rename_file(loc,newname)
end
end
def instance_rename_file(loc,newname)
if file_exist?(loc).is_a? Integer
@mounted_disks[@mounted_volumes.index(loc.to_s.split(":")[0])][0][found] = @mounted_disks[@mounted_volumes.index(loc.to_s.split(":")[0])][0][found].to_s.split("@")[0].to_s + "@" + newname.to_s + "@" + files[found].to_s.split("@")[2].to_s
if @auto_save ; save_disk ; end ; true
else
"FILE NON-EXISTENT"
end
end
def direct_rename_file(loc,newname)
end
def rename_folder(loc,newname)
if @mode == "instance"
instance_rename_folder(loc,newname)
elsif @mode == "direct"
direct_rename_folder(loc,newname)
end
end
def instance_rename_folder(loc,newname)
end
def direct_rename_folder(loc,newname)
end
def import_file(loc,os_file_path)
if @mode == "instance"
instance_import_file(loc,os_file_path)
elsif @mode == "direct"
direct_import_file(loc,os_file_path)
end
end
def instance_import_file(loc,os_file_path)
if ftype?(loc.to_s).to_s == "FOLDER"
if File.exist?(os_file_path.to_s)
begin
f = File.open(os_file_path.to_s,"r") ; cont = f.read.to_s ; f.close
write_file(loc.to_s + "/" + os_file_path.to_s.split("/")[-1].to_s,cont.to_s)
if @auto_save ; save_disk ; end
file_exist?(loc.to_s + "/" + os_file_path.to_s.split("/")[-1].to_s).is_a? Integer
rescue
"Could not access file: " + os_file_path.to_s
end
else
"Invalid File Path: " + os_file_path.to_s
end
else
"INVALID IMPORT LOCATION: " + loc.to_s
end
end
def direct_import_file(loc,os_file_path)
end
def export_file(loc,os_directory)
if @mode == "instance"
instance_export_file(loc,os_directory)
elsif @mode == "direct"
direct_export_file(loc,os_directory)
end
end
def instance_export_file(loc,os_directory)
if File.directory?(os_directory)
if ftype(loc.to_s).to_s == "FILE"
cont = read_file(loc.to_s).to_s
begin
f = File.open(os_directory.to_s + "/" + loc.to_ss,"w") ; f.write(cont.to_s) ; f.close
File.exist?(os_directory.to_s + "/" + loc.to_s.split("/")[-1].to_s)
rescue
"Could not write to directory: " + os_directory.to_s
end
else
"Invalid Export File: " + loc.to_s
end
else
"Invalid Export Directory: " + os_directory.to_s
end
end
def direct_export_file(loc,os_directory)
end
def import_folder(loc,os_diectory)
if @mode == "instance"
instance_import_folder(loc,os_directory)
elsif @mode == "direct"
direct_import_folder(loc,os_directory)
end
end
def instance_import_folder(loc,os_file_path)
end
def direct_import_folder(loc)
end
def export_folder(loc,os_directory)
if @mode == "instance"
instance_export_folder(loc,os_directory)
elsif @mode == "direct"
direct_export_folder(loc,os_directory)
end
end
def instance_export_folder(loc,os_directory)
end
def direct_export_folder(loc,os_directory)
end
def export_disk(vol,os_file_path,password)
if @mounted_volumes.inculde?(vol)
if @mode == "instance"
instance_export_disk(vol,os_file_path,password)
elsif @mode == "direct"
direct_export_disk(vol,os_file_path,password)
end
else
"Disk '" + vol.to_s + ":/' is not mounted or is an invalid volume label."
end
end
def instance_export_disk(vol,diskfilepath,password)
if password == nil or password == "" or password == "0" or password == 0
pv = 0
else
pv = numerize(password.to_s).to_i
end
img = numerize(numerize(@mounted_disks[@mounted_volumes.index(loc.to_s.split(":")[0])][0].join(",") + "###" + @mounted_disks[@mounted_volumes.index(loc[0].to_s)][1].join(","))).to_i * pv - diskfilepath.to_s.length.to_i
file = File.new(diskfilepath,"w")
file.write(img.to_s) ; file.close
diskfilepath.to_s
end
def direct_export_disk(vol,os_file_path,password)
end
def import_disk(vol,os_file_path,password)
if @mounted_volumes.include?(vol)
if @mode == "instance"
instance_import_disk(vol,os_file_path,password)
elsif @mode == "direct"
direct_import_disk(vol,os_file_path,password)
end
else
"Disk '" + vol.to_s + ":/' is not mounted or is an invalid volume label."
end
end
def instance_import_disk(vol,diskfilepath,password)
if password == nil or password == '' or password == 0
pv = 0
else
pv = numerize(password.to_s).to_i
end
file = File.open(diskfilepath,"r")
img = file.read.to_s ; file.close
img = img.to_i / pv.to_i + diskfilepath.length.to_i
@mounted_disks[@mounted_volumes.index(loc.to_s.split(":")[0])][0] = denumerize(img.to_s).to_s.split("###")[0].to_s.split(",")
@mounted_disks[@mounted_volumes.index(loc[0].to_s)][1] = denumerize(img.to_s).to_s.split("###")[1].to_s.split(",")
end
def direct_import_disk(vol,os_file_path,password)
end
def format_disk(vol)
if @mounted_volumes.include?(vol)
if @mode == "instance"
instance_format_disk(vol)
elsif @mode == "direct"
direct_format_disk(vol)
end
else
"Disk '" + vol.to_s + ":/' is not mounted or is an invalid volume label."
end
end
def instance_format_disk(vol)
@mounted_disks[@mounted_volumes.index(vol)][0] = [] ; @mounted_disks[@mounted_volumes.index(vol)][1] = [vol.to_s + ':'] ; save_disk(vol) ; true
end
def direct_format_disk(vol)
end
def disk_size(vol)
if @mounted_volumes.include?(vol)
if @mode == "instance"
instance_disk_size(vol)
elsif @mode == "direct"
direct_disk_size(vol)
end
else
"Disk '" + vol.to_s + ":/' is not mounted or is an invalid volume label."
end
end
def instance_disk_size(vol)
@mounted_disks[@mounted_volumes.index(vol)].to_s.length.to_i
end
def direct_disk_size(vol)
file = File.open(__FILE__,"r") ; fcont = file.read.to_s ; file.close
icont = fcont.to_s.split("###DISK " + "DATA BELOW###")[-1].to_s.split("###DISK " + "DATA ABOVE###")[0]
volumes = eval("[" + icont.to_s.split("\n")[2].to_s.split("@volumes = [")[1].to_s[0..-3].to_s + "]")
disks = (icont.to_s.split("\n")[1].split(" = ")[1].to_s)
disks[volumes.index(vol)].to_s.length
end
def mounted?
return @mounted_volumes
end
def mount(vol)
if @mounted_disks.include?(vol)
"Disk '" + vol.to_s + "' is already mounted."
else
file = File.open(__FILE__,"r") ; fcont = file.read.to_s ; file.close
icont = fcont.to_s.split("###DISK " + "DATA BELOW###")[-1].to_s.split("###DISK " + "DATA ABOVE###")[0]
volumes = eval("[" + icont.to_s.split("\n")[2].to_s.split("@volumes = [")[1].to_s[0..-3].to_s + "]")
disks = (icont.to_s.split("\n")[1].split(" = ")[1].to_s)
if volumes.include?(vol)
@mounted_volumes << vol.to_s ; @mounted_disks << [[],[]] ; load_disk(vol)
true
else
"Invalid Volume Label: " + vol.to_s + ", No such disk attached."
end
end
end
def unmount(vol)
if @mounted_volumes.include?(vol)
i = @mounted_volumes.index(vol)
@mounted_volumes.delete_at(i) ; @mounted_disks.delete_at(i)
true
else
"Disk '" + vol.to_s + ":/' is not mounted or is an invalid volume label."
end
end
def load_disk(vol)
# if @mounted_volumes.include?(vol)
file = File.open(__FILE__,"r") ; fcont = file.read.to_s ; file.close
icont = fcont.to_s.split("###DISK " + "DATA BELOW###")[-1].to_s.split("###DISK " + "DATA ABOVE###")[0]
volumes = eval("[" + icont.to_s.split("\n")[2].to_s.split("@volumes = [")[1].to_s[0..-3].to_s + "]")
disks = (icont.to_s.split("\n")[1].split(" = ")[1].to_s)
img = disks[volumes.index(vol)]
@mounted_disks[@mounted_volumes.index(vol)][0] = denumerize(img.to_s).to_s.split("###")[0].to_s.split(",")
@mounted_disks[@mounted_volumes.index(vol)][1] = denumerize(img.to_s).to_s.split("###")[1].to_s.split(",")
# else
# "Disk '" + vol.to_s + ":/' is not mounted or is an invalid volume label."
# end
end
def save_disk#(vol)
# if @mounted_volumes.include?(vol)
img = numerize(@mounted_disks[@mounted_volumes.index(vol)][0].join(",") + "###" + @mounted_disks[@mounted_volumes.index(vol)][1].join(","))
file = File.open(__FILE__,"r") ; fcont = file.read.to_s ; file.close
icont = fcont.to_s.split("###DISK " + "DATA BELOW###")[-1].to_s.split("###DISK " + "DATA ABOVE###")[0]
volumes = eval("[" + icont.to_s.split("\n")[2].to_s.split("@volumes = [")[1].to_s[0..-3].to_s + "]")
disks = (icont.to_s.split("\n")[1].split(" = ")[1].to_s)
disks[volumes.index(vol)] = img.to_s
first = fcont.to_s.split("###DISK " + "DATA BELOW###")[0].to_s + "###DISK " + "DATA BELOW###"
middle = "@disks = " + disks.to_s + "\n" + "@volumes = " + volumes.to_s + "\n"
last = "###DISK " + "DATA ABOVE###" + fcont.to_s.split("###DISK " + "DATA ABOVE###")[-1].to_s
file = File.open(__FILE__,"w") ; file.write(first.to_s + "\n" + middle.to_s + "\n" + last.to_s) ; file.close
true
# else
# "Disk '" + loc[0].to_s + ":/' is not mounted or is an invalid volume label."
# end
end
end
require 'ruboto/activity'
require 'ruboto/widget'
require 'ruboto/util/toast'
ruboto_import_widgets :TextView, :LinearLayout, :Button, :ListView, :EditText
class Ossy_Activity
def on_create(b)
super
@vdr = VDrive.new()
@debug_mode = true
@loaded_files = []
@clock_speeds = [0.05,0.10,0.25,0.33,0.5,1.0,2.0,5.0,10.0,15.0,30.0,60.0,120.0,300.0,600.0,900.0,1200.0,1800.0,2700.0,3600.0] # twentieth s |tenth | quarter s | third s | half s | one s | two s five s | ten s | quarter m | half m | one m | two m | five m | ten m | fifteen m | twenty m | thirty m | forty-five m | sixty m
@task_delay = 0.0
@bg_clock = 2.5
@config = ""
@look_for_config = true
@task_set = []
@task_que = []
@log = []
@cycle_counter = 0
@task_counter = 0
@run_main = false
@run_tasks = false
@trigger = false
@main_thread = nil
build_ui("shell")
end
def start
if @run_main == false
@main_thread = Thread.new { run_main }
end
end
def stop
@run_main = false ; sleep @bg_clock.to_f
@main_thread.kill ; @main_thread = nil
end
def state?
if @run_main and @run_tasks
"Active"
elsif @run_main and @run_tasks == false
"Idle"
elsif @run_main == false
"Dormant"
end
end
def build_ui(ui)
if ui == "shell"
setTitle "Welcome to your Shell"
set_content_view(linear_layout(:orientation => :vertical) do
@fid = 0 ; scl = 10 ; $scl = scl - scl - scl ; @sc1 = $scl ; @sc2 = -1 ; $display_lines = [] ; $display = "" ; scl.times { $display_lines << "" ; $display = $display_lines.join("\n") }
@display = text_view :text => $display_lines.join("\n") ; @wrap_length = 100
set_content_view(linear_layout(:orientation => :horizontal) do
@fid_label = text_view :text => "<" + @fid.to_s + ": "
@input_bar = edit_text
button :text => "E", :on_click_listener => (proc{forward_input}) , :height => 60 , :width => 40
end)
end)
elsif ui == "pannel"
set_content_view(
linear_layout(:orientation => :vertical) do
text_view :text => "Trigger: #{$ossy.instance_variable_get('@trigger').to_s}, State: #{$ossy.state?.to_s}, Cycles: #{$ossy.instance_variable_get('@cycle_counter').to_s}, Tasks Complated: #{$ossy.instance_variable_get('@task_counter').to_s}"
text_view :text => "Cycle Delay: #{$ossy.instance_variable_get('@bg_clock').to_s}, Task Delay: #{$ossy.instance_variable_get('@task_delay').to_s}"
linear_layout(:orientation => :horizontal) do
text_view :text => "Config: " + cfg.to_s
@cfg_bar = edit_text
button :text => "Configure", :on_click_listener => (proc{set_cfg}) , :height => 60
end
button :text => "Back to Shell", :on_click_listener => (proc{build_ui("shell")}) , :height => 60
end
)
end
end
def run_main
if @run_main == false
@run_main = true ; @start_time = Time.now
while @run_main
c = configure
p = process_ques
t = do_tasks
@cycle_counter += 1
sleep @bg_clock.to_f
end
@run_main = false
end
@main_thread.kill ; @main_thread = nil
end
def que_task(timestr,task) # time format: 2016.12.31.23.59.59
tt = timestr.to_s.split(".") ; tt = Time.new(tt[0],tt[1],tt[2],tt[3],tt[4],tt[5])
if tt > Time.now
@task_que << timestr.to_s + "@" + numerize(task.to_s).to_s
else
return "Invalid task time"
end
end
def configure
temp = false
if @config.to_s.length > 0
begin ; eval(@config.to_s) ; rescue ; ct = "CFG EXEC FAILED" ; end ; @config = '' ; ct = true
elsif @look_for_config and @config == "" and File.exist?(@homedir.to_s + "/ossy_config")
file = File.open(@homedir.to_s + "/ossy_config","r") ; cfg = file.read.to_s.split("\n") ; file.close
File.delete(@homedir.to_s + "/ossy_config")
begin
eval(cfg.join("\n").to_s) ; ct = true
rescue
ct = "CFG FILE EXEC FAILED"
end
elsif @look_for_config and @config == "" and $vdr.file_exist?("v:/ossy_config").is_a? Integer
cfg = $vdr.read_file("v:/ossy_config").to_s.split("\n")
$vdr.delete_file("v:/ossy_config")
begin
eval(cfg.join("\n").to_s) ; ct = true
rescue
ct = "CFG VFILE EXEC FAILED"
end
elsif @config == ""
ct = false
## no cfg, shouldnt happen but can, maybe check online or something
end
return ct.to_s
end
def process_ques
qp = 0
if @task_que.length > 0
ct = Time.now ; exp = []
@task_que.each do |t|
if t.split("@")[0].to_s == "p" # time str
tt = t.split("@")[1].split(".") ; tt = Time.new(tt[0],tt[1],tt[2],tt[3],tt[4],tt[5])
if Time.now > tt
@task_set << denumerize(t.split("@")[2].to_s).to_s ; @task_que.delete_at(i)
end
elsif t.to_s.split("@")[0] == "r" ## 12.31.23.59 (leave zero for unspecified)
tt = t.split("@")[1].split(".")
ct = Time.now.to_s.split(" ").join(".").split(":").join(".").split("-").join(".").split(".")[1..-1]
nct = [] ; ci 0
tt.each do |t|
if t.to_i == 0
nct << 0
else
nct << ct[ci]
end
ci += 1
end
tt = Time.new(0,tt[0],tt[1],tt[2],tt[3])
ct = Time.new(0,nct[0],nct[1],nct[2],nct[3])
if ct == tt
@task_set << denumerize(t.split("@")[2].to_s).to_s
end
end
i += 1
end
if exp.length > 0
exp.each do |e|
@task_set << denumerize(@task_que[e].to_s.split("@")[-1].to_s).to_s
@task_que.delete_at(e)
end
end
end
return qp.to_i
end
def info
str = "\n"
if @config == "" ; cf = "none" ; else ; cf = @config.to_s.split("\n").join(";").to_s ; end
str << "State: " + state?.to_s + "\n"
str << "Trigger: " + @trigger.to_s + "\n"
str << "Cycle Clock: " + @bg_clock.to_s + "\n"
str << "Cycles Elapsed: " + @cycle_counter.to_s + "\n"
str << "Current Task Load: " + @task_set.length.to_s + "\n"
str << "Total Tasks Performed: " + @task_counter.to_s + "\n"
str << "Task Clock: " + @task_delay.to_s + "\n"
str << "Tasks Qued: " + @task_que.length.to_s + "\n"
return str.to_s
end
def que_task(m,timestr,task)
@task_que << m.to_s + "@" + timestr.to_s + "@" + numerize(task.to_s).to_s
end
def do_tasks
tt = 0
if @task_set.length > 0
i = 1 ; tasks = @task_set ; @run_tasks = true
tasks.each do |t|
begin
res = eval(t.to_s).to_s ; @task_counter += 1 ; tt += 1 ; @log << res
rescue
@log << "Failure at line " + i.to_s + " : " + t.to_s
end
i += 1
sleep @task_delay.to_f
end
if @trigger == true
## repeat task set next cycle
elsif @trigger == false
@task_set = []
elsif @trigger.is_a? Integer
if @trigger > 1
@trigger -= 1
else
@trigger = false
end
else @trigger.is_a? Time
if Time.now > @trigger
@task_set = [] ; @trigger = false
end
end
@run_tasks = false
end
return tt.to_i
end
def general_request(inp)
if inp[0].to_s == "<" ## process ruby code
begin
return eval(inp[1..-1].to_s)
rescue
return "Execution Failed"
end
elsif inp[0..2].to_s == "os:" #system call
retur eval("`" + inp[3..-1].to_s + "`")
elsif inp.to_s == "time"
return Time.now.to_s
elsif inp.to_s == "exit"
stop ; finish
else
## see if cmd is program or file, then check if its ruby that can be executed
begin
eval(inp.to_s)
rescue
"Unknown command."
end
end
end
def pannel
build_ui("pannel")
end
def add_task
ts = $ossy.instance_variable_get("@task_set") ; ts << @task_bar.get_text.to_s ; $ossy.instance_variable_set("@task_set",ts) ; @task_bar.set_text("")
end
def set_cfg
if @cfg_bar.get_text.to_s == ""
build_ui("pannel")
else
$ossy.instance_variable_set("@config",@cfg_bar.get_text.to_s) ; @cfg_bar.set_text("") ; build_ui("pannel")
end
end
def append_consol(inp)
ll = [] ; inp.to_s.split("\n").each { |l| ll << l.to_s }
nl = []
ll.each do |l|
if l.to_s.length > @wrap_length.to_i
c = 0 ; line = ""
l.split('').each do |ch|
if c < @wrap_length.to_i - 1
line << ch.to_s ; c += 1
else
nl << line.to_s + ch.to_s ; line = "" ; c = 0
end
end
else
nl << l.to_s
end
end
nl.each { |l| $display_lines << l.to_s }
update_display
end
def update_display
@display.set_text($display_lines[@sc1..@sc2].join("\n").to_s)
end
def set_display_lines(lines) # INTEGER
$scl = lines.to_i - lines.to_i - lines.to_i ; @sc1 = $scl ; update_display
end
def set_display_width(length)
@wrap_length = length.to_i
end
def scroll_top
l = $display_lines.length.to_i ; l = l - l - l
@sc1 = l.to_i ; @sc2 = @sc1 - $scl - 2
update_display
end
def scroll_up
cpv = $display_lines.length - $display_lines.length - $display_lines.length
if @sc1.to_i > cpv.to_i
@sc1 -= 1 ; @sc2 -= 1
update_display
else
## cant scroll any more
end
end
def scroll_bottom
@sc1 = $scl ; @sc2 = -1 ; update_display
end
def scroll_down
if @sc2.to_i < -1
@sc1 += 1 ; @sc2 += 1
update_display
else
## cant scroll any more
end
end
def forward_input
cmd = @input_bar.get_text.to_s ; @input_bar.set_text("")
if cmd[0].to_s == "*"
cmd = cmd[1..-1].to_s ; @input_bar.set_text("*" + cmd.to_s)
end
if cmd[0..2].to_s == "sh<"
append_consol("<" + @fid.to_s + ": " + cmd.to_s)
begin
res = eval(cmd[3..-1].to_s).to_s
rescue
res = "Execution Failed (Shell: " + self.to_s + ")"
end
elsif cmd.to_s == "[u"
res = nil ; scroll_up
elsif cmd.to_s == "[d"
res = nil ; scroll_down
elsif cmd.to_s == "[h"
res = nil ; scroll_top
elsif cmd.to_s == "[e"
res = nil ; scroll_bottom
else ## not shell cmd so must be for ossy or irb
append_consol("<" + @fid.to_s + ": " + cmd.to_s)
res = general_request(cmd.to_s)
end
if res == nil
## append nothing, no message to return
else
append_consol(":" + @fid.to_s + "> " + res.to_s )
end
@fid += 1
@fid_label.set_text(@fid.to_s)
end
end
$vdr = VDrive.new()
#$irb.start_ruboto_activity :class_name => "Ossy_Activity"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment