Advertisement
Arg0s1080

lanzador-flameshot-v-0-2

Oct 19th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.79 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: UTF-8 -*-
  3.  
  4. ########################################################################
  5. #                      Lanzador Flameshot v.0.2                        #
  6. ########################################################################
  7. #                                                                      #
  8. # Si se ha utilizado el instalador, la aplicación debería aparecer en  #
  9. # el menú de aplicaciones, en caso contrarío, ejecutar mediante el     #
  10. # comando python u otorgando permisos de ejecución y utilizando ./     #
  11. #                                                                      #
  12. # Se puede mirar, copiar o modificar lo que se quiera                  #
  13. #                                                                      #
  14. # Iván Rincón 2017                                                     #
  15. ########################################################################
  16.  
  17. import gettext
  18. import os
  19. import wx
  20. from ConfigParser import ConfigParser
  21.  
  22. # Variables
  23. rutaArchivoConfig = os.getenv("HOME") + "/.config/lanzador-flameshot/Lanzador.cfg"
  24. # Clases
  25. config = ConfigParser()
  26.  
  27.  
  28. class LanzadorFrame(wx.Frame):
  29.     def __init__(self, *args, **kwds):
  30.         kwds["style"] = wx.CLOSE_BOX | wx.MINIMIZE_BOX  # | wx.STAY_ON_TOP
  31.         wx.Frame.__init__(self, *args, **kwds)
  32.         self.labelModoCapura = wx.StaticText(self, wx.ID_ANY, _("Modo de captura"), style=wx.ALIGN_LEFT)
  33.         self.labelArea = wx.StaticText(self, wx.ID_ANY, _(u"\u00c1rea:"))
  34.         self.comboboxArea = wx.ComboBox(self, wx.ID_ANY, choices=[_("Pantalla completa"), _(u"Secci\u00f3n rectangular")
  35.                                                                   ], style=wx.CB_READONLY)
  36.         self.labelDemora = wx.StaticText(self, wx.ID_ANY, _("Demora"))
  37.         self.spinCtrlSegundos = wx.SpinCtrl(self, wx.ID_ANY, "0", min=0, max=300, style=wx.TE_READONLY)
  38.         self.labelSegundos = wx.StaticText(self, wx.ID_ANY, _("segundos"), style=wx.ALIGN_RIGHT)
  39.         self.static_line_2 = wx.StaticLine(self, wx.ID_ANY)
  40.         self.label_5 = wx.StaticText(self, wx.ID_ANY, _("Ruta guardado:"))
  41.         self.textCtrlRutaGuardado = wx.TextCtrl(self, wx.ID_ANY, "")
  42.         self.buttonDirDialog = wx.Button(self, wx.ID_ANY, _("..."))
  43.         self.static_line_3 = wx.StaticLine(self, wx.ID_ANY)
  44.         self.buttonRealizarCaptura = wx.Button(self, wx.ID_ANY, _("Realizar captura"))
  45.         self.buttonPreferencias = wx.Button(self, wx.ID_PREFERENCES, "")
  46.         self.buttonSalir = wx.Button(self, wx.ID_EXIT, "")
  47.  
  48.         self.__set_properties()
  49.         self.__do_layout()
  50.         self.setConfig()
  51.  
  52.         self.Bind(wx.EVT_COMBOBOX, self.OnComboBoxAreaClick, self.comboboxArea)
  53.         self.Bind(wx.EVT_SPINCTRL, self.OnSpinCtrlSegundosClick, self.spinCtrlSegundos)
  54.         self.Bind(wx.EVT_TEXT, self.OnTextCtrlRutaGuardadoClick, self.textCtrlRutaGuardado)
  55.         self.Bind(wx.EVT_BUTTON, self.OnButtonClickDialogClick, self.buttonDirDialog)
  56.         self.Bind(wx.EVT_BUTTON, self.OnButtonRealizarCapturaClick, self.buttonRealizarCaptura)
  57.         self.Bind(wx.EVT_BUTTON, self.OnButtonPreferenciasClick, self.buttonPreferencias)
  58.         self.Bind(wx.EVT_BUTTON, self.OnButtonSalirClick, self.buttonSalir)
  59.         self.Bind(wx.EVT_CLOSE, self.OnClose)
  60.  
  61.     def __set_properties(self):
  62.         self.SetTitle(_("Flameshot"))
  63.         _icon = wx.NullIcon
  64.         #  icon.CopyFromBitmap(wx.Bitmap(os.getenv("HOME") + "/.local/share/icons/flameshot.png", wx.BITMAP_TYPE_ANY))
  65.         _icon.CopyFromBitmap(wx.Bitmap("/usr/local/share/icons/flameshot.png", wx.BITMAP_TYPE_ANY))
  66.         self.SetIcon(_icon)
  67.         self.SetSize((251, 245))
  68.         self.labelModoCapura.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
  69.         self.labelArea.SetMinSize((71, 30))
  70.         self.comboboxArea.SetMinSize((160, 30))
  71.         self.comboboxArea.SetSelection(0)
  72.         self.labelDemora.SetMinSize((71, 30))
  73.         self.spinCtrlSegundos.SetMinSize((50, 28))
  74.         self.labelSegundos.SetMinSize((65, 18))
  75.         self.label_5.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
  76.         self.textCtrlRutaGuardado.SetMinSize((215, 30))
  77.         self.textCtrlRutaGuardado.SetValue(os.getenv("HOME"))
  78.         self.buttonDirDialog.SetMinSize((25, 30))
  79.         self.buttonRealizarCaptura.SetMinSize((242, 35))
  80.         self.buttonPreferencias.SetMinSize((121, 30))
  81.         self.buttonSalir.SetMinSize((121, 30))
  82.  
  83.     def __do_layout(self):
  84.         sizer_1 = wx.BoxSizer(wx.VERTICAL)
  85.         sizer_5 = wx.BoxSizer(wx.HORIZONTAL)
  86.  
  87.         sizer_4 = wx.BoxSizer(wx.HORIZONTAL)
  88.         sizer_3 = wx.BoxSizer(wx.HORIZONTAL)
  89.         sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
  90.         sizer_1.Add(self.labelModoCapura, 0, wx.BOTTOM | wx.TOP, 5)
  91.         sizer_2.Add(self.labelArea, 1, wx.BOTTOM, 5)
  92.         sizer_2.Add((20, 20), 0, 0, 0)
  93.         sizer_2.Add(self.comboboxArea, 0, wx.ALIGN_RIGHT | wx.BOTTOM | wx.RIGHT, 5)
  94.         sizer_1.Add(sizer_2, 1, wx.ALIGN_RIGHT, 0)
  95.         sizer_3.Add(self.labelDemora, 1, 0, 5)
  96.         sizer_3.Add((35, 20), 0, 0, 0)
  97.         sizer_3.Add(self.spinCtrlSegundos, 0, wx.ALIGN_RIGHT, 0)
  98.         sizer_3.Add((20, 20), 0, wx.ALIGN_RIGHT, 0)
  99.         sizer_3.Add(self.labelSegundos, 0, wx.ALIGN_RIGHT | wx.RIGHT, 5)
  100.         sizer_1.Add(sizer_3, 1, wx.ALIGN_RIGHT | wx.SHAPED, 0)
  101.         sizer_1.Add(self.static_line_2, 0, 0, 0)
  102.         sizer_1.Add(self.label_5, 0, wx.BOTTOM | wx.TOP, 5)
  103.         sizer_4.Add(self.textCtrlRutaGuardado, 0, wx.ALIGN_RIGHT, 0)
  104.         sizer_4.Add((10, 20), 0, wx.ALIGN_RIGHT, 0)
  105.         sizer_4.Add(self.buttonDirDialog, 0, wx.ALIGN_RIGHT | wx.RIGHT, 5)
  106.         sizer_1.Add(sizer_4, 1, wx.ALIGN_RIGHT, 0)
  107.         sizer_1.Add(self.static_line_3, 0, wx.EXPAND, 0)
  108.         sizer_1.Add(self.buttonRealizarCaptura, 0, wx.EXPAND | wx.LEFT | wx.RIGHT, 3)
  109.         sizer_5.Add(self.buttonPreferencias, 0, wx.EXPAND, 0)
  110.         sizer_5.Add((2, 10), 0, 0, 0)
  111.         sizer_5.Add(self.buttonSalir, 0, wx.ALIGN_RIGHT | wx.EXPAND, 0)
  112.         sizer_1.Add(sizer_5, 1, wx.ALIGN_CENTER_HORIZONTAL, 0)
  113.         self.SetSizer(sizer_1)
  114.         self.Layout()
  115.  
  116.     def OnClose(self, event):
  117.  
  118.         with open(rutaArchivoConfig, "w") as archivoConfig:
  119.             config.write(archivoConfig)
  120.         self.Destroy()
  121.  
  122.     def OnComboBoxAreaClick(self, event):
  123.  
  124.         indice = self.comboboxArea.GetSelection()
  125.  
  126.         config.read(rutaArchivoConfig)
  127.         config.set("Configuracion", "indiceCombo", indice)
  128.  
  129.         event.Skip()
  130.  
  131.     def OnSpinCtrlSegundosClick(self, event):
  132.  
  133.         retardo = self.spinCtrlSegundos.GetValue()
  134.  
  135.         config.read(rutaArchivoConfig)
  136.         config.set("Configuracion", "retardo", retardo)
  137.  
  138.         event.Skip()
  139.  
  140.     def OnTextCtrlRutaGuardadoClick(self, event):
  141.  
  142.         ruta = self.textCtrlRutaGuardado.GetValue().encode("utf8")
  143.  
  144.         config.read(rutaArchivoConfig)
  145.         config.set("Configuracion", "rutaGuardado", ruta)
  146.  
  147.         event.Skip()
  148.  
  149.     def OnButtonClickDialogClick(self, event):
  150.  
  151.         dlg = wx.DirDialog(None, "Seleccione un directorio", os.getenv("HOME"),
  152.                            wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
  153.         if dlg.ShowModal() == wx.ID_OK:
  154.             self.textCtrlRutaGuardado.SetValue(dlg.GetPath())
  155.             config.read(rutaArchivoConfig)
  156.             config.set("Configuracion", "rutaGuardado", dlg.GetPath())
  157.         event.Skip()
  158.  
  159.     def OnButtonRealizarCapturaClick(self, event):
  160.  
  161.         if self.checkFlameshot() == wx.ID_NO: return
  162.  
  163.         import re
  164.         import commands
  165.  
  166.         ruta = self.textCtrlRutaGuardado.GetValue()
  167.  
  168.         # TODO: Probar de nuevo con and
  169.         if not os.path.exists(ruta):
  170.  
  171.             if bool(re.match(r"(\/[\w^ ]+)+\/?([\w.])+[^.]$", ruta)):
  172.                 os.makedirs(ruta)
  173.  
  174.             else:
  175.                 self.msgDlg(ruta + "\n\nno es un nombre de directorio válido".decode("utf8"))
  176.                 return
  177.  
  178.         salida = commands.getoutput(self.setArgumentos())
  179.  
  180.         if salida != "":
  181.             self.msgDlg(salida.replace("See 'flameshot --help'.", ""))
  182.  
  183.         self.OnClose(event)
  184.  
  185.         event.Skip()
  186.  
  187.     def OnButtonPreferenciasClick(self, event):
  188.  
  189.         os.system("/usr/local/bin/flameshot config")
  190.         event.Skip()
  191.  
  192.     def OnButtonSalirClick(self, event):
  193.  
  194.         self.OnClose(event)
  195.         event.Skip()
  196.  
  197.     def setConfig(self):
  198.  
  199.         if os.path.exists(rutaArchivoConfig):
  200.             config.read(rutaArchivoConfig)
  201.             self.comboboxArea.SetSelection(config.getint("Configuracion", "indiceCombo"))
  202.             self.spinCtrlSegundos.SetValue(config.getint("Configuracion", "retardo"))
  203.             self.textCtrlRutaGuardado.SetValue(config.get("Configuracion", "rutaGuardado"))
  204.  
  205.         else:
  206.  
  207.             # TODO: pasar el testFlameshot() aquí
  208.  
  209.             ruta = rutaArchivoConfig.replace("Lanzador.cfg", "")
  210.  
  211.             if not os.path.exists(ruta):
  212.                 os.makedirs(ruta)
  213.  
  214.             config.add_section("Configuracion")
  215.             config.set("Configuracion", "indiceCombo", 0)
  216.             config.set("Configuracion", "retardo", 0)
  217.             config.set("Configuracion", "rutaGuardado", os.getenv("HOME"))
  218.             config.set("Configuracion", "copiaClipboard", 0)  # Sin implementar (modo full)
  219.  
  220.             with open(rutaArchivoConfig, "wb") as archivoConfig:  # w: sobrescritura: si no existe crea. b: binario
  221.                 config.write(archivoConfig)
  222.  
  223.     def setArgumentos(self):
  224.  
  225.         argumentos = list()
  226.         argumentos.append("/usr/local/bin/flameshot")
  227.         argumentos.append("-d " + str(self.spinCtrlSegundos.GetValue() * 1000))
  228.         argumentos.append("-p " + str(self.textCtrlRutaGuardado.GetValue()))
  229.  
  230.         if self.comboboxArea.GetSelection():  # Se le toma como bool al tener sólo 2 posibilidades
  231.             argumentos.insert(1, "gui")
  232.         else:
  233.             argumentos.insert(1, "full")
  234.  
  235.         return " ".join(argumentos)
  236.  
  237.     def checkFlameshot(self):
  238.  
  239.         if not os.path.exists("/usr/local/bin/flameshot"):
  240.             return self.msgDlg("Parece que Flameshot no está instalado.\n\n¿Desea continuar?".decode("utf8"),
  241.                                wx.YES_NO, wx.ICON_QUESTION)
  242.  
  243.     def msgDlg(self, mensaje, botones=wx.OK, simbolo=wx.ICON_EXCLAMATION):
  244.         d = wx.MessageDialog(self, mensaje, "Flameshot", botones | simbolo)
  245.         res = d.ShowModal()
  246.         d.Destroy()
  247.         return res
  248.  
  249.  
  250. # Final de la clase LanzadorFrame
  251.  
  252. class LanzadorFlameshot(wx.App):
  253.  
  254.     def OnInit(self):
  255.         frame_ppal = LanzadorFrame(None, wx.ID_ANY, "")
  256.         self.SetTopWindow(frame_ppal)
  257.         frame_ppal.Show()
  258.         return True
  259.  
  260.  
  261. # Final de la clase LanzadorFlameshot
  262.  
  263. if __name__ == "__main__":
  264.  
  265.     gettext.install("appLanzador")
  266.  
  267.     appLanzador = LanzadorFlameshot(0)
  268.     appLanzador.MainLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement