How to use slash_list method in Slash

Best Python code snippet using slash

gui.py

Source:gui.py Github

copy

Full Screen

1# coding: utf-82''' Zhudi provides a Chinese - language dictionnary based on the3 C[E|F]DICT project Copyright - 2011 - Ma Jiehong4 Zhudi is free software: you can redistribute it and/or modify it5 under the terms of the GNU General Public License as published by6 the Free Software Foundation, either version 3 of the License, or7 (at your option) any later version.8 Zhudi is distributed in the hope that it will be useful, but WITHOUT9 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY10 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public11 License for more details.12 You should have received a copy of the GNU General Public License13 If not, see <http://www.gnu.org/licenses/>.14'''15from gi import require_version16require_version('Gtk', '3.0')17from gi.repository import Gtk, Pango, Gdk18import zhudi19CANGJIE5_OBJ = zhudi.chinese_table.Cangjie5Table()20ARRAY30_OBJ = zhudi.chinese_table.Array30Table()21WUBI86_OBJ = zhudi.chinese_table.Wubi86Table()22class DictionaryWidgetMain(object):23 """ Dictionary tab gui. """24 def __init__(self, data_object):25 self.data_object = data_object26 self.language = ""27 self.results_list = []28 self.lock = False29 self.search_field = None30 self.translation_box = None31 def build(self):32 """ Mandatory build() function. """33 # Search label34 search_label = Gtk.Label()35 # Search field36 search_field = Gtk.Entry()37 search_field.set_visible(True)38 search_field.connect("activate",39 lambda x: self.search_asked(search_field))40 search_field.set_placeholder_text("Looking for something?")41 self.search_field = search_field42 # Go, search! button43 go_button = Gtk.Button("Search")44 go_button.connect("clicked",45 lambda x: self.search_asked(search_field))46 # Search + button box47 sb_box = Gtk.Grid()48 sb_box.attach(search_field, 0, 0, 5, 1)49 sb_box.attach_next_to(go_button,50 search_field,51 Gtk.PositionType.RIGHT, 1, 1)52 sb_box.set_column_homogeneous(True)53 # Search label zone54 frame_search = Gtk.Frame()55 frame_search.set_label_widget(search_label)56 frame_search.add(sb_box)57 # Results part in a list58 self.results_list = Gtk.ListStore(str)59 results_tree = Gtk.TreeView(self.results_list)60 renderer = Gtk.CellRendererText()61 results_tree.tvcolumn = Gtk.TreeViewColumn("Results", renderer, text=0)62 results_tree.append_column(results_tree.tvcolumn)63 self.results_list.cell = Gtk.CellRendererText()64 results_tree.tvcolumn.pack_start(self.results_list.cell, True)65 results_tree.set_enable_search(False)66 results_tree.tvcolumn.set_sort_column_id(-1)67 results_tree.set_reorderable(False)68 select = results_tree.get_selection()69 select.connect("changed", self.display_another_result)70 results_scroll = Gtk.ScrolledWindow()71 # No horizontal bar, automatic vertical bar72 results_scroll.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)73 results_scroll.add_with_viewport(results_tree)74 frame_results = Gtk.Frame()75 frame_results.add(results_scroll)76 # Translation Label77 translation_label = Gtk.Label()78 translation_label.set_text("<big>Translation</big>")79 translation_label.set_use_markup(True)80 # Translation view81 self.translation_box = Gtk.TextView(buffer=None)82 self.translation_box.set_editable(False)83 self.translation_box.set_cursor_visible(False)84 # No horizontal bar, vertical bar if needed85 self.translation_box.set_wrap_mode(Gtk.WrapMode.WORD)86 translation_scroll = Gtk.ScrolledWindow()87 translation_scroll.add_with_viewport(self.translation_box)88 frame_translation = Gtk.Frame()89 frame_translation.set_label_widget(translation_label)90 frame_translation.add(translation_scroll)91 # Mapping of the main window92 left_vertical_box = Gtk.Grid()93 left_vertical_box.add(frame_search)94 left_vertical_box.attach_next_to(frame_results,95 frame_search,96 Gtk.PositionType.BOTTOM, 1, 9)97 left_vertical_box.set_row_homogeneous(True)98 left_vertical_box.set_column_homogeneous(True)99 right_vertical_box = Gtk.Grid()100 right_vertical_box.add(frame_translation)101 right_vertical_box.set_column_homogeneous(True)102 right_vertical_box.set_row_homogeneous(True)103 horizontal_box = Gtk.Grid()104 horizontal_box.attach(left_vertical_box, 0, 0, 1, 1)105 horizontal_box.attach_next_to(right_vertical_box,106 left_vertical_box,107 Gtk.PositionType.RIGHT, 1, 1)108 horizontal_box.set_column_homogeneous(True)109 horizontal_box.set_row_homogeneous(True)110 return horizontal_box111 def search_asked(self, searchfield):112 """ Start search when users hit ENTER or the search button. """113 text = searchfield.get_text()114 if text == "":115 self.lock = True116 DICTIONARY_TOOLS_OBJECT.index = []117 self.results_list.clear()118 self.display_translation(0)119 else:120 self.lock = False121 self.language = self.determine_language(text)122 if self.language == "Latin":123 given_list = self.data_object.translation124 elif self.data_object.hanzi == "Traditional":125 given_list = self.data_object.traditional126 else:127 given_list = self.data_object.simplified128 DICTIONARY_TOOLS_OBJECT.search(given_list, text)129 self.update_results()130 self.display_translation(0)131 # end of search_asked132 @staticmethod133 def determine_language(input_text):134 """135 Determine the language of the input text, according to its content136 """137 if SEGMENTATION_TOOLS_OBJECT.is_not_chinese(input_text):138 return "Latin"139 else:140 return "Chinese"141 def display_translation(self, which):142 """ Handles the display of the translation for the selected element. """143 translation_buffer = self.translation_box.get_buffer()144 if len(DICTIONARY_TOOLS_OBJECT.index) == 0:145 translation_buffer.set_text("Nothing found.")146 if len(self.results_list) == 0:147 self.results_list.append(["Nothing found."])148 return149 else:150 index = DICTIONARY_TOOLS_OBJECT.index[which]151 if self.data_object.hanzi == "traditional":152 hanzi_dic = self.data_object.traditional153 else:154 hanzi_dic = self.data_object.simplified155 if self.data_object.romanisation == "zhuyin":156 romanisation_dic = self.data_object.zhuyin157 else:158 romanisation_dic = self.data_object.pinyin159 slash_list = []160 trans_index = self.data_object.translation[index]161 for l in range(len(trans_index)):162 if trans_index[l] == "/":163 slash_list.append(l)164 temp = 0165 trans = []166 for local_index in range(len(slash_list)):167 trans.append(str(local_index + 1) + ". " + trans_index[temp:slash_list[local_index]])168 temp = slash_list[local_index] + 1169 trans.append(str(len(slash_list) + 1) + ". " + trans_index[temp:len(trans_index)])170 string = ""171 for i in range(len(slash_list) + 1):172 string = string + trans[i] + "\n"173 # Add [] arround the pronounciation parts174 p_string = romanisation_dic[index].split()175 pronounciation_string = []176 for point in range(len(p_string)):177 if self.data_object.romanisation == "pinyin":178 pronounciation_string.append(DICTIONARY_TOOLS_OBJECT.unicode_pinyin(p_string[point]))179 pronounciation_string.append(" ")180 else:181 pronounciation_string.append("[")182 pronounciation_string.append(p_string[point])183 pronounciation_string.append("]")184 # Display the cangjie of the entry185 cangjie5_displayed = ""186 for hanzi in hanzi_dic[index]:187 if hanzi != "\n":188 key_code, displayed_code = CANGJIE5_OBJ.proceed(hanzi, self.data_object.cangjie5)189 cangjie5_displayed += "["190 cangjie5_displayed += displayed_code191 cangjie5_displayed += "]"192 # Display the array30 of the entry193 array30_displayed = ""194 for hanzi in hanzi_dic[index]:195 if hanzi != "\n":196 key_code, displayed_code = ARRAY30_OBJ.proceed(hanzi, self.data_object.array30)197 array30_displayed += "["198 array30_displayed += displayed_code199 array30_displayed += "]"200 # Display the array30 of the entry (here code = displayed)201 wubi86_code = ""202 for hanzi in hanzi_dic[index]:203 if hanzi != "\n":204 key_code, displayed_code = WUBI86_OBJ.proceed(hanzi, self.data_object.wubi86)205 wubi86_code += "["206 wubi86_code += key_code207 wubi86_code += "]"208 # Display in the Translation box209 translation_buffer.set_text("Chinese\n" + hanzi_dic[index] + "\n\n" +210 "Pronunciation\n" + ''.join(pronounciation_string) + "\n\n" +211 "Meaning\n" + string +212 "Input methods codes:\n" +213 "Array30 (行列30): \n" + array30_displayed + "\n\n" +214 "Cangjie5 (倉頡5): \n" + cangjie5_displayed + "\n\n" +215 "Wubi86 (五筆86): \n" + wubi86_code)216 bold = translation_buffer.create_tag(weight=Pango.Weight.BOLD)217 big = translation_buffer.create_tag(size=30 * Pango.SCALE)218 medium = translation_buffer.create_tag(size=15 * Pango.SCALE)219 blue = translation_buffer.create_tag(foreground="#268bd2")220 # "Chinese" in bold221 start_1 = translation_buffer.get_iter_at_line(0)222 end_1 = translation_buffer.get_iter_at_line(0)223 end_1.forward_to_line_end()224 translation_buffer.apply_tag(bold, start_1, end_1)225 # Bigger Chinese226 start_c = translation_buffer.get_iter_at_line(1)227 end_c = translation_buffer.get_iter_at_line(1)228 end_c.forward_to_line_end()229 translation_buffer.apply_tag(big, start_c, end_c)230 # "Pronunciation" in bold231 start_2 = translation_buffer.get_iter_at_line(4)232 end_2 = translation_buffer.get_iter_at_line(4)233 end_2.forward_to_line_end()234 translation_buffer.apply_tag(bold, start_2, end_2)235 # "Pronunciation" in blue236 start_3 = translation_buffer.get_iter_at_line(5)237 end_3 = translation_buffer.get_iter_at_line(5)238 end_3.forward_to_line_end()239 translation_buffer.apply_tag(blue, start_3, end_3)240 translation_buffer.apply_tag(medium, start_3, end_3)241 # "Meaning" in bold242 start_3 = translation_buffer.get_iter_at_line(7)243 end_3 = translation_buffer.get_iter_at_line(7)244 end_3.forward_to_line_end()245 translation_buffer.apply_tag(bold, start_3, end_3)246 guess = string.count("\n")247 # "Input methods codes" in bold248 start_4 = translation_buffer.get_iter_at_line(guess+7)249 end_4 = translation_buffer.get_iter_at_line(guess+7)250 end_4.forward_to_line_end()251 translation_buffer.apply_tag(bold, start_4, end_4)252 def update_results(self):253 """ Clear, and refill the result list. """254 self.results_list.clear()255 displayed_index = 1256 threashold = 40 # threshold for line wrap257 for k in DICTIONARY_TOOLS_OBJECT.index:258 if self.language == "latin":259 string = self.data_object.translation[k]260 elif self.data_object.hanzi == "traditional":261 string = self.data_object.traditional[k]262 else:263 string = self.data_object.simplified[k]264 if len(string) > threashold:265 string = str(displayed_index) + ". " + string[0:threashold] + "…"266 else:267 string = str(displayed_index) + ". " + string268 string = string[:-1] # no \n269 self.results_list.append([string])270 displayed_index += 1271 def display_another_result(self, selection):272 """ Display the newly selected result. """273 if not self.lock:274 model, treeiter = selection.get_selected()275 if treeiter is not None:276 row = model[treeiter][0]277 counter = 0278 if row is not None:279 while row[counter] != ".":280 counter += 1281 figure = int(row[0:counter])282 if figure > len(DICTIONARY_TOOLS_OBJECT.index):283 self.display_translation(0)284 else:285 self.display_translation(figure-1)286class SegmentationWidget(object):287 """ Class that defines the segmentation GUI layer.288 """289 def __init__(self, data_object):290 self.frame_label = None291 self.horizontal_separator = None292 self.go_button = None293 self.title_box = None294 self.text_field = None295 self.scrolledwindow = None296 self.results_list = []297 self.frame_results = None298 self.left_vertical_box = None299 self.right_vertical_box = None300 self.results_label = None301 self.results_field = None302 self.results_scroll = None303 self.results_frame = None304 self.horizontal_box = None305 self.data_object = data_object306 def build(self):307 """ Mandatory GUI build method. """308 # Frame label309 self.frame_label = Gtk.Label()310 self.frame_label.set_text("<big>Chinese text to process:</big>")311 self.frame_label.set_use_markup(True)312 # Horzontal separator313 self.horizontal_separator = Gtk.Separator()314 # Go! button315 self.go_button = Gtk.Button("Go!")316 self.go_button.connect("clicked",317 lambda x: self.go_segment(self.text_field.get_buffer()))318 # Frame title + Go! button319 self.title_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=3)320 self.title_box.pack_start(self.frame_label, False, False, 0)321 self.title_box.pack_start(self.horizontal_separator, True, False, 0)322 self.title_box.pack_start(self.go_button, True, True, 0)323 # Text field (to process)324 self.text_field = Gtk.TextView()325 self.text_field.set_editable(True)326 self.text_field.set_cursor_visible(True)327 self.text_field.set_wrap_mode(Gtk.WrapMode.CHAR)328 self.scrolledwindow = Gtk.ScrolledWindow()329 self.scrolledwindow.set_hexpand(True)330 self.scrolledwindow.set_vexpand(True)331 self.scrolledwindow.add(self.text_field)332 # Results part in a list333 self.results_list = Gtk.ListStore(str)334 results_tree = Gtk.TreeView(self.results_list)335 renderer = Gtk.CellRendererText()336 results_tree.tvcolumn = Gtk.TreeViewColumn("Results", renderer, text=0)337 results_tree.append_column(results_tree.tvcolumn)338 self.results_list.cell = Gtk.CellRendererText()339 results_tree.tvcolumn.pack_start(self.results_list.cell, True)340 results_tree.set_enable_search(False)341 results_tree.tvcolumn.set_sort_column_id(-1)342 results_tree.set_reorderable(False)343 select = results_tree.get_selection()344 select.connect("changed", self.word_selected)345 results_scroll = Gtk.ScrolledWindow()346 # No horizontal bar, automatic vertical bar347 results_scroll.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)348 results_scroll.add_with_viewport(results_tree)349 self.frame_results = Gtk.Frame()350 self.frame_results.add(results_scroll)351 # Mapping of window352 self.left_vertical_box = Gtk.Grid()353 self.left_vertical_box.add(self.title_box)354 self.left_vertical_box.attach_next_to(self.scrolledwindow,355 self.title_box,356 Gtk.PositionType.BOTTOM, 1, 2)357 self.left_vertical_box.attach_next_to(self.frame_results,358 self.scrolledwindow,359 Gtk.PositionType.BOTTOM, 1, 8)360 self.left_vertical_box.set_column_homogeneous(True)361 self.left_vertical_box.set_row_homogeneous(True)362 # Results frame363 self.results_label = Gtk.Label()364 self.results_label.set_text("<big>Translation</big>")365 self.results_label.set_use_markup(True)366 self.results_field = Gtk.TextView(buffer=None)367 self.results_field.set_editable(False)368 self.results_field.set_cursor_visible(False)369 # No horizontal bar, vertical bar if needed370 self.results_field.set_wrap_mode(Gtk.WrapMode.WORD)371 self.results_scroll = Gtk.ScrolledWindow()372 self.results_scroll.add_with_viewport(self.results_field)373 self.results_frame = Gtk.Frame()374 self.results_frame.set_label_widget(self.results_label)375 self.results_frame.add(self.results_scroll)376 self.right_vertical_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=2)377 self.right_vertical_box.pack_start(self.results_frame, True, True, 0)378 self.horizontal_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=2)379 self.horizontal_box.pack_start(self.left_vertical_box, False, True, 0)380 self.horizontal_box.pack_start(self.right_vertical_box, False, True, 0)381 self.horizontal_box.set_homogeneous(True)382 return self.horizontal_box383 def go_segment(self, text_buffer):384 """ Get the input text to segment, and display the words. """385 beginning = text_buffer.get_start_iter()386 end = text_buffer.get_end_iter()387 # grab hidden characters388 text = text_buffer.get_text(beginning, end, True)389 text = text.replace(" ", "")390 segmented_text = SEGMENTATION_TOOLS_OBJECT.sentence_segmentation(text)391 self.display_results(segmented_text, text_buffer)392 self.display_selectable_words(segmented_text)393 def display_selectable_words(self, segmented_text):394 """ Add in the results the segmented words. """395 widget = self.results_list396 widget.clear()397 for word in segmented_text:398 widget.append([word])399 @staticmethod400 def display_results(text, results_buffer):401 """ Display the segmentation result directly in the input area.402 This has a nice side effect: allowing you to copy the result.403 """404 text_to_display = ""405 for item in text:406 text_to_display += item407 text_to_display += " "408 results_buffer.set_text(text_to_display)409 def display_translation(self, index, bypass=False):410 """ Display the given index [of a word] in a nicely formatted output.411 If bypass is True, then the index variable is a string that has to412 be displayed as it.413 """414 translation_buffer = self.results_field.get_buffer()415 if bypass:416 translation_buffer.set_text(index)417 return418 if self.data_object.hanzi == "traditional":419 hanzi_dic = self.data_object.traditional420 else:421 hanzi_dic = self.data_object.simplified422 if self.data_object.romanisation == "zhuyin":423 romanisation_dic = self.data_object.zhuyin424 else:425 romanisation_dic = self.data_object.pinyin426 slash_list = []427 trans_index = self.data_object.translation[index]428 for line in range(len(trans_index)):429 if trans_index[line] == "/":430 slash_list.append(line)431 temp = 0432 trans = []433 for key in range(len(slash_list)):434 trans.append(str(key+1) + ". " + trans_index[temp:slash_list[key]])435 temp = slash_list[key]+1436 trans.append(str(len(slash_list) + 1) + ". " + trans_index[temp:len(trans_index)])437 string = ""438 for local_index in range(len(slash_list) + 1):439 string = string + trans[local_index] + "\n"440 # Add [] arround the pronounciation parts441 p_string = romanisation_dic[index].split()442 pronounciation_string = []443 for point in range(len(p_string)):444 if self.data_object.romanisation == "pinyin":445 pronounciation_string.append(DICTIONARY_TOOLS_OBJECT.unicode_pinyin(p_string[point]))446 pronounciation_string.append(" ")447 else:448 pronounciation_string.append("[")449 pronounciation_string.append(p_string[point])450 pronounciation_string.append("]")451 # Display the cangjie of the entry452 cangjie5_displayed = ""453 for hanzi in hanzi_dic[index]:454 if hanzi != "\n":455 key_code, displayed_code = CANGJIE5_OBJ.proceed(hanzi, self.data_object.cangjie5)456 cangjie5_displayed += "["457 cangjie5_displayed += displayed_code458 cangjie5_displayed += "]"459 # Display the array30 of the entry460 array30_displayed = ""461 for hanzi in hanzi_dic[index]:462 if hanzi != "\n":463 key_code, displayed_code = ARRAY30_OBJ.proceed(hanzi, self.data_object.array30)464 array30_displayed += "["465 array30_displayed += displayed_code466 array30_displayed += "]"467 # Display the array30 of the entry (here code = displayed)468 wubi86_code = ""469 for hanzi in hanzi_dic[index]:470 if hanzi != "\n":471 key_code, displayed_code = WUBI86_OBJ.proceed(hanzi, self.data_object.wubi86)472 wubi86_code += "["473 wubi86_code += key_code474 wubi86_code += "]"475 # Display in the Translation box476 translation_buffer.set_text("Chinese\n" + hanzi_dic[index] + "\n\n" +477 "Pronunciation\n" + ''.join(pronounciation_string) + "\n\n"478 "Meaning\n" + string +479 "Input methods codes:\n" +480 "Array30 (行列30): \n" + array30_displayed + "\n\n" +481 "Cangjie5 (倉頡5): \n" + cangjie5_displayed + "\n\n" +482 "Wubi86 (五筆86): \n" + wubi86_code)483 bold = translation_buffer.create_tag(weight=Pango.Weight.BOLD)484 big = translation_buffer.create_tag(size=30*Pango.SCALE)485 medium = translation_buffer.create_tag(size=15*Pango.SCALE)486 blue = translation_buffer.create_tag(foreground="#268bd2")487 # "Chinese" in bold488 start_1 = translation_buffer.get_iter_at_line(0)489 end_1 = translation_buffer.get_iter_at_line(0)490 end_1.forward_to_line_end()491 translation_buffer.apply_tag(bold, start_1, end_1)492 # Bigger Chinese493 start_c = translation_buffer.get_iter_at_line(1)494 end_c = translation_buffer.get_iter_at_line(1)495 end_c.forward_to_line_end()496 translation_buffer.apply_tag(big, start_c, end_c)497 # "Pronunciation" in bold498 start_2 = translation_buffer.get_iter_at_line(4)499 end_2 = translation_buffer.get_iter_at_line(4)500 end_2.forward_to_line_end()501 translation_buffer.apply_tag(bold, start_2, end_2)502 # "Pronunciation" in blue503 start_3 = translation_buffer.get_iter_at_line(5)504 end_3 = translation_buffer.get_iter_at_line(5)505 end_3.forward_to_line_end()506 translation_buffer.apply_tag(blue, start_3, end_3)507 translation_buffer.apply_tag(medium, start_3, end_3)508 # "Meaning" in bold509 start_3 = translation_buffer.get_iter_at_line(7)510 end_3 = translation_buffer.get_iter_at_line(7)511 end_3.forward_to_line_end()512 translation_buffer.apply_tag(bold, start_3, end_3)513 guess = string.count("\n")514 # "Input methods codes" in bold515 start_4 = translation_buffer.get_iter_at_line(guess+7)516 end_4 = translation_buffer.get_iter_at_line(guess+7)517 end_4.forward_to_line_end()518 translation_buffer.apply_tag(bold, start_4, end_4)519 def word_selected(self, selection):520 """ Display the selected word in the translation area.521 """522 model, treeiter = selection.get_selected()523 if treeiter is not None:524 word = model[treeiter][0]525 if word is not None:526 index = SEGMENTATION_TOOLS_OBJECT.search_unique(word, self.data_object)527 if index is None:528 self.display_translation(word, True)529 else:530 self.display_translation(index)531class OptionsWidget(object):532 """ Class defining the Options/About tab layout """533 def __init__(self, data_object):534 self.data_object = data_object535 def build(self):536 vertical_box = Gtk.Box()537 vertical_box.set_orientation(Gtk.Orientation.VERTICAL)538 vertical_box.set_homogeneous(False)539 title = Gtk.Label("")540 vertical_box.pack_start(title, False, True, 20)541 horizontal_box = Gtk.Box()542 label = Gtk.Label("Romanisation:")543 horizontal_box.pack_start(label, True, True, 0)544 # List of romanisation available545 name_store = Gtk.ListStore(str)546 rom = ["Pinyin", "Zhuyin"]547 for value in rom:548 name_store.append([value])549 combo = Gtk.ComboBox.new_with_model(name_store)550 renderer_text = Gtk.CellRendererText()551 combo.pack_start(renderer_text, True)552 combo.add_attribute(renderer_text, "text", 0)553 # Set active the saved value554 if self.data_object.romanisation == 'zhuyin':555 combo.set_active(1)556 else:557 combo.set_active(0)558 combo.connect("changed", self.on_rom_combo)559 horizontal_box.pack_start(combo, True, True, 0)560 # Empty space561 space = Gtk.Label("")562 horizontal_box.pack_start(space, True, True, 20)563 horizontal_box.set_homogeneous(True)564 vertical_box.pack_start(horizontal_box, False, True, 5)565 horizontal_box = Gtk.Box()566 label = Gtk.Label("Character set:")567 horizontal_box.pack_start(label, True, True, 0)568 # List of romanisation available569 name_store = Gtk.ListStore(str)570 rom = ["Simplified", "Traditional"]571 for value in rom:572 name_store.append([value])573 combo = Gtk.ComboBox.new_with_model(name_store)574 renderer_text = Gtk.CellRendererText()575 combo.pack_start(renderer_text, True)576 combo.add_attribute(renderer_text, "text", 0)577 if self.data_object.hanzi == 'traditional':578 combo.set_active(1)579 else:580 combo.set_active(0)581 combo.connect("changed", self.on_char_combo)582 horizontal_box.pack_start(combo, True, True, 0)583 space = Gtk.Label("")584 horizontal_box.pack_start(space, True, True, 20)585 horizontal_box.set_homogeneous(True)586 vertical_box.pack_start(horizontal_box, False, True, 5)587 about_text = Gtk.Frame(label_yalign=1, label_xalign=1)588 about_text.set_label("\n\n\n\n"589 "Zhudi, 2011-2019")590 vertical_box.pack_start(about_text, True, True, 5)591 return vertical_box592 def on_rom_combo(self, combo):593 """ Action when the romanisation is changed. """594 tree_iter = combo.get_active_iter()595 if tree_iter != None:596 model = combo.get_model()597 value = model[tree_iter][0]598 self.data_object.romanisation = value.lower()599 self.data_object.save_config()600 def on_char_combo(self, combo):601 """ Action when the set is changed. """602 tree_iter = combo.get_active_iter()603 if tree_iter != None:604 model = combo.get_model()605 value = model[tree_iter][0]606 self.data_object.hanzi = value.lower()607 self.data_object.save_config()608class MainWindow(object):609 """ Class that defines the welcome screen, and gives access to other layers.610 """611 def __init__(self, data_object, language):612 self.window = Gtk.Window(type=Gtk.WindowType.TOPLEVEL)613 self.window.set_default_size(700, 500)614 self.window.set_title("Zhudi")615 self.window.set_position(Gtk.WindowPosition.CENTER)616 self.window.connect("key-release-event", self.on_key_release)617 self.data_object = data_object618 self.language = language619 self.dict_gui = None620 self.seg_gui = None621 self.tab_box = None622 self.vbox = None623 @staticmethod624 def loop():625 """ Starting things! """626 Gtk.main()627 def build(self):628 """ Mandatory build function. """629 self.data_object.create_set_chinese_characters()630 global DICTIONARY_TOOLS_OBJECT631 DICTIONARY_TOOLS_OBJECT = zhudi.processing.DictionaryTools()632 global SEGMENTATION_TOOLS_OBJECT633 SEGMENTATION_TOOLS_OBJECT = zhudi.processing.SegmentationTools()634 SEGMENTATION_TOOLS_OBJECT.load(self.data_object)635 # Welcome tab636 self.vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=2)637 # Tabs638 self.dict_gui = self.dictionary_gui()639 self.seg_gui = self.segmentation_gui()640 self.options_gui = self.options_gui()641 # Build the tab frame642 self.tab_box = Gtk.Notebook()643 self.tab_box.set_tab_pos(Gtk.PositionType.TOP)644 self.tab_box.append_page(self.dict_gui, None)645 self.tab_box.set_tab_label_text(self.dict_gui, "Dictionary")646 self.tab_box.append_page(self.seg_gui, None)647 self.tab_box.set_tab_label_text(self.seg_gui, "Segmentation")648 self.tab_box.append_page(self.options_gui, None)649 self.tab_box.set_tab_label_text(self.options_gui, "Options")650 self.window.add(self.tab_box)651 self.window.connect("destroy", Gtk.main_quit)652 self.window.show_all()653 def options_gui(self):654 """ Options tab. """655 return OptionsWidget(self.data_object).build()656 def dictionary_gui(self):657 """ Start the dictionary widget. """658 ui = DictionaryWidgetMain(self.data_object)659 ui.language = self.language660 return ui.build()661 def segmentation_gui(self):662 """ Start the segmentation widget. """663 return SegmentationWidget(self.data_object).build()664 @staticmethod665 def on_key_release(widget, event, data=None):666 """667 Crtl-w to quit the application.668 """669 if event.keyval == Gdk.KEY_w and event.state & Gdk.ModifierType.CONTROL_MASK:...

