Re: Re: installing an end user editable file
Il giorno lun, 14/02/2011 alle 19.38 +0000, james frize ha scritto:
> Hey mentors,
>
> Firstly, thanks for all the help so far.
>
> I like the idea of having a default copy of the file used by my
> program, as suggested by Lars and PJ, so I've altered my source code
> to use a default file and allow multiple users to have their own
> copies of this file in their home/<user>/Documents folder.
>
> "creating a default file in /usr/share and having the program copy it
> to the user's directory when it's run"
>
> However, I can't open a file from /usr/share via my python script as I
> don't have permission to open files in a protected directory, and
> after reading up a bit on it, it's been suggested that it's bad form
> to invoke super user in a script, as it's construed as a security risk
> (i.e. http://tldp.org/HOWTO/Security-HOWTO/file-security.html;
> http://www.velocityreviews.com/forums/t346075-sudo-open-python-newbee-question.html)
>
> Do I really have to use sudo or super user privileges to just open and
> copy a text template?
No, you don't.
Just try
python -c "print open('/usr/share/common-licenses/GPL').readline()"
, but more in general: just try.
>
> Where do packagers normally install files of this type?
>
> I thought it might be a good idea to store it in
> home/<user>/.<packageName> But I'm still at a loss as to how I can
> find a users home directory from the rules makefile :(
You've already been explained that this is _wrong_, regardingless of how
you do it.
>
> been trying to use ~/ and $XDG_CONFIG_HOME but I just end up with
> everything being installed in folders called "~" or "DG_CONFIG_HOME"
>
> Here's the source code if you need more info:
>
> #!/usr/bin/python
>
> # Code Name: gtk-link-lizard
> # Created By: Jim Frize, <http://sonodrome.co.uk>
> # Copyright (C) 2011 Jim Frize, <jim@sonodrome.co.uk>
>
> # Licence: GTK Link Lizard is free software: you can redistribute it
> and/or modify it under the terms of the GNU General Public License as
> published by the Free Software Foundation, either version 3 of the
> License, or (at your option) any later version. This program is
> distributed in the hope that it will be useful, but WITHOUT ANY
> WARRANTY; without even the implied warranty of MERCHANTABILITY or
> FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
> for more details. You should have received a copy of the GNU General
> Public License along with this program. If not, see
> <http://www.gnu.org/licenses/>
>
> # Version: 1.5
> # Description: Simple GUI for storing and displaying web links
>
> # This GUI will do away with synchronizing bookmarks between browsers,
> web links are stored in a regular text file which is found in your
> documents folder. GTK Link Lizard makes it easy to edit, swap or
> replace a list of clickable links
>
> import pygtk
> pygtk.require("2.0")
> import gtk
> import sys
> import re
> import os
>
> path = os.getenv("HOME")
>
> class link_lizard():
>
> def __init__(self):
>
> self.load_main()
>
> #############
> # Load File #
> #############
> def load_file(self):
> try:
> self.text_file = open(path +
> "/Documents/gtk-link-lizard/links.txt","r+")
> except IOError:
> print "loading links.txt from usr/share/doc/gtk-link-lizard..."
> try:
> self.text_file =
> open("usr/share/doc/gtk-link-lizard/links.txt","r")
> self.text_file.write(path +
> "/Documents/gtk-link-lizard/links.txt")
> self.text_file = open(path +
> "/Documents/gtk-link-lizard/links.txt","r+")
> except IOError:
> print "ERROR: link.txt not found"
>
> #################
> # Load main GUI #
> #################
> def load_main(self, data=None):
> # Create main window
> self.main_window = gtk.Window(gtk.WINDOW_TOPLEVEL)
> # Quit main function when window is destroyed
> self.main_window.connect("destroy", gtk.main_quit)
> self.main_window.set_size_request(600, 600)
> self.main_window.set_position(gtk.WIN_POS_CENTER)
> self.main_window.set_opacity(0.9)
> self.main_window.set_title("GTK Link Lizard")
> self.main_window.set_keep_above(True)
>
> # Create scrolled window
> scrolled_window = gtk.ScrolledWindow()
> scrolled_window.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
>
> # Create placement boxes and dictionaries
> main_box = gtk.VBox(homogeneous=False)
> link_box = gtk.VBox(homogeneous=False)
> linkbutton = {}
> label = {}
>
> # Create edit button
> edit_button = gtk.Button("Edit Links")
> edit_button.connect("clicked", self.load_edit)
>
> # Count number of lines in text file
> self.load_file()
> number_lines = len(self.text_file.readlines())
>
> # Reset counter and check through each line of text
> count = 0
>
> while(count < number_lines):
> self.load_file()
> all_lines = self.text_file.readlines()
> current_line = all_lines[count]
> match = re.search("http://", current_line)
> match2 = re.search("https://", current_line)
> match3 = re.search("www.", current_line)
>
> # If http link is found, make a linkButton
> if match:
> current_url = match.group()
> # Remove http://
> split_line = current_line.split("http://")
>
> if match3:
> # Remove www.
> split_line = split_line[1].split("www.")
>
> linkbutton[count] = gtk.LinkButton(current_line, split_line[1])
> linkbutton[count].set_size_request(600, 30)
> link_box.pack_start(linkbutton[count], expand=False)
>
> # If https link is found, make a linkButton
> elif match2:
> current_url = match2.group()
> # Remove https://
> split_line = current_line.split("https://")
>
> if match3:
> # Remove www.
> split_line = split_line[1].split("www.")
>
> linkbutton[count] = gtk.LinkButton(current_line, split_line[1])
> linkbutton[count].set_size_request(600, 30)
> link_box.pack_start(linkbutton[count], expand=False)
>
>
> # If no link is found, add text as a label
> else:
> label[count] = gtk.Label(current_line)
> label[count].set_size_request(600, 20)
> link_box.pack_start(label[count], expand=False)
>
> count+=1
>
> # Add everything
> main_box.pack_start(edit_button, expand=False)
> scrolled_window.add_with_viewport(link_box)
> main_box.add(scrolled_window)
> self.main_window.add(main_box)
> self.main_window.show_all()
>
> #################
> # Load edit GUI #
> #################
> def load_edit(self, data=None):
> # Hide main window
> self.main_window.hide()
> # Create edit window
> self.edit_window = gtk.Window(gtk.WINDOW_TOPLEVEL)
> # Return to main window when edit window is destroyed
> self.edit_window.connect("destroy", self.load_main)
> self.edit_window.set_size_request(600, 600)
> self.edit_window.set_position(gtk.WIN_POS_CENTER)
> self.edit_window.set_title("GTK Link Lizard (Editing Links)")
>
> # Create scrolled window
> scrolled_window = gtk.ScrolledWindow()
> scrolled_window.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
>
> # Create boxes for layout
> main_box = gtk.VBox(homogeneous=False)
> text_box = gtk.VBox(homogeneous=False)
> button_box = gtk.HBox(homogeneous=False)
>
> # Create buttons
> save_button = gtk.Button("Save")
> save_button.connect("clicked", self.save_edit)
> cancel_button = gtk.Button("Cancel")
> cancel_button.connect("clicked", self.cancel_edit)
>
> # Open file and display in text viewer
> self.load_file()
> all_text = self.text_file.read()
> text_buffer = gtk.TextBuffer()
> text_buffer.set_text(all_text)
> self.text_view = gtk.TextView(text_buffer)
>
> # Add everything
> button_box.add(save_button)
> button_box.add(cancel_button)
> text_box.add(self.text_view)
> scrolled_window.add_with_viewport(text_box)
> main_box.pack_start(button_box, expand=False)
> main_box.add(scrolled_window)
> self.edit_window.add(main_box)
> self.edit_window.show_all()
>
> #############
> # Save edit #
> #############
> def save_edit(self, data=None):
> save_buffer = self.text_view.get_buffer()
> save_text = save_buffer.get_text(save_buffer.get_start_iter(),
> save_buffer.get_end_iter())
> self.load_file()
> self.text_file.write(save_text)
> self.edit_window.destroy()
>
> ###############
> # Cancel edit #
> ###############
> def cancel_edit(self, data=None):
> self.edit_window.destroy()
>
> #################
> # Main function #
> #################
> def main(self):
> gtk.main()
>
> # Run main function when class is called
> if __name__ == "__main__":
> new_link_lizard = link_lizard()
> new_link_lizard.main()
>
>
Reply to: