Class: Studium::LvaNummer

Inherits:
Base
  • Object
show all
Defined in:
lib/studium/utility_scripts/lva_nummer.rb

Overview

Studium::LvaNummer

Constant Summary collapse

NAMESPACE =
#

NAMESPACE

#
inspect
MAIN_POSITION_IN_THE_ARRAY =
#

MAIN_POSITION_IN_THE_ARRAY

This constant should refer to the position of the .csv file that keeps our lecture ID.

#
7

Constants inherited from Base

Base::ARRAY_HTML_COLOURS_IN_USE, Base::CHECK_FOR_INCORRECT_ENTRIES_IN_THE_QUESTION_ANSWER_FILE_THAT_SHOULD_BE_COMMENTS, Base::COLOURS, Base::COMMON_YEAR_DAYS_IN_MONTH, Base::GRAY, Base::LEFT_PADDING, Base::N_CHARACTERS_PER_LINE, Base::UNICODE_RIGHT_ARROW

Constants included from Colours

Colours::COLOURS

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#add_proper_year_entry_to_this_line, #already_solved_this_lva_number?, #append_what_into, #available_topics_as_short_name, #average_grade, #bachelor_master_or_diploma, #base_dir?, #be_silent, #be_verbose?, #beautiful_url, #change_directory, #cheering_person, #cliner, #colour_for_ects_points, #colourize_percentage, #commandline_arguments?, #commandline_arguments_joined, #commandline_arguments_with_hyphens?, #convert_this_array_of_lectures_into_the_full_dataset, #copy, #cpruby, #create_directory, #create_file, #csv_file?, #current_month?, #datum?, #day_month_year, #debug?, #delete_file, #directory_of_the_exam_topics?, #directory_to_my_exam_topics, #directory_to_the_exam_topics?, #disable_colours, #disable_colours_globally, #do_not_be_verbose, #do_not_use_opn, #do_these_curricula_include_both_a_bachelor_and_a_master_curriculum?, #does_this_line_include_a_german_weekday?, #does_this_line_include_a_html_colour?, #e, #ecomment, #editor?, #ee, #enable_colours, #enable_colours_globally, #enable_debug, #encoding?, #english_to_german_weekday, #ensure_main_encoding, #ensure_that_the_log_directory_exists, #eparse, #erev, #esystem, #etomato, #exit_program, #expand_topic, #extract_dd_mm_yyyy, #file_lecture_information?, #file_read_with_proper_encoding, #filter_away_invalid_questions, #find_corresponding_exam_topic, #first_argument?, #first_non_hyphen_argument?, #from_curriculum_id_to_university, #german_date_notation, #german_weekday?, #german_weekdays?, #has_a_bachelor_curriculum?, #has_a_master_curriculum?, #has_this_exam_topic_been_solved?, #has_year_entry?, #hh_mm_ss_dd_mm_yyyy, #horizontal_bar?, #html_log_directory?, #is_a_master_lecture?, #is_a_weekday?, #is_connected_to_the_www?, #is_on_roebe?, #is_prüfungsimmanent?, #is_this_a_valid_html_colour?, #is_this_day_today?, #konsole_support?, #last_file?, #lectures_from_indi1, #lectures_from_indi2, #lectures_from_indi3, #lectures_from_indi4, #lectures_from_the_bachelor_genetik_curriculum, #lectures_from_the_individual_vektor_curricula, #load_yaml_dataset, #move_file, #n_days_difference_from_today_to_this_day, #no_ext_name, #no_file_at, #number_to_month, #obtain, #obtain_lectures_from_this_curriculum, #open_in_editor, #opnn, #path_to_exam_topic?, #read_dataset_from_a_curriculum_file, #read_in_exam_dataset_from_this_file, #read_this_file_with_default_encoding, #readlines_from_this_input_file, #register_sigint, #remove_empty_strings, #remove_escape_sequences, #remove_internal_comments, #remove_leading_comments, #remove_leading_weekday_names_from, #remove_newlines, #remove_tags_from_this_input, #rename_kde_konsole_tab, #rename_konsole_tab?, #replace_all_html_colours_in_this, #replace_bold_token_with_default_colour, #replace_italic_token_with_default_colour, #replace_italic_token_with_this_colour, #replace_regular_numbers_with_unicode_numbers, #replace_underline_token_with_default_colour, #replace_underline_token_with_this_colour, #return_all_bachelor_lectures, #return_all_exams_on_this_day, #return_all_numbers_from, #return_all_registered_curricula, #return_all_steop_lectures, #return_current_year, #return_dataset_for_this_topic, #return_dataset_from_this_curriculum_file, #return_dd_mm_yy_and_time_from, #return_directory_for, #return_ects_points_from_these_lectures, #return_either_grey_or_the_custom_colour_for_answers, #return_either_grey_or_the_custom_colour_for_questions, #return_file_for_this_curriculum, #return_german_name_for_this_english_month, #return_german_weekday_of?, #return_hh_mm_ss_from, #return_hour_from_this, #return_lectures_from_bachelor_vektor_curriculum, #return_lectures_with_at_the_least_one_upcoming_exam, #return_local_path_of_this_pwdstud, #return_lva_number_of_this_lecture, #return_month_fitting_to_this_input, #return_n_days_until_monday, #return_n_days_until_sunday, #return_n_exam_questions_in, #return_n_questions_were_answered_for_this_topic, #return_name_of_the_weekday, #return_name_of_this_curriculum, #return_name_of_this_curriculum_based_on_its_id_number, #return_opnn, #return_passed_courses, #return_pwd, #return_random_curriculum, #return_sanitized_dataset_from_the_file_lecture_information, #return_solved_lectures, #return_steop_lectures, #return_the_remote_homepage_url, #return_this_line_has_n_characters_as_html_colour_tags, #return_title_from_this_curriculum_file, #rev, #right_arrow?, #rinstall2, #roebe_exam_directory?, #ruby_src_dir_at_home?, #runmode?, #sanitize_this_topic, #sanitize_url, #select_lectures_with_this_key, #set_be_quiet, #set_be_verbose, #set_commandline_arguments, #set_commandline_mode, #set_runmode, #set_this_cd_alias_to, #set_www_mode, #sfancy, #shall_we_make_use_of_unicode_symbols?, #show_todays_date, #simp, #sort_these_lecture_names_by_time, #spacer, #swarn, #this_month_has_n_days?, #total_ects_points_passed, #translate_dd_mm_yyyy_to_weekday, #try_to_require_the_rcfiles, #turn_this_array_into_the_full_dataset, #unicode_blocks_line, #use_colours?, #use_opn?, #vertical_bar?, #web_liner, #weekday?, #weekdays?, #word_wrap, #write_what_into, #www_connection_is_unavailable, #www_mode?, #yaml_dir?