Full Screen

Full Screen

slashbot.py

Source:slashbot.py Github

copy

Full Screen

1import inspect2import hmac3import hashlib4import json5import boto36import requests7import sys8import time9import inspect10from urllib.parse import parse_qs11route_list = []12SLACK_SIGNING_SECRET = ""13SLACK_AUTHENTICATE_REQUEST = True14def get_route(full_command, routes):15 command = full_command.split()[0].lower()[16 1:17 ] # just the slash command, remove the slash18 # lets only look through routes that are correct command or default19 filtered_routes = [r for r in routes if r["slash"] in [command, "*"]]20 def word_count(route_entry):21 if route_entry["verb"] == "*": # force wildcards to be considered last22 return 023 return len(route_entry["verb"].split())24 clean_command = " ".join([word.lower() for word in full_command.split()[1:]])25 # brute force method, look through routes in order of # of words desc.26 sortedroutes = sorted(filtered_routes, key=word_count, reverse=True)27 for r in sortedroutes:28 route_verb = r["verb"]29 # add a space to both sides to black partial matches30 if (clean_command + " ").startswith(route_verb + " ") or route_verb == "*":31 return r32 # if there is no match return none33 return None34class SlashCommand:35 def __init__(self, event) -> None:36 self._payload = event.get("payload")37 self._params = parse_qs(event["body"])38 if not self._params.get("text"):39 self._text = self._params.get("command")[0]40 self.provided_verb = None41 else:42 self._text = (43 self._params.get("command")[0] + " " + self._params.get("text")[0]44 )45 self.provided_verb = self._text.split()[1]46 self.channel_id = self._params.get("channel_id")[0]47 self.channel_name = self._params.get("channel_name")[0]48 self.user_id = self._params.get("user_id")[0]49 self.user_name = self._params.get("user_name")[0]50 self.slash_text = self._params.get("command")[0][51 1:52 ] # we remove the \ from the left53 self.response_url = self._params.get("response_url")[0]54 self.team_domain = self._params.get("team_domain")[0]55 self.is_callback = event.get("is_callback")56 self.route = get_route(self._text, route_list)57 if self.route:58 print(self.route)59 self.verb = self.route.get("verb")60 verb_word_count = len(self.verb.split())61 if self.verb == "*":62 verb_word_count = 063 else:64 verb_word_count = len(self.verb.split())65 self.command_args = self._text.split()[verb_word_count + 1 :]66 else:67 self.verb = self._text.split()[1]68 def __str__(self):69 retval = f"""("slash_text":"{self.slash_text}")"""70 return retval71def invoke_lambda(lambda_name: str, payload: dict):72 payload_str = str.encode(json.dumps(payload))73 if "pytest" not in sys.modules: #74 boto3.client("lambda").invoke(75 FunctionName=lambda_name, Payload=payload_str, InvocationType="Event"76 )77def send_delayed_reply(url, msg, private=False, attachments=None):78 if "pytest" in sys.modules:79 return80 data = {"text": msg, "response_type": "ephemeral" if private else "in_channel"}81 if attachments:82 data["attachments"] = attachments83 RESPONSE = requests.post(84 url, data=json.dumps(data), headers={"Content-Type": "application/json"}85 )86 if RESPONSE.status_code != 200:87 raise ValueError(88 "Request to slack returned an error %s, the response is:\n%s"89 % (RESPONSE.status_code, RESPONSE.text)90 )91def route(verb, acl=None, slash="*"):92 if acl and type(acl) == str:93 user_list = [acl]94 elif type(acl) == list:95 user_list = acl96 else:97 user_list = []98 if slash and type(slash) == str:99 slash_list = [slash]100 elif type(slash) == list:101 slash_list = slash102 else:103 slash_list = ["*"]104 def inner_decorator(f):105 for slash in slash_list:106 entry = {107 "verb": verb.lower(),108 "handler": f,109 "acl": user_list,110 "slash": slash,111 }112 route_list.append(entry)113 return f114 return inner_decorator115def authenticate(event):116 if not SLACK_AUTHENTICATE_REQUEST:117 return True118 # The request timestamp is more than five minutes from local time.119 # It could be a replay attack, so let's ignore it.120 timestamp = event["headers"]["X-Slack-Request-Timestamp"]121 if abs(time.time() - float(timestamp)) > 60 * 5:122 # check for replay attach123 if "pytest" not in sys.modules:124 # suppress this check when under test125 raise (Exception("Invalid request due to timestamp"))126 sig_basestring = "v0:" + timestamp + ":" + event["body"]127 my_signature = (128 "v0="129 + hmac.new(130 bytes(SLACK_SIGNING_SECRET, "utf-8"),131 msg=bytes(sig_basestring, "utf-8"),132 digestmod=hashlib.sha256,133 ).hexdigest()134 )135 slack_signature = event["headers"]["X-Slack-Signature"]136 if slack_signature != my_signature:137 raise (Exception("Invalid request due to signature"))138 # The request timestamp is more than five minutes from local time.139 # It could be a replay attack, so let's ignore it.140 return True141def handle(cmd: SlashCommand):142 # route_entry = SlashCommand.143 if not cmd.route:144 reply = f"{cmd.verb} is not a valid command."145 return reply146 if cmd.is_callback:147 handler = cmd.route["handler"]148 else: # is a callback149 return ""150 acl = cmd.route.get("acl")151 if acl and cmd.user_name not in acl:152 reply = f"User {cmd.user_name} not authorized to execute {cmd.verb}"153 return reply154 fn_response = handler(cmd)155 if type(fn_response) == tuple:156 reply = fn_response[0]157 suppress_callback = fn_response[1]158 else:159 reply = fn_response160 suppress_callback = False # Dont suppress callback is default behavior161 return reply162def build_response(msg: str) -> dict:163 body = {"response_type": "in_channel", "text": msg}164 json_response = {"statusCode": 200, "body": json.dumps(body)}165 return json_response166def main(event, context):167 authenticate(event)168 cmd = SlashCommand(event)169 acl = cmd.route.get("acl")170 if not cmd.route:171 reply = f"{cmd.verb} is not a valid command."172 return build_response(reply)173 if acl and cmd.user_name not in acl:174 reply = f"User {cmd.user_name} not authorized to execute {cmd.verb}"175 return build_response(reply)176 if not event.get("is_callback"):177 event_clone = event.copy()178 event_clone["is_callback"] = True179 invoke_lambda(context.function_name, event_clone)180 return build_response("")181 reply = handle(cmd)182 send_delayed_reply(cmd.response_url, reply)...

