Python Forum

Full Version: How to access variables from dirsync module
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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.
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.

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)