From 552230dc3e27a9c7f566718e981d4e61513cca3d Mon Sep 17 00:00:00 2001 From: Eugene Mirotin Date: Sat, 11 Feb 2017 12:07:26 +0300 Subject: [PATCH] fix #2 - init globals before using them --- ice | 144 ++++++++++++++++++++++++++++++------------------------------ 1 file changed, 73 insertions(+), 71 deletions(-) diff --git a/ice b/ice index 5cbf647..fc1c4dd 100755 --- a/ice +++ b/ice @@ -50,7 +50,7 @@ def get_details(app): pixbuf = Pixbuf.new_from_file_at_size("/usr/share/pixmaps/ice.png", 16, 16) elif "StartupWMClass=Chromium" in line: is_ice = True - + if not nameline == None and not iconline == None and is_ice == True: return (nameline, pixbuf) else: @@ -95,31 +95,31 @@ def download_favicon(url, file_prefix='', target_dir='/tmp'): favicon_path, favicon_filename = os.path.split(favicon_filepath) valid_chars = "-_.() %s%s" % (string.ascii_letters, string.digits) - + sanitized_filename = "".join([x if valid_chars \ else "" for x in favicon_filename]) - - sanitized_filename = os.path.join(target_dir, file_prefix + + + sanitized_filename = os.path.join(target_dir, file_prefix + sanitized_filename) - + with open(sanitized_filename, 'wb') as f: - for chunk in response.iter_content(chunk_size=1024): + for chunk in response.iter_content(chunk_size=1024): if chunk: # filter out keep-alive new chunks f.write(chunk) f.flush() - + return sanitized_filename def parse_markup_for_favicon(markup, url): parsed_site_uri = urlparse(url) - + soup = BeautifulSoup(markup) - + icon_link = soup.find('link', rel='icon') if icon_link and icon_link.has_attr('href'): - + favicon_url = icon_link['href'] - + if favicon_url.startswith('//'): parsed_uri = urlparse(url) favicon_url = parsed_uri.scheme + ':' + favicon_url @@ -127,12 +127,12 @@ def parse_markup_for_favicon(markup, url): elif favicon_url.startswith('/'): favicon_url = parsed_site_uri.scheme + '://' + \ parsed_site_uri.netloc + favicon_url - + elif not favicon_url.startswith('http'): path, filename = os.path.split(parsed_site_uri.path) favicon_url = parsed_site_uri.scheme + '://' + \ parsed_site_uri.netloc + '/' + os.path.join(path, favicon_url) - + return favicon_url return None @@ -144,16 +144,16 @@ def get_favicon_url(url): response = requests.get(url, headers=headers) except: raise Exception("Unable to find URL. Is it valid? %s" % url) - + if response.status_code == requests.codes.ok: favicon_url = parse_markup_for_favicon(response.content, url) if favicon_url: return favicon_url - + favicon_url = '{uri.scheme}://{uri.netloc}/favicon.ico'.format(\ uri=parsed_site_uri) - + response = requests.get(favicon_url, headers=headers) if response.status_code == requests.codes.ok: return favicon_url @@ -163,13 +163,13 @@ def get_favicon_url(url): def applicate(): title = name.get_text() address = normalize(url.get_text()) - + semiformatted = "" array = filter(str.isalpha, title) for obj in array: semiformatted = semiformatted + obj formatted = semiformatted.lower() - + loc = where.get_active_text() if loc == "Accessories": location = "Utility;" @@ -187,11 +187,11 @@ def applicate(): location = "AudioVideo;" elif loc == "System Tools": location = "System;" - + global iconpath iconname = iconpath.replace("/", " ").split()[-1] iconext = iconname.replace(".", " ").split()[-1] - + if os.path.exists(os.path.expanduser("~/.local/share/applications/" + formatted + ".desktop")): DuplicateError(title, formatted, address, iconext, location) else: @@ -232,7 +232,7 @@ def writefile(title, formatted, address, iconext, location): if (browser == "ice-firefox"): address1 = str.replace(address, 'http://', '') appfile1.write("IceFirefox=" + str.replace(address1, 'https://', '')) - + name.set_text("") url.set_text("") iconpath = "/usr/share/pixmaps/ice.png" @@ -240,13 +240,13 @@ def writefile(title, formatted, address, iconext, location): icon.set_from_pixbuf(new_icon) iconline, pixbuf = get_details(appfile) liststore.prepend([pixbuf, iconline]) - + def delete(button): a = iconview.get_selected_items() b = liststore.get_iter(a[0]) c = liststore.get_value(b, 1) liststore.remove(b) - + semiformatted = "" array = filter(str.isalpha, c) for obj in array: @@ -275,14 +275,14 @@ class IconSel(Gtk.FileChooserDialog): def update_image(dialog): filename = dialog.get_preview_filename() - + try: pixbuf = Pixbuf.new_from_file(filename) preview.set_from_pixbuf(pixbuf) valid_preview = True except: valid_preview = False - + dialog.set_preview_widget_active(valid_preview) filew = Gtk.FileChooserDialog("Please choose an icon.", None, Gtk.FileChooserAction.OPEN, (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OPEN, Gtk.ResponseType.OK)) @@ -299,7 +299,7 @@ class IconSel(Gtk.FileChooserDialog): filter1.add_pattern("*.xpm") filter1.add_pattern("*.svg") filew.add_filter(filter1) - + preview = Gtk.Image() filew.set_preview_widget(preview) filew.connect("update-preview", update_image) @@ -315,15 +315,15 @@ class IconSel(Gtk.FileChooserDialog): filew.destroy() class NoBrowserError(Gtk.Window): - + def destroy(self, button): self.close() - + def __init__(self): Gtk.Window.__init__(self, title="Browser Error") self.set_size_request(250, 130) self.set_icon_from_file("/usr/share/pixmaps/ice.png") - + print("test") main_lab = Gtk.Label() @@ -337,24 +337,24 @@ class NoBrowserError(Gtk.Window): box = Gtk.HBox() box.pack_start(void, True, True, 0) box.pack_start(close, False, False, 0) - + main_vbox = Gtk.VBox() main_vbox.pack_start(main_lab, False, False, 10) main_vbox.pack_start(text_lab, False, False, 0) main_vbox.pack_start(box, False, False, 10) - + main_hbox = Gtk.HBox() main_hbox.pack_start(main_vbox, True, True, 10) self.add(main_hbox) self.show_all() class DuplicateError(Gtk.Window): - + def destroy(self, button): self.close() - + def okay_clicked(self, button, title, formatted, address, iconext, location): - + for item in liststore: itemiter = item.iter semiformatted = "" @@ -362,7 +362,7 @@ class DuplicateError(Gtk.Window): for obj in array: semiformatted = semiformatted + obj forma = semiformatted.lower() - + if forma == formatted: liststore.remove(itemiter) @@ -373,7 +373,7 @@ class DuplicateError(Gtk.Window): Gtk.Window.__init__(self, title="Duplication Error") self.set_size_request(250, 130) self.set_icon_from_file("/usr/share/pixmaps/ice.png") - + main_lab = Gtk.Label() main_lab.set_markup("Warning: File Duplication Error") text_lab = Gtk.Label("The name of the SSB will cause an existing SSB to\nbe overwritten. To prevent this, change a letter in\nthe name. Continue anyway?") @@ -387,31 +387,31 @@ class DuplicateError(Gtk.Window): box.pack_start(void, True, True, 0) box.pack_start(okay, False, False, 10) box.pack_start(cancel, False, False, 0) - + main_vbox = Gtk.VBox() main_vbox.pack_start(main_lab, False, False, 10) main_vbox.pack_start(text_lab, False, False, 0) main_vbox.pack_start(box, False, False, 10) - + main_hbox = Gtk.HBox() main_hbox.pack_start(main_vbox, True, True, 10) self.add(main_hbox) self.show_all() class AddressError(Gtk.Window): - + def destroy(self, button): self.close() - + def okay_clicked(self, button): applicate() self.close() - + def __init__(self): Gtk.Window.__init__(self, title="Address Error") self.set_size_request(250, 130) self.set_icon_from_file("/usr/share/pixmaps/ice.png") - + main_lab = Gtk.Label() main_lab.set_markup("Warning: HTTP or URL Error") text_lab = Gtk.Label("An error with the web address has been detected.\nThis is possibly the site being down or unavailable\nright now. Continue anyway?") @@ -425,22 +425,22 @@ class AddressError(Gtk.Window): box.pack_start(void, True, True, 0) box.pack_start(okay, False, False, 10) box.pack_start(cancel, False, False, 0) - + main_vbox = Gtk.VBox() main_vbox.pack_start(main_lab, False, False, 10) main_vbox.pack_start(text_lab, False, False, 0) main_vbox.pack_start(box, False, False, 10) - + main_hbox = Gtk.HBox() main_hbox.pack_start(main_vbox, True, True, 10) self.add(main_hbox) self.show_all() class Ice(Gtk.Window): - + def destroy(self, button): Gtk.main_quit() - + def icon_select(self, button): IconSel() @@ -471,17 +471,17 @@ class Ice(Gtk.Window): iconpath = "/usr/share/pixmaps/ice.png" new_icon = Pixbuf.new_from_file_at_size(iconpath, 32, 32) icon.set_from_pixbuf(new_icon) - + def __init__(self): Gtk.Window.__init__(self, title="Ice") self.current_directory = os.path.realpath(os.path.expanduser('~') + "/.local/share/applications/") self.set_size_request(460, 350) self.set_icon_from_file("/usr/share/pixmaps/ice.png") - + ###################### ### 'Create' page. ### ###################### - + welcome = Gtk.Label() welcome.set_markup("Welcome to Ice, a simple SSB manager.") global name @@ -490,7 +490,7 @@ class Ice(Gtk.Window): global url url = Gtk.Entry() url.set_placeholder_text("Enter web address") - + where_store = ["Accessories", "Games", "Graphics", "Internet", "Office", "Programming", "Sound & Video", "System Tools"] where_lab = Gtk.Label("Where in the menu?") global where @@ -499,60 +499,62 @@ class Ice(Gtk.Window): for entry in where_store: where.append_text(entry) where.set_active(3) - + where_box = Gtk.HBox() where_void = Gtk.Label() where_box.pack_start(where_lab, False, False, 0) where_box.pack_start(where_void, False, False, 10) where_box.pack_start(where, True, True, 0) - + global iconpath iconpath = "/usr/share/pixmaps/ice.png" icon_pixbuf = Pixbuf.new_from_file_at_size(iconpath, 32, 32) global icon icon = Gtk.Image() icon.set_from_pixbuf(icon_pixbuf) - + icon_void = Gtk.Label() icon_box = Gtk.HBox() icon_box.pack_start(icon, False, False, 10) icon_box.pack_start(icon_void, False, False, 10) - + choose_icon = Gtk.Button("Select an icon") choose_icon.connect("clicked", self.icon_select) download_icon = Gtk.Button("Use site favicon") download_icon.connect("clicked", self.icon_download) - + icon_vbox = Gtk.VBox() icon_vbox.pack_start(choose_icon, True, True, 5) icon_vbox.pack_start(download_icon, True, True, 5) icon_hbox = Gtk.HBox() icon_hbox.pack_start(icon_box, False, False, 10) icon_hbox.pack_start(icon_vbox, True, True, 0) - + global firefox firefox = Gtk.RadioButton.new_with_label_from_widget(None, "Firefox") + global chrome + chrome = Gtk.RadioButton.new_from_widget(firefox) + global chromium + chromium = Gtk.RadioButton.new_from_widget(chrome) + if not os.path.exists("/usr/bin/firefox"): firefox.set_sensitive(False) if not os.path.exists("/usr/bin/chromium-browser") and not os.path.exists("/usr/bin/google-chrome") and os.path.exists("/usr/bin/firefox"): chrome.set_active(True) - global chrome - chrome = Gtk.RadioButton.new_from_widget(firefox) + chrome.set_label("Chrome") if not os.path.exists("/usr/bin/google-chrome"): chrome.set_sensitive(False) if not os.path.exists("/usr/bin/chromium-browser") and not os.path.exists("/usr/bin/firefox") and os.path.exists("/usr/bin/google-chrome"): chrome.set_active(True) - global chromium - chromium = Gtk.RadioButton.new_from_widget(chrome) chromium.set_label("Chromium") if not os.path.exists("/usr/bin/chromium-browser"): chromium.set_sensitive(False) if not os.path.exists("/usr/bin/google-chrome") and not os.path.exists("/usr/bin/firefox") and os.path.exists("/usr/bin/chromium-browser"): chromium.set_active(True) - + apply_button = Gtk.Button.new_from_stock(Gtk.STOCK_APPLY) apply_button.connect("clicked", self.apply_clicked) close_button = Gtk.Button.new_from_stock(Gtk.STOCK_CLOSE) @@ -565,7 +567,7 @@ class Ice(Gtk.Window): button_box.pack_start(button_void, True, True, 0) button_box.pack_start(apply_button, False, False, 20) button_box.pack_start(close_button, False, False, 0) - + create_vbox = Gtk.VBox() create_vbox.pack_start(welcome, False, False, 15) create_vbox.pack_start(name, False, False, 0) @@ -573,15 +575,15 @@ class Ice(Gtk.Window): create_vbox.pack_start(where_box, False, False, 10) create_vbox.pack_start(icon_hbox, False, False, 10) create_vbox.pack_start(button_box, False, False, 0) - + create_hbox = Gtk.HBox() create_hbox.pack_start(create_vbox, True, True, 20) create_lab = Gtk.Label("Create") - + ###################### ### 'Remove' page. ### ###################### - + global liststore liststore = Gtk.ListStore(Pixbuf, str) for fl in os.listdir(os.path.expanduser("~/.local/share/applications")): @@ -597,10 +599,10 @@ class Ice(Gtk.Window): iconview.set_pixbuf_column(0) iconview.set_text_column(1) iconview.set_selection_mode(1) - + scroll = Gtk.ScrolledWindow() scroll.add(iconview) - + remove = Gtk.Button.new_from_stock(Gtk.STOCK_REMOVE) remove.connect("clicked", delete) close = Gtk.Button.new_from_stock(Gtk.STOCK_CLOSE) @@ -610,15 +612,15 @@ class Ice(Gtk.Window): buttons.pack_start(void, True, True, 0) buttons.pack_start(remove, False, False, 20) buttons.pack_start(close, False, False, 0) - + remove_vbox = Gtk.VBox() remove_vbox.pack_start(scroll, True, True, 10) remove_vbox.pack_start(buttons, False, False, 17) - + remove_hbox = Gtk.HBox() remove_hbox.pack_start(remove_vbox, True, True, 20) remove_lab = Gtk.Label("Remove") - + ########################## ### Main window stuff. ### ########################## @@ -626,7 +628,7 @@ class Ice(Gtk.Window): notebook = Gtk.Notebook() notebook.append_page(create_hbox, create_lab) notebook.append_page(remove_hbox, remove_lab) - + main_vbox = Gtk.VBox() main_vbox.pack_start(notebook, True, True, 10) main_hbox = Gtk.HBox()