user@linuxtrack:~ $ python -c 'print("Soyez les bienvenus !")'

Vous n'êtes pas identifié(e).

#1 17-12-2014 20:13:28

IceF0x
#! Gourou Linux

m_partage mise à jour

Voici une petite mise à jour du code de m_partage que je me suis permis de faire.
J'aurais préférer lui soumettre mes idées comme je le faisais habituellement avec elle, plutôt que changer moi même le code, mais la vie en a décidé autrement.

1418839823_capture_d_ecran_-_17122014_-_19_05_57.png

Ajout du code pour centrer la fenêtre
Ajout de l'icône de m_partage dans la barre de tableau de board 1418839841_capture_d_ecran_-_17122014_-_19_06_14.png
Ajout de la licence libre + hommage à manon

#!/usr/bin/env python
# -*-coding:utf-8-*-

##########################################################
#m_partage                                               #
#by manon R.I.P. <http://shovel-crew.org>                #
#À la mémoire de Manon décédée le 13/12/2014             #
#Nous ne t'oublierons jamais                             #
#Under licence GPLv3 http://www.gnu.org/licenses/gpl.txt #
##########################################################

import pygtk
pygtk.require("2.0")
import gtk, subprocess, threading, gobject, os, sys
import SimpleHTTPServer, SocketServer
from os import chdir
import ConfigParser
import locale

gobject.threads_init()

PORT = 9900
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)

ip_public = subprocess.Popen("wget -qO- icanhazip.com", shell=True, stdout=subprocess.PIPE)
lien_de_partage_public = ip_public.communicate()[0].rstrip()
ip_locale = subprocess.Popen("ip addr show $(ip link | grep -m 1 \"state UP\" | awk -F\":\" '{print $2}') | grep inet\  | awk -F\" \" '{print $2}' | awk -F\"/\" '{print $1}'", shell=True, stdout=subprocess.PIPE)
lien_de_partage_locale = ip_locale.communicate()[0]
listNettoyage = []

LANG = locale.setlocale(locale.LC_ALL, "")[0:2]

if os.path.isdir("/usr/share/m_partage/lang/" + LANG) == True:
	FICHIER_CONF = "/usr/share/m_partage/lang/" + LANG + "/mpartage.conf"
else:
	FICHIER_CONF = "/usr/share/m_partage/lang/en/mpartage.conf"
	print "translation missing"

config = ConfigParser.RawConfigParser()
config.read(FICHIER_CONF)

class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
	serveur_version = '1.0'

class Serveur_thread(threading.Thread):
	def __init__(self):
		threading.Thread.__init__ (self)
	def run(self):
		httpd.serve_forever()

class Systray():
	def __init__(self, window, timer):
		self.window = window
		self.icon = gtk.StatusIcon()
		self.icon.set_from_icon_name('m_partage')
		self.icon.set_visible(True)

		self.icon.connect('activate', self.affiche)
		gobject.timeout_add_seconds(timer, self.show_icons)

	def affiche(self, widget):
		self.icon.set_visible(False)
		self.window.show()

	def show_icons(self):
		self.icon.set_visible(True)

class Main():
	
	def motion_cb(self, wid, context, x, y, time):
		# selection du widget envoi du signal copie
		context.drag_status(gtk.gdk.ACTION_COPY, time)
		return True

	def drop_cb(self, wid, context, x, y, time):
		wid.drag_get_data(context, context.targets[-1], time)
		return True

	def got_data_cb(self, wid, context, x, y, data, info, time):
		# Le dossier est déposé dans la fenetre,  deposer
		emplacement = data.get_uris()[0].split('file://')[1]
		emplacement = self.m_decode(emplacement)
		try:
			chdir(emplacement)
			self.creation_page(emplacement)
			self.label.set_text("\n"+emplacement+"\n\nhttp://"+lien_de_partage_locale.rstrip()+":"+str(PORT)+"\n\nhttp://"+lien_de_partage_public+":"+str(PORT)+"\n\n")
		except OSError:
			fichier = os.path.basename(emplacement)
			emplacement = emplacement.rstrip(fichier)
			chdir(emplacement)
			self.label.set_text("\n"+fichier+"\n\nhttp://"+lien_de_partage_locale.rstrip()+":"+str(PORT)+"/"+fichier+"\n\nhttp://"+lien_de_partage_public+":"+str(PORT)+"/"+fichier+"\n\n")

		self.label.set_selectable(True)
		context.finish(True, False, time)

	def quitter(self, widget, boutonValidation, typePartage, window):
		# On recupére la valeur des boutons pour s'avoir comment quitter
		if boutonValidation.get_active() and typePartage.get_active() == False:
			for fichier in listNettoyage:
				os.remove(fichier)
			httpd.shutdown()
			gtk.main_quit()
		if boutonValidation.get_active() and typePartage.get_active() == True:
			# le bouton garder le partage actif est pas actif on cache la fenetre
			window.hide ()
			Systray(window, 300)
		else:
			gtk.main_quit()

	def evnmt_delete(self, widget, evenement, donnees=None):
		# empeche la fermeture de la fenetre au clique sur la croix
		Systray(widget, 300)
		widget.hide ()
		return True

	def partage(self, widget, boutonValidation):
		# si le bouton Activer le partage est active on démarre le serveur sinon on le coupe
		if widget.get_active():
			serveur =  Serveur_thread()
			serveur.start()	
			boutonValidation.set_label(config.get('conf', 'sharing_on'))
		else:
			httpd.shutdown()
			boutonValidation.set_label(config.get('conf', 'enable_sharing'))
			if len(listNettoyage) > 0:
				for fichier in listNettoyage:
					os.remove(fichier)

	def m_decode(self, emplacement):
		liste = []
		searchCode = emplacement.split('%')
		searchCode.remove(searchCode[0])

		for index, element in enumerate(searchCode):
			if len(element) == 2 and element != searchCode[-1]:
				liste.append( element + searchCode[index + 1][:2])

		emplacement = emplacement.replace('%', '')
		for element in liste:
			if element in emplacement:
				emplacement = emplacement.replace(element, element.decode('hex'))

		return emplacement

	def creation_page(self, emplacement):
		# Fonction qui recupére les fichiers et dossier a partager et créer la page html
		liste_images, list_dossiers, list_doc, list_download, list_multimedia = [], [], [], [], []
		liste_fichiers = os.listdir(os.getcwd())
		indexHtml = open(os.getcwd()+"/index.html", 'w')
		indexHtml.write('<html>\n<meta charset="utf-8"/>\n<title>' + config.get('conf', 'html_title') + '</title>\n<body  style="background-color:#e1e1e1;color:#222;">\n<a href="../">' + config.get('conf', 'updir') + '</a>\n<h2>' + config.get('conf', 'html_title2') + '</h2>\n<hr>\n')
		listNettoyage.append(os.getcwd()+"/index.html")	
		for fichier in liste_fichiers:
			if os.path.isdir(fichier) == True:
				list_dossiers.append(fichier)
				chdir(fichier)
				self.creation_page(emplacement)# Rappel de la fonction pour créer page dans les sous dossiers
			if os.path.splitext(fichier)[1].lower() in ['.jpg', '.png', '.gif', '.jpeg', '.xcf', '.bmp', '.xpm', '.psd', '.psp', '.tif', '.tiff', '.ai', '.aac', '.ico', '.ppm', '.xbm', '.svg']:
				liste_images.append(fichier)
			if os.path.splitext(fichier)[1].lower() in  ['.mp3', '.flv', '.mp4', '.m4v', '.mpg', '.mpeg', '.m4a', '.wma', '.mov', '.avi', '.mkv', '.ogg', '.ogv', '.ogm', '.wmv', '.wav', '.mid', '.ac3', '.aif', '.aifc', '.aiff', '.divx', '.flac', '.m3u', '.ra', '.rv', '.vob', '.webm']:
				list_multimedia.append(fichier)
			if os.path.splitext(fichier)[1].lower() in ['.py', '.sh', '.pl', '.rb', '.txt', '.css', '.pdf', '.php', '.odb', '.odc', '.odf', '.odg', '.odp', '.ods', '.odt', '.ott', '.otg', '.oth', '.ots', '.sxw', '.stw', '.fodt', '.uot', '.doc', '.docx', '.dot', '.dotm', '.dotx', '.xml', '.pdb', '.psw', '.ppt', '.pps', '.xhtml', '.html', '.htm', '.rtf', '.ini', '.xls', '.abw', '.c', '.cc', '.cp', '.cpp', '.cs', '.csv', '.cfg', '.h', '.info', '.js', '.jav', '.java', '.latex', '.log', '.old', '.ps', '.rc', '.rdf']:
				list_doc.append(fichier)
			if os.path.splitext(fichier)[1] == '' and  os.path.isfile(fichier) == True:
				list_doc.append(fichier)
			if os.path.splitext(fichier)[1].lower() in ['.deb', '.gnu', '.gz', '.tar', '.tar.gz', '.tgz', '.zip', '.bzip', '.rar', '.bz', '.bz2', '.7z', '.cbr', '.arc', '.arj', '.ark', '.exe', '.run', '.bin', '.ttf', '.bat', '.iso', '.torrent']:
				list_download.append(fichier)

		if len(list_dossiers) != 0:
			indexHtml.write('<h4>' + config.get('conf', 'dirfile') + '/</h4>\n<ul>\n')
			for dossier in list_dossiers:
				indexHtml.write("<li><a href='"+dossier+"'>"+dossier+"</a></li>\n")
			indexHtml.write('</ul>\n<hr>\n')

		if len(liste_images) != 0:
			indexHtml.write('<h4>' + config.get('conf', 'dirpics') + '/</h4>\n<ul>\n')
			for image in liste_images:
				indexHtml.write("<li><a href='"+image+"'>"+image+"</a></li>\n")
			indexHtml.write('</ul>\n<hr>\n')

		if len(list_multimedia) != 0:
			indexHtml.write('<h4>' + config.get('conf', 'dirmedia') + '/</h4>\n<ul>\n')
			for media in list_multimedia:
				indexHtml.write("<li><a href='"+media+"'>"+media+"</a></li>\n")
			indexHtml.write('</ul>\n<hr>\n')

		if len(list_download) != 0:
			indexHtml.write('<h4>' + config.get('conf', 'dirdown') + '/</h4>\n<ul>\n')
			for down in list_download:
				indexHtml.write("<li><a href='"+down+"'>"+down+"</a></li>\n")
			indexHtml.write('</ul>\n<hr>\n')

		if len(list_doc) != 0:
			indexHtml.write('<h4>' + config.get('conf', 'dirdocs') + '/</h4>\n<ul>\n')
			for doc in list_doc:
				indexHtml.write("<li><a href='"+doc+"'>"+doc+"</a></li>\n")
			indexHtml.write('</ul>\n<hr>\n')

		indexHtml.write('<p align=center ><FONT size="2"><b>m_partage</b> par manon@<a href="http://www.shovel-crew.org">Shovel-crew.org</a></FONT></p><hr>\n</body>\n</html>')
		indexHtml.close()
		chdir(emplacement)

	def __init__(self):
		
		window = gtk.Window()
		window.set_size_request(450, 250)
		window.set_position(gtk.WIN_POS_CENTER)
		window.set_border_width(15)
		window.connect('delete_event', self.evnmt_delete, window)
		window.set_icon_from_file("/usr/share/icons/m_partage.png")

		vBox = gtk.VBox()
		cadre = gtk.Frame(label=config.get('conf', 'lbl_1'))
		cadre.set_label_align(0.0, 0.5)
		cadre.set_shadow_type(gtk.SHADOW_IN)
		self.label = gtk.Label(config.get('conf', 'lbl_2') + "\n" + config.get('conf', 'lbl_2b'))
		self.label.drag_dest_set(0, [], 0)
		self.label.connect('drag_motion', self.motion_cb)
		self.label.connect('drag_drop', self.drop_cb)
		self.label.connect('drag_data_received', self.got_data_cb)
		cadre.add(self.label)
		vBox.pack_start(cadre, True, True, 10)

		option = gtk.HBox()
		boutonValidation = gtk.CheckButton(config.get('conf', 'button_1'))
		typePartage = gtk.CheckButton(config.get('conf', 'button_2'))
		boutonValidation.connect("clicked", self.partage, boutonValidation)
		option.pack_start(boutonValidation, False, False, 4)
		option.pack_end(typePartage, False, False, 6)
		vBox.pack_start(option, False, False, 4)

		boutonQuitter = gtk.Button("Quitter", stock = gtk.STOCK_QUIT)
		boutonQuitter.connect("clicked", self.quitter, boutonValidation, typePartage, window)
		vBox.pack_end(boutonQuitter, False, False, 2)

		window.add(vBox)
		window.show_all()
		gtk.main()

if __name__ == '__main__':
	Main()

Utiliser des logiciels propriétaires, c'est comme les plats préparés, on est incapable de dire les conservateurs qu'ils contiennent, on dira toujours que c'est bon, mais ça ne remplacera jamais le repas fait maison par sa maman.
]:D #! Crunchbang & Archlinux GNU/Linux User ]:D

Hors ligne

Pied de page des forums