Full Screen

Full Screen

auth_dir_utils.py

Source:auth_dir_utils.py Github

copy

Full Screen

1"""Extra utils for ldap.2Главная проблема - то, что приходит от AuthenticationDirectory в качестве GUID - совсем не бьется со стандартным3форматом UUID.4"""5import re6from typing import List, Optional7def split_into_chunks(string, chunk_length=2):8 """Split string to chunks fixed length."""9 chunks = []10 while len(string) > 0:11 chunks.append(string[:chunk_length])12 string = string[chunk_length:]13 return chunks14def to_oracle_raw16(string, strip_dashes=True, dashify_result=False):15 """Convert to raw16."""16 oracle_format_indices = [3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11, 12, 13, 14, 15]17 if strip_dashes:18 string = string.replace("-", "")19 parts = split_into_chunks(string)20 result = ""21 for index in oracle_format_indices:22 result = result + parts[index]23 if dashify_result:24 result = (25 result[:8]26 + "-" # noqa: W50327 + result[8:12] # noqa: W50328 + "-" # noqa: W50329 + result[12:16] # noqa: W50330 + "-" # noqa: W50331 + result[16:20] # noqa: W50332 + "-" # noqa: W50333 + result[20:] # noqa: W50334 )35 return result36def escape_bytes(bytes_value):37 """Convert a byte sequence to a properly escaped for LDAP (format BACKSLASH HEX HEX) string."""38 if bytes_value:39 if isinstance(bytes_value, str):40 bytes_value = bytearray(bytes_value, encoding="utf-8")41 escaped = "\\".join([("%02x" % int(b)) for b in bytes_value])42 else:43 escaped = ""44 result = ("\\" + escaped) if escaped else ""45 return result46def pack_guid(string):47 return escape_bytes(bytearray.fromhex(to_oracle_raw16(string)))48def unpack_guid(ba):49 """Convert bytes-string returned by AuthenticationDirectory."""50 hex_s = "".join("%02x" % b for b in ba)51 return to_oracle_raw16(hex_s, True, True)52def unpack_ad_info(ad_info: dict, param_name: str) -> bytes:53 """Проверяет наличие ожидаемой структуры и возвращает значение."""54 # Красиво не сработает, потому что применение условий должно быть последовательным55 if (56 isinstance(ad_info, dict)57 and ad_info.get(param_name) # noqa: W50358 and isinstance(ad_info[param_name], list) # noqa: W50359 and isinstance(ad_info[param_name][0], bytes) # noqa: W50360 ):61 return ad_info[param_name][0]62 return None63def extract_domain_from_username(username: str) -> List[str]:64 """Метод для разделения имени пользователя.65 Разделение по символу @ на имя пользовательской учетной записи и доменное имя контроллера доменов.66 :param username: имя пользователя67 :return: список, содержащий имя пользователской учетной записи (sAMAccountName)68 и доменное имя контроллера доменов69 """70 dog_list = username.split("@")71 if len(dog_list) > 1:72 return dog_list[0], dog_list[1]73 slash_list = username.split("\\")74 if len(slash_list) > 1:75 return slash_list[1], slash_list[0]76 backslash_list = username.split("/")77 if len(backslash_list) > 1:78 return backslash_list[1], None79 return username, None80def get_ad_user_ou(user_info: str) -> Optional[str]:81 """Получения информации о структурном подразделении.82 :param user_info: строка, содержащая имя структурного подразделения (OU).83 :return: имя структурного подразделения84 """85 if user_info:86 for chunk in re.split(r"(?<!\\),", user_info):87 if chunk.startswith("OU="):88 return chunk[3:]89def get_free_ipa_user_ou(user_info: str) -> Optional[str]:90 if not user_info:91 return92 if isinstance(user_info, bytes):93 user_info = user_info.decode()94 user_info = user_info.split(",")95 for attr in user_info:96 if attr.startswith("OU=") or attr.startswith("ou="):97 return attr[3:]98def get_ms_ad_user_groups(user_groups: List[bytes]) -> List[str]:99 """Метод получения имен групп пользователя службы каталогов.100 :param user_groups: список строк, содержащих имена (CN) групп пользователя службы каталогов.101 :return: список имен групп102 """103 groups_names = []104 for group in user_groups:105 group_attrs = re.split(r"(?<!\\),", group.decode())106 for attr in group_attrs:107 if attr.startswith("CN="):108 groups_names.append(attr[3:])109 return groups_names110def get_free_ipa_user_groups(user_groups: List[bytes]) -> List[str]:111 groups_names = []112 for group in user_groups:113 group_attr = group.decode().split(",")114 for attr in group_attr:115 if attr.startswith("CN=") or attr.startswith("cn="):116 groups_names.append(attr[3:])117 break...

Full Screen

Full Screen

helper.py

Source:helper.py Github

copy

Full Screen

1import numpy as np2import re3import os4def read_turian(cfg):5 Turianword = []6 pwordlookup = []7 if os.path.isfile(cfg.PRE_EMB_FILE):8 with open(cfg.PRE_EMB_FILE, 'r') as fp:9 for w in fp:10 # w_list = re.split(r" +", w.strip())11 w_list = w.strip().split(' ')12 Turianword.append(w_list[0])13 pwordlookup.append([float(f) for f in w_list[1:] if f])14 return (Turianword, pwordlookup)15def top_k_2D_col_indexes(arr: np.array, k: int):16 assert (len(arr.shape) == 2 and k >= 0 and k <= arr.size)17 tot_size = arr.size18 num_row = arr.shape[0]19 t = np.argpartition(arr.T.reshape((tot_size,)), -k)[-k:]20 res = t // num_row21 index = t % num_row22 return res, index23class judge():24 def __init__(self, slash, slash_id, symbol, symbol_id, lparent_index, rparent_index, EOS_index, ccg_tag, ccg_tag_id):25 self.slash = slash26 self.slash_id = slash_id27 self.symbol = symbol28 self.symbol_id = symbol_id29 self.lparent_index = lparent_index30 self.rparent_index = rparent_index31 self.EOS_index = EOS_index32 self.parent = {'(', ')'}33 self.parent_id = {self.lparent_index, self.rparent_index}34 self.ccg_tag = ccg_tag35 self.ccg_tag_id = ccg_tag_id36 def __call__(self, lparent_cnt, rparent_cnt, last, ccg_id, check):37 if last == '#' and ccg_id in self.slash_id:38 return False39 if last in self.slash:40 if (ccg_id in self.slash_id) or (ccg_id in self.symbol_id):41 return False42 if ccg_id == self.rparent_index:43 return False44 if ccg_id == self.EOS_index:45 return False46 if last == '(' and ccg_id in self.slash_id:47 return False48 if last == ')':49 if ccg_id in self.ccg_tag_id or ccg_id == self.lparent_index:50 return False51 if last in self.ccg_tag:52 if ccg_id in self.ccg_tag_id or ccg_id == self.lparent_index:53 return False54 if last != '#' and ccg_id in self.symbol_id:55 return False56 if ccg_id == self.rparent_index:57 if lparent_cnt == rparent_cnt:58 return False59 if last == '(':60 return False61 if ccg_id == self.EOS_index:62 if lparent_cnt != rparent_cnt:63 return False64 if ccg_id in self.slash_id:65 temp = check + '1'66 if temp.find("0101") != -1:67 return False68 if ccg_id == self.rparent_index:69 lindex = check.rfind('(')70 temp = check[lindex+1:]71 if len(temp) == 1:72 return False73 return True74class FindCCG():75 def __init__(self, vocab):76 # slash indexes77 self.slash = {'/', '\\'}78 self.slash_list = set()79 for example in self.slash:80 self.slash_list.add(vocab.get_token_index(example, 'atom_ccg'))81 self.symbol = {',', '.', ':', ';'}82 self.symbol_list = set()83 # punctuation symbols84 for example in self.symbol:85 self.symbol_list.add(vocab.get_token_index(example, 'atom_ccg'))86 self.ccg_tag = set(vocab.vocab['atom_ccg'].keys()) - self.slash - self.symbol - {'EOS', '(', ')'}87 self.ccg_tag_id = set()88 for example in self.ccg_tag:89 self.ccg_tag_id.add(vocab.get_token_index(example, 'atom_ccg'))90 self.lparent_index = vocab.get_token_index('(', 'atom_ccg')91 self.rparent_index = vocab.get_token_index(')', 'atom_ccg')92 self.EOS_index = vocab.get_token_index('EOS', 'atom_ccg')93 self.judge_legal = judge(self.slash, self.slash_list, self.symbol, self.symbol_list, self.lparent_index, self.rparent_index, self.EOS_index, self.ccg_tag,94 self.ccg_tag_id)95 self.tag_cnt = vocab.vocab_cnt['atom_ccg']96 self.vocab = vocab97 def initialize(self):98 # cnt used to add constraints on decoder99 self.last = '#'100 self.lparent_cnt = 0101 self.rparent_cnt = 0102 # string used for checking legal or not103 self.check = ''104 def __call__(self, y, ccg):105 while True:106 if not any(y):107 atom_ccg_id = 0108 while not self.judge_legal(self.lparent_cnt, self.rparent_cnt, self.last, atom_ccg_id, self.check):109 atom_ccg_id += 1110 break111 atom_ccg_id = np.argmax(y)112 if atom_ccg_id == self.EOS_index and (not ccg):113 y[atom_ccg_id] = 0114 continue115 if self.last in self.symbol:116 atom_ccg_id = self.EOS_index117 break118 if not self.judge_legal(self.lparent_cnt, self.rparent_cnt, self.last, atom_ccg_id, self.check):119 y[atom_ccg_id] = 0120 continue121 break122 output = self.vocab.get_token_from_index(atom_ccg_id, 'atom_ccg')123 if atom_ccg_id == self.lparent_index:124 self.lparent_cnt += 1125 elif atom_ccg_id == self.rparent_index:126 self.rparent_cnt += 1127 self.last = output128 if atom_ccg_id in self.ccg_tag_id:129 self.check += '0'130 elif atom_ccg_id in self.slash_list:131 self.check += '1'132 elif output == '(':133 self.check += output134 elif output == ')':135 lindex = self.check.rfind('(')136 check = self.check[:lindex]137 self.check = check + '0'138 elif atom_ccg_id in self.symbol_list:139 self.check += output140 return atom_ccg_id141def get_ccg_matrix(vocab):142 res_list = []143 for i in range(vocab.vocab_cnt['ccg']):144 ccg = vocab.get_token_from_index(i, 'ccg')145 split_str = r'([()/\\])'146 atom_ccg_list = re.split(split_str, ccg)147 atom_ccg_list = [x for x in atom_ccg_list if x != '']148 atom_ccg_list.append('EOS')149 res = []150 for example in atom_ccg_list:151 res.append(vocab.get_token_index(example, 'atom_ccg'))152 res_list.append(res)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Slash automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful