Module: Synapse

Defined in:
lib/rbbt/sources/synapse.rb

Constant Summary collapse

PYTHON_LOGIN_TEMPLATE =
<<-EOF
import synapseclient
syn = synapseclient.login()

EOF
PYTHON_FILE_TEMPLATE =
<<-EOF
f = synapseclient.File('[FILE]', parentId='[PARENTID]', name='[NAME]', contentType='[CONTENT-TYPE]')
[ANNOTATIONS]

EOF
PYTHON_SEND_TEMPLATE =
<<-EOF
f = syn.store(f, used= [USED], executed = [EXECUTED])
print("syn: " + f.id)

EOF
PYTHON_DIR_TEMPLATE =
<<-EOF
f = synapseclient.Folder('[NAME]', parentId='[PARENTID]')
f = syn.store(f)
print("syn: " + f.id)

EOF

Class Method Summary collapse

Class Method Details

.create_directories(dirs, syn) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/rbbt/sources/synapse.rb', line 133

def self.create_directories(dirs, syn)
  template =  
  order = []
  dirs.each do |dir|
    order << dir
    template <<  python_create_dir(dir, syn)
  end
  syns = Misc.insist do
    TmpFile.with_file(template) do |ftemplate|
      Log.warn "Creating directories: #{Misc.fingerprint dirs}"
      res = `python '#{ ftemplate }'`
      res.scan(/syn: syn\d+/).collect{|m| m.match(/(syn\d+)/)[0]}
    end
  end
  Hash[*order.zip(syns).flatten]
end

.create_directory(dir, syn) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/rbbt/sources/synapse.rb', line 122

def self.create_directory(dir, syn)
  template =  + python_create_dir(dir, syn)
  syn = Misc.insist do
    Log.warn "Creating directory: #{dir}"
    TmpFile.with_file(template) do |ftemplate|
      `python '#{ ftemplate }'`.match(/syn: (syn\d+)/)[1]
    end
  end
  syn
end

.directory_structure(directory) ⇒ Object



150
151
152
153
154
155
156
157
158
# File 'lib/rbbt/sources/synapse.rb', line 150

def self.directory_structure(directory)
  structure = {}
  Dir.glob(File.join(directory, '*')).each do |path|
    if File.directory? path
      structure[path] = directory_structure(path)
    end
  end
  structure
end

.python_create_dir(name, parentid) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/rbbt/sources/synapse.rb', line 32

def self.python_create_dir(name, parentid)
  name = File.basename(name)
  template = PYTHON_DIR_TEMPLATE.dup

  template.sub!('[PARENTID]', parentid)
  template.sub!('[NAME]', name)

  template
end

.python_file_template(file, parentid, name = nil, content_type = nil, annotations = {}) ⇒ Object



62
63
64
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
# File 'lib/rbbt/sources/synapse.rb', line 62

def self.python_file_template(file, parentid, name = nil, content_type = nil, annotations = {})
  template = PYTHON_FILE_TEMPLATE.dup

  annotations = {} if annotations.nil?

  name = annotations.delete :name if name.nil?
  name = File.basename(file) if name.nil?

  content_type = annotations.delete :content_type if content_type.nil?
  content_type = begin
                   MimeMagic.by_path(file) 
                 rescue
                   'text/plain'
                 end if content_type.nil?

  template.sub!('[FILE]', file)
  template.sub!('[PARENTID]', parentid)
  template.sub!('[NAME]', name)
  template.sub!('[CONTENT-TYPE]', content_type)

  if annotations
    annotation_str = annotations.collect{|k,v| "f.#{k} = '#{v}'"} * "\n"
    template.sub!('[ANNOTATIONS]', annotation_str)
  else
    template.sub!('[ANNOTATIONS]', '')
  end

  template
end

.python_loginObject



28
29
30
# File 'lib/rbbt/sources/synapse.rb', line 28

def self.
  PYTHON_LOGIN_TEMPLATE.dup
end

.python_upload_template(used = nil, executed = nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rbbt/sources/synapse.rb', line 42

def self.python_upload_template(used = nil, executed = nil)
  template = PYTHON_SEND_TEMPLATE.dup
  if used
    used_str = "#{used.inspect}"
  else
    used_str = "[]"
  end

  if executed
    executed_str = "'#{executed}'"
  else
    executed_str = "''"
  end

  template.sub!('[USED]', used_str)
  template.sub!('[EXECUTED]', executed_str)

  template
end

.upload_dir(directory, syn, file_annotations = {}) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/rbbt/sources/synapse.rb', line 160

def self.upload_dir(directory, syn, file_annotations = {})
  Log.warn "Uploading #{ Log.color :blue, directory }"
  files = Dir.glob(File.join(directory, '*')).reject{|f| File.directory? f}
  file_info = {}
  files.each{|file| file_info[file] = file_annotations[file] || {} } if Hash === file_annotations
  files.each{|file| file_info[file] = file_annotations.call(file) || {} } if Proc === file_annotations
  files.each{|file| file_info[file] = {} } if file_annotations.nil?

  Log.warn "Uploading #{ Log.color :blue, directory } files: #{Misc.fingerprint files}"
  upload_files(file_info, syn)

  structure = directory_structure(directory)
  directories = structure.keys
  diretories_syns = create_directories(directories, syn)
  Log.warn "Traversing #{ directory } subdirectories"
  diretories_syns.each do |subdirectory,subsyn|
    upload_dir(subdirectory, subsyn, file_annotations)
  end
end

.upload_file(file, syn, name = nil, content_type = nil, annotations = {}) ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'lib/rbbt/sources/synapse.rb', line 92

def self.upload_file(file, syn, name=nil, content_type=nil, annotations = {})
  template =  + python_file_template(file, syn, name, content_type, annotations) + python_upload_template
  syn = Misc.insist do
    Log.warn "Uploading file: #{file}"
    TmpFile.with_file(template) do |ftemplate|
      `python '#{ ftemplate }'`.match(/syn: (syn\d+)/)[1]
    end
  end
  syn
end

.upload_files(files, syn) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/rbbt/sources/synapse.rb', line 103

def self.upload_files(files, syn)
  template =  
  order = []
  files.each do |file,info|
    order << file
    name, content_type, annotations = info.values_at :name, :content_type, :annotations
    template << python_file_template(file, syn, name, content_type, annotations) + python_upload_template
  end

  syns = Misc.insist do
    Log.warn "Uploading files: #{Misc.fingerprint files}"
    TmpFile.with_file(template) do |ftemplate|
      res = `python '#{ ftemplate }'`
      res.scan(/syn: syn\d+/).collect{|m| m.match(/(syn\d+)/)[0]}
    end
  end
  Hash[*order.zip(syns).flatten]
end