Advertisement
steve-shambles-2109

Sync It V0.26

Apr 1st, 2020
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.30 KB | None | 0 0
  1. """
  2. Sync two folders from systray V0.26 April 2020
  3. Windows only.
  4. ------------------------
  5. If you use this program, it's at your own risk.
  6. -------------------------
  7. pip3 install dirsync
  8. pip3 install infi.systray
  9. pip3 install Pillow
  10.  
  11. sync-it.ico in same dir as this code is run from.
  12. if not found the default system icon will be used.
  13. """
  14. import os
  15. from time import sleep
  16. from tkinter import filedialog, Label, messagebox, Tk, Toplevel
  17. import webbrowser as web
  18.  
  19. from dirsync import sync
  20. from infi.systray import SysTrayIcon as stray
  21. from PIL import ImageGrab # Hidden import, required only for pyinstaller exe.
  22.  
  23. root = Tk()
  24. root.withdraw()
  25.  
  26. class Glo:
  27.     """Global store, this makes these vars global,e.g Glo.var."""
  28.     custom_mbox = ''
  29.     src_fldr = ''
  30.     dest_fldr = ''
  31.     limit_src = ''
  32.     limit_dest = ''
  33.  
  34. def settings(stray):
  35.     """Read or create settings files."""
  36.     if os.path.exists('sync-src.txt'):
  37.         with open('sync-src.txt', 'r') as contents:
  38.             Glo.src_fldr = contents.read()
  39.     else:
  40.         with open('sync-src.txt', 'w') as contents:
  41.             Glo.src_fldr = ''
  42.  
  43.     if os.path.exists('sync-dest.txt'):
  44.         with open('sync-dest.txt', 'r') as contents:
  45.             Glo.dest_fldr = contents.read()
  46.     else:
  47.         with open('sync-dest.txt', 'w') as contents:
  48.             Glo.dest_fldr = ''
  49.  
  50. def trun_cate():
  51.     """Truncate folder name strings to max 50 chars in case crazy length used."""
  52.     Glo.limit_src = Glo.src_fldr[:50]
  53.     Glo.limit_dest = Glo.dest_fldr[:50]
  54.  
  55. def msg_box(stray):
  56.     """Custom non blocking message box."""
  57.     Glo.custom_mbox = Toplevel(root)
  58.     Glo.custom_mbox.title('Sync It V0.26. ')
  59.     Glo.custom_mbox.attributes('-topmost', 1) # Bring custom window to front.
  60.  
  61.     trun_cate()
  62.     cstm_mb_label = Label(Glo.custom_mbox,
  63.                           text='   By Steve Shambles. April 2020.   \n\n'
  64.                           'Syncing folders:\n'
  65.                           '\n'+str(Glo.limit_src)+''
  66.                           '\n'+str(Glo.limit_dest)+'\n'
  67.                           '\n\nplease wait...')
  68.     cstm_mb_label.grid()
  69.  
  70.     # Centre custom box on screen.
  71.     # From code snippets vol.4, snippet 20.
  72.     # https://stevepython.wordpress.com/2018/09/07/python-code-snippets-4/
  73.     WINDOWWIDTH = Glo.custom_mbox.winfo_reqwidth()
  74.     WINDOWHEIGHT = Glo.custom_mbox.winfo_reqheight()
  75.     POSITIONRIGHT = int(Glo.custom_mbox.winfo_screenwidth()/2 - WINDOWWIDTH/2)
  76.     POSITIONDOWN = int(Glo.custom_mbox.winfo_screenheight()/2 - WINDOWHEIGHT/2)
  77.     # Positions the window in the center of the page.
  78.     Glo.custom_mbox.geometry("+{}+{}".format(POSITIONRIGHT, POSITIONDOWN))
  79.  
  80.     sleep(3) # Gives a chance to read text in case it's a very quick sync.
  81.  
  82. def set_src_fldr(stray):
  83.     """Change source folder location, which is then written to a text file."""
  84.     get_new_sf = filedialog.askdirectory()
  85.  
  86.     if get_new_sf == '':
  87.         return
  88.  
  89.     with open('sync-src.txt', 'w') as contents:
  90.         contents.write(str(get_new_sf))
  91.         Glo.src_fldr = get_new_sf
  92.  
  93. def set_dest_fldr(stray):
  94.     """Change destination folder location, which is then written to text file."""
  95.     get_new_df = filedialog.askdirectory()
  96.     if get_new_df == '':
  97.         return
  98.  
  99.     with open('sync-dest.txt', 'w') as contents:
  100.         contents.write(str(get_new_df))
  101.         Glo.dest_fldr = get_new_df
  102.  
  103. def open_src_folder(stray):
  104.     """File browser to view contents of source folder."""
  105.     if Glo.src_fldr == '':
  106.         return
  107.     web.open(Glo.src_fldr)
  108.  
  109. def open_dest_folder(stray):
  110.     """File browser to view contents of destination."""
  111.     if Glo.dest_fldr == '':
  112.         return
  113.     web.open(Glo.dest_fldr)
  114.  
  115. def exit_prg(stray):
  116.     """This is fake callback required by systray."""
  117.     pass
  118.  
  119. def sync_help(stray):
  120.     """Help pop up message."""
  121.     messagebox.showinfo('Help',
  122.                         'Sync It V0.26\n\n'
  123.                         'Sync It allows any two pre-selected folders\n'
  124.                         'to be kept in sync manually.\n\n'
  125.                         'First choose the two folders that you would like to\n'
  126.                         'synchronize by right-clicking on the Sync It\n'
  127.                         'system tray icon and using the Set src folder and\n'
  128.                         'Set dest folder menu options in the settings menu.\n'
  129.                         '\nThese settings will be saved so you only need to do\n'
  130.                         'this once, unless you decide to change folders later.\n\n'
  131.                         'In the Options menu you can open both these folders to\n'
  132.                         'check they are correct by selecting Open src folder\n'
  133.                         'and Open dest folder.\n\n'
  134.                         'To perform a sync either select Sync It from the menu\n'
  135.                         'or you can just double click the Sync It systray icon.\n\n'
  136.                         )
  137.  
  138. def about_syncit(stray):
  139.     """About program pop up."""
  140.     messagebox.showinfo('Info',
  141.                         'Sync It V0.26\n\n'
  142.                         'Freeware by Steve Shambles'
  143.                         ' April 2020.\n\n'
  144.                         'Sync It allows any two pre-selected folders\n'
  145.                         'to be kept in sync manually.\n\n'
  146.                         'By using this software you are agreeing\n'
  147.                         'that you use it at your own risk.')
  148.  
  149. def visit_blog(stray):
  150.     """Open webbrowser and go to my Python blog site."""
  151.     web.open('https://stevepython.wordpress.com/')
  152.  
  153. def sync_it(stray):
  154.     """Perform syncing of the two folders scr_fldr and dest_fldr."""
  155.     if Glo.src_fldr == '' or Glo.dest_fldr == '':
  156.         messagebox.showinfo('Info',
  157.                             'please set source and destination\n'
  158.                             'folders from the systray menu first.')
  159.         return
  160.  
  161.     trun_cate()
  162.  
  163.     ask_yn = messagebox.askyesno('Question',
  164.                                  'Sync these two folders?'
  165.                                  '\n\n'+str(Glo.limit_src)+''
  166.                                  '\n'+str(Glo.limit_dest)+'\n\n')
  167.     if ask_yn is False:
  168.         return
  169.  
  170.     msg_box(stray)
  171.  
  172.     sync(Glo.src_fldr, Glo.dest_fldr, 'sync', purge=True)
  173.     Glo.custom_mbox.destroy()
  174.  
  175.     messagebox.showinfo('Info',
  176.                         'Sync It V0.26\n\n'
  177.                         'Sync completed.')
  178.  
  179.  
  180. def main(stray):
  181.     """Main program control using infi.systray module."""
  182.     hover_text = 'Sync It V0.26'
  183.  
  184.     menu_options = (('Sync it', None, sync_it),
  185.                     ('Options', None, (('Help', None, sync_help),
  186.                                        ('About', None, about_syncit),
  187.                                        ('Open src folder', None, open_src_folder),
  188.                                        ('Open dest folder', None, open_dest_folder),
  189.                                        ('Visit my blog', None, visit_blog),)),
  190.  
  191.                     ('Settings', None, (('Set source folder', None, set_src_fldr),
  192.                                         ('Set destination folder', None, set_dest_fldr))))
  193.  
  194.     stray = stray('sync-it.ico',
  195.                   hover_text, menu_options, on_quit=exit_prg,
  196.                   default_menu_index=0)
  197.     stray.start()
  198.  
  199. settings(stray)
  200. main(stray)
  201.  
  202. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement