Python Forum
How to access variables from dirsync module
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to access variables from dirsync module
#1
I have just completed a new project called Sync It,
it uses the excellent dirsync module.

I achieved all that I wanted to in my little app except
for one glaring omission and that was to report the file
statistics after a sync operation.

The sync module does a good job of this and the report is
sent to the Python shell, but because I am running from an
executable and from the system tray I can't seem to access
these variables.

I thought it would just be a case of module_name.variable,
for example:
dirsync._numdelfiles
but that doesn't work.


An alternative solution might be mentioned in the docs of dirsync

quote:
"Custom logger
From python, you may not want to have the output sent to stdout.
To do so, you can simply pass your custom logger via the
logger keyword argument of the sync function:

sync(sourcedir, targetdir, action, logger=my_logger, **options)"
unquote.

When I put logger=my_logger in my sync line I just get a
NameError: name 'my_logger' is not defined error.

I know nothing of logging yet either (obviously) so I'm
off to do some reading in case nobody can save me time
and a headache here with the solution.

My Sync It code is on Github if you want it.

cheers.
Reply
#2
If you want to pass custom logger, you need to create logger object using logging module and pass it when call sync.
That is the way to customize reporting provided by the dirsync author.

As an alternative you can create your own sync function.
Look at dirsync.sync source code:
def sync(sourcedir, targetdir, action, **options):

    copier = Syncer(sourcedir, targetdir, action, **options)
    copier.do_work()

    # print report at the end
    copier.report()

    return set(copier._changed).union(copier._added).union(copier._deleted)
as you can see they instantiate dirsync.Syncer object. when the function execution completes this object is destroyed. That is why you don't have access to properties you want. You can do your own reporting (instead of calling copier.report()). Or you can make your function to return the statistics, instead of set of files affected as the original function does.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thanks for your quick reply Buran. I will have to take my time
going through your help, just wanted to let you know
I am on it and to thank you.


(Apr-02-2020, 04:43 AM)buran Wrote: If you want to pass custom logger, you need to create logger object using logging module and pass it when call sync.
That is the way to customize reporting provided by the dirsync author.

As an alternative you can create your own sync function.
Look at dirsync.sync source code:
def sync(sourcedir, targetdir, action, **options):

    copier = Syncer(sourcedir, targetdir, action, **options)
    copier.do_work()

    # print report at the end
    copier.report()

    return set(copier._changed).union(copier._added).union(copier._deleted)
as you can see they instantiate dirsync.Syncer object. when the function execution completes this object is destroyed. That is why you don't have access to properties you want. You can do your own reporting (instead of calling copier.report()). Or you can make your function to return the statistics, instead of set of files affected as the original function does.

Reply
#4
I tried sending the log to file and this works for me as I can
read what info I need from there, or worst case just display the output as a text file.

Thanks for pointing me towards a solution.

import logging
logging.basicConfig(filename="sync-it.log", level=logging.DEBUG)
my_log = logging.getLogger('dirsync')

and then in my dirsync line:

sync(Glo.src_fldr, Glo.dest_fldr, 'sync', logger=my_log, purge=True)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  importing variables from module 8376459 1 289 Feb-18-2024, 02:24 PM
Last Post: deanhystad
  Can't access the net using Python's Selenium module ShishirModi 2 2,068 Jul-21-2020, 06:03 AM
Last Post: ShishirModi
  module to store functions/variables and how to call them? mstichler 3 2,426 Jun-03-2020, 06:49 PM
Last Post: mstichler
  help with threading module and passing global variables ricardons 1 7,867 Feb-21-2019, 12:48 PM
Last Post: stullis
  How can classes access each other Functions and Variables at the same time PythonOK 4 3,079 Dec-09-2018, 03:46 AM
Last Post: ichabod801
  f2py recovering variables from Fortran module marius 0 3,465 Dec-02-2016, 03:52 PM
Last Post: marius

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020