Methods included from Colours

#bold_blue, #bold_red, #bold_yellow, #cadetblue, #chartreuse, crimson, #darkgray, darkgreen, darkseagreen, deepskyblue, #deepskyblue, default_colour, #dimgray, disable_colours, #dodgerblue, e, #efancy, enable_colours, #ewarn, #forestgreen, gold, #grey, lightblue, lightgreen, #lightslategray, #lightsteelblue, #mediumorchid, #mediumseagreen, #oldlace, #olivedrab, orange, orangered, #orchid, palevioletred, #papayawhip, rev, royalblue, sdir, #sdir, sfancy, #sfancy, sfile, #sfile, simp, #simp, slateblue, steelblue, #steelblue, #swarn, swarn, #teal, turquoise, use_colours?, use_this_colour_for_exam_answers, use_this_colour_for_exam_questions, #yellow

Constructor Details

#initialize(commandline_arguments = ARGV, run_already = true) ⇒ LvaNummer

#

initialize

Note that the first argument may also be a Hash.

#


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/studium/utility_scripts/lva_nummer.rb', line 43

def initialize(
    commandline_arguments = ARGV,
    run_already           = true
  )
  reset
  set_find_this_lecture(commandline_arguments)
  if block_given?
    yielded = yield
    case yielded
    when :do_not_modify_the_xorg_buffer
      @do_modify_the_xorg_buffer = false
    end
  end
  case run_already
  when :do_not_run_yet
    run_already = false
  end
  run if run_already
end

Class Method Details

.[](i) ⇒ Object

#

Studium::LvaNummer[]

#


436
437
438
# File 'lib/studium/utility_scripts/lva_nummer.rb', line 436

def self.[](i)
  new(i)
end

Instance Method Details

#do_not_report_resultsObject Also known as: do_not_report_the_results

#

do_not_report_results

#


149
150
151
# File 'lib/studium/utility_scripts/lva_nummer.rb', line 149

def do_not_report_results
  @do_report_results = false
end

#find_this_lecture?Boolean Also known as: input?

#

find_this_lecture?

#

Returns:

  • (Boolean)


127
128
129
# File 'lib/studium/utility_scripts/lva_nummer.rb', line 127

def find_this_lecture?
  @find_this_lecture
end

#get_rid_of_the_datasetObject

#

get_rid_of_the_dataset

Get rid of @dataset again.

#


183
184
185
# File 'lib/studium/utility_scripts/lva_nummer.rb', line 183

def get_rid_of_the_dataset
  remove_instance_variable(:@dataset)
end

#lva_number?Boolean Also known as: lva_number, id?

#

lva_number?

#

Returns:

  • (Boolean)


141
142
143
# File 'lib/studium/utility_scripts/lva_nummer.rb', line 141

def lva_number?
  @lva_number
end

#report_results(use_these_results = @results) ⇒ Object

#

report_results

The input to this method will have results IF we already passed that particular exam.

IF we have not yet passed that exam, the input may look like this:

["931103|https://online.boku.ac.at/BOKUonline/wbLv.wbShowLVDetail?pStpSpNr=277659"]

Unfortunately, some exams may have already been passed, but may now have another id, so ideally we also need a way to determine the LVA-ID number in these cases.

An alternative is to register the passed exams in a file called lecture_information.yml.

#


362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/studium/utility_scripts/lva_nummer.rb', line 362

def report_results(
    use_these_results = @results
  )
  if use_these_results.empty?
    # opnn; e 'This lecture ('+sfancy(input?.to_s)+') is not registered/known.'
  else
    use_these_results.each {|line| # <- Assume an Array here.
      # =================================================================== #
      # For now we will always assume that the dataset given to this
      # method will contain a '|' token. Do not forget that.
      # =================================================================== #
      splitted = line.split('|').map(&:strip)
      lva_number = splitted[MAIN_POSITION_IN_THE_ARRAY] # Is at position number 7.
      e lva_number if @do_report_results # This line is the "reporting" part. :)
      try_to_modify_the_xorg_buffer(lva_number) if @do_modify_the_xorg_buffer
    }
  end
end

#resetObject

#

reset

#


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
91
92
93
94
# File 'lib/studium/utility_scripts/lva_nummer.rb', line 66

def reset
  super()
  # ======================================================================= #
  # === @namespace
  # ======================================================================= #
  @namespace = NAMESPACE
  # ======================================================================= #
  # === @dataset
  #
  # Obtain the dataset holding all the passed exams per month.
  # ======================================================================= #
  @dataset = File.readlines(
    FILE_PASSED_EXAMS_PER_MONTH,
    encoding: main_encoding?
  )
  # ======================================================================= #
  # === @do_modify_the_xorg_buffer
  # ======================================================================= #
  @do_modify_the_xorg_buffer = true
  # ======================================================================= #
  # === @do_report_results
  # ======================================================================= #
  @do_report_results = true
  # ======================================================================= #
  # === @lva_number
  # ======================================================================= #
  @lva_number = 0 # Store the LVA Number here.
  sanitize_dataset
end

#return_all_numbers_as_arrayObject

#

return_all_numbers_as_array

#


172
173
174
175
176
# File 'lib/studium/utility_scripts/lva_nummer.rb', line 172

def return_all_numbers_as_array
  @results.map {|entry|
    entry.split('|').first.strip
  }
end

#runObject

#

run

#


427
428
429
430
431
# File 'lib/studium/utility_scripts/lva_nummer.rb', line 427

def run
  try_to_find_matches
  get_rid_of_the_dataset
  report_results if @do_report_results
end

#sanitize_datasetObject

#

sanitize_dataset

#


384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# File 'lib/studium/utility_scripts/lva_nummer.rb', line 384

def sanitize_dataset
  @dataset.reject! {|entry| entry.strip.empty? }
  @dataset.reject! {|entry| entry.strip.start_with? '#'}
  # ======================================================================= #
  # Some exams, in particular at the University of Vienna, are equivalent
  # to other exams. For example, "Physik für Biologinnen und Biologen
  # und LA-BU" has been imputed for "B-BIO 4 Biologische Physik (BIO 3)".
  # Or the lecture "300006 VO Allgemeine und Molekulare Genetik Teil II"
  # which is equivalent to "300132 VO Allgemeine und Molekulare Genetik
  # II".
  # This explains why some entries ought to be "corrected".
  # The code that follows will do this.
  # ======================================================================= #
  split_token_to_use = '|'
  @dataset.map! {|line|
    line.chomp!
    splitted = line.split(split_token_to_use).map(&:strip)
    name_of_the_lecture = splitted.last
    id_of_the_lecture   = splitted[7]
    # ===================================================================== #
    # The following method is defined in:
    #   studium/toplevel_methods/has_this_lecture_been_already_passed_but_the_id_was_changed_lateron.rb
    # ===================================================================== #
    if ::Studium.has_this_lecture_been_already_passed_but_the_id_was_changed_lateron?(
        id_of_the_lecture, name_of_the_lecture
      )
      # =================================================================== #
      # In this case, since the ID has already been changed,
      # re-write the dataset.
      # =================================================================== #
      id_of_the_lecture   = HASH_PASSED_LECTURES_THAT_WERE_LATERON_CHANGED_LV_ID_ALIASES[id_of_the_lecture]
      name_of_the_lecture = HASH_PASSED_LECTURES_THAT_WERE_LATERON_CHANGED_ALIAS_NAME[id_of_the_lecture]
      splitted[-1] = name_of_the_lecture
      splitted[7]  = id_of_the_lecture
      line = splitted.join(split_token_to_use)
    end
    line
  }
end

#set_find_this_lecture(i) ⇒ Object

#

set_find_this_lecture

#


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/studium/utility_scripts/lva_nummer.rb', line 99

def set_find_this_lecture(i)
  i = i.join(' ').strip if i.is_a? Array
  # ======================================================================= #
  # Need to protect against frozen strings.
  # ======================================================================= #
  i = i.dup if i.frozen?
  unless i.encoding.to_s.include? ::Studium.main_encoding?
    i = i.force_encoding(::Studium.main_encoding?)
  end    
  i.strip!
  # ======================================================================= #
  # The following constant is defined in studium/constants/constants.rb.
  # Somehow this was changed .... hmmmm.
  # ======================================================================= #
  # if HASH_PASSED_LECTURES_THAT_WERE_LATERON_CHANGED.values.include?(i)
  #   # ===================================================================== #
  #   # "Re-wire" to the real name of the lecture.
  #   # ===================================================================== #
  #   lva_id = HASH_PASSED_LECTURES_THAT_WERE_LATERON_CHANGED.invert[i] # Now we have the LV-id.
  #   lva_id = HASH_PASSED_LECTURES_THAT_WERE_LATERON_CHANGED_LV_ID_ALIASES[lva_id]
  #   i = HASH_PASSED_LECTURES_THAT_WERE_LATERON_CHANGED_ALIAS_NAME[lva_id]
  # end
  @find_this_lecture = i
end

#set_lva_number(i) ⇒ Object

#

set_lva_number

#


134
135
136
# File 'lib/studium/utility_scripts/lva_nummer.rb', line 134

def set_lva_number(i)
  @lva_number = i.to_s.strip # Is always a String.
end

#set_results(i) ⇒ Object

#

set_results

#


156
157
158
# File 'lib/studium/utility_scripts/lva_nummer.rb', line 156

def set_results(i)
  @results = i
end

#try_to_find_matchesObject

#

try_to_find_matches

This method will try to find any matches.

#


192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/studium/utility_scripts/lva_nummer.rb', line 192

def try_to_find_matches
  _ = @dataset #.join(N) # Need to re-add the "\n" here.
  # ======================================================================= #
  # Next, we need to use a proper regex to find the correct entry.
  # This is a critical step - if our regex finds invalid input, we would
  # propagate this faulty input, hence why the regex needs to be very
  # accurate.
  # ======================================================================= #
  use_this_input = input?
  if use_this_input
    unless use_this_input.encoding.to_s.include? ::Studium.main_encoding?
      use_this_input = ensure_main_encoding(use_this_input)
    end
  end
  # ======================================================================= #
  # For the following regex, see here:
  #   http://rubular.com/r/2xNaE5FXPe
  # use_this_regex = /#{Regexp.quote(use_this_input)}\s*$/
  # ======================================================================= #
  use_this_regex = /(\| #{Regexp.quote(use_this_input)}$)/
  scanned = _.select {|line|
    line =~ use_this_regex
  }
  # ======================================================================= #
  # Note that the variable _ may look like this:
  #   "20.09.2013 | 1.0 |  2.0 | 4 | UniWien |  B/M | VO |   300150   | 2013S | Chromosomenbiologie und Cytogenetik"
  #   "25.11.1997|2.0|xxx|4|UniWien|D|VO|300053|1997W|Physik für Biologinnen und Biologen und LA-BU"
  # ======================================================================= #
  # ======================================================================= #
  # We need to keep in mind that the lecture is not registered in the
  # "already passed exams" part yet. In this case, the variable scanned
  # will be empty, which is a case we will handle next.
  # ======================================================================= #
  if scanned.empty?
    # ===================================================================== #
    # Ok. How do we proceed from here? Well, we have a few options.
    #
    # A simple one is to make use of the file called 'lecture_information.yml'
    #
    # This should ideally have our lecture registered.
    #
    # In the event that it ahs not, we will try to obtain the information
    # from a remote URL. For this we will use the project BeautifulUrl -
    # but keep in mind that BeautifulUrl must have registered that page
    # prior before it could be found.
    #
    # Also, keep in mind that we can not get rid of '(' and ')', because
    # some lectures may mandate this as part of their name.
    # ===================================================================== #
    keys_only = YAML.load_file(FILE_LECTURE_INFORMATION).keys
    hash = {}
    keys_only.each {|line|
      line = ensure_main_encoding(line)
      index = line.index(' ')
      key = line[0,index]
      value = line[index+1 .. -1]
      hash[key] = value
    }
    _ = input?.delete(' ') # .delete('()') # Easier to get the input without any ' ' in it and without ( or ) either.
    url = Studium.sanitize_url(_) # So that we do not depend on Studium::Base.
    if hash.has_value?(input?)
      this_lva_number = hash.key(input?)
      set_lva_number(this_lva_number)
      if @do_report_results
        e lva_number?
        try_to_modify_the_xorg_buffer(lva_number?) if @do_modify_the_xorg_buffer
      end
    elsif url.is_a? Array
      target_url = url.first
      # =================================================================== #
      # Assign a default lecture_id value next.
      # =================================================================== #
      lecture_id = '000000' # Use a default, just in case.
      # =================================================================== #
      # Intercept nil values first.
      # =================================================================== #
      if target_url.nil?
        e 'Found a nil value for target_url, on '+sfancy(_)+'.'
        e 'Please fix this issue.'
      elsif target_url.include? 'ufind.'
        use_this_regex = # See: http://rubular.com/r/LsdAmVnOT1
          /ufind.univie.ac.at\/de\/course.html\?lv=(\d{4,6})&semester=20\d\d/
        target_url =~ use_this_regex
        lecture_id = $1.to_s.dup
      else
        # ================================================================= #
        # Get the content of the remote page. we assume this to be a
        # BOKU lecture if we enter here.
        # ================================================================= #
        # ================================================================= #
        # We must handle possible errors, as the URL may be non-existent,
        # for instance.
        # ================================================================= #
        begin
          remote_page_content = open(target_url)
          remote_page_content = remote_page_content.read.delete(N) # Easier to work without newlines.
          # =============================================================== #
          # Next, find the proper match via a regex. For BOKU lectures
          # this is the "Nummer" entry.
          # =============================================================== #
          use_this_regex = # See: http://rubular.com/r/07S71aEYEt
            /class="Mask ">Nummer<\/label><\/td><td colspan="0" class=" MaskRenderer top"><table class=" MaskSpacing"><tr><td class=" MaskRenderer"><span class="bold ">(\d{1,6})<\/span>/
          # =============================================================== #
          # We need to grab the lecture_id next.
          # =============================================================== #
          if remote_page_content.include? 'Nummer</label>'
            remote_page_content =~ use_this_regex
            lecture_id = $1.to_s.dup
          end
        rescue OpenURI::HTTPError => error
          opnn; e 'An error happened which will be shown next ('+
                   swarn('OpenURI::HTTPError')+')'
          pp error
          opnn; e 'This means that a remote website is probably unavailable. '\
                  '(target_url was: '+simp(target_url)+')'
          opnn; e 'If this is for a lecture at the BOKU, then this usually '
          opnn; e 'means that some maintenance work is currently ongoing.'
          opnn; e 'We will thus exit for now as we can not access '\
                  'the remote information anyway.'
          exit
        rescue SocketError => error # This here requires: require 'socket'
          opnn; e 'An error happened:'
          pp error
          lecture_id = '0'
        rescue Errno::ENOENT
          lecture_id = '0'
        rescue Net::OpenTimeout
          opnn; e 'A Net::OpenTimeout was encountered. Your internet '\
                  'connection may not be working or the remote site '\
                  'may have a problem.'
          lecture_id = 'Net::OpenTimeout'
        rescue Interrupt
          opnn; e 'Exiting upon user request.'
          exit
        rescue Exception => error
          opnn; e 'Another error for the URL '+sfancy(target_url)+': '
          pp error
        end
      end
      set_lva_number(lecture_id) # Also keep a reference to the LVA number at hand.
      report_results([lecture_id+'|'+target_url]) if @do_report_results
    end
  else # else still register the LVA number.
    if lva_number?.to_s.empty? or
      (lva_number?.to_s == '0')
      first = scanned.first
      _ = first.split('|')[MAIN_POSITION_IN_THE_ARRAY] # Obtain the LVA number here, position 8.
      set_lva_number(_)
    end
  end
  set_results(scanned)
end

#try_to_modify_the_xorg_buffer(i) ⇒ Object

#

try_to_modify_the_xorg_buffer

#


163
164
165
166
167
# File 'lib/studium/utility_scripts/lva_nummer.rb', line 163

def try_to_modify_the_xorg_buffer(i)
  if Object.const_defined? :XorgBuffer
    XorgBuffer[i]
  end
end