<div dir="ltr"><div>Hi all,</div><div><br></div><div>I would like to get your opinion about making the code in the python scripts more reusable so they can be imported as modules.</div><div><br></div><div>I'm working on improving gdal_calc.py:</div><div><a href="https://github.com/OSGeo/gdal/pull/3016">https://github.com/OSGeo/gdal/pull/3016</a></div><div>Eventually I would need to import it as a module, since it's not in a package it's not possible without copying it into a package. <br></div><div><br></div><div>From a recent discussion with <a class="gmail_plusreply" id="plusReplyChip-0" href="mailto:even.rouault@spatialys.com" tabindex="-1">@Even Rouault</a> :</div><div><a href="https://github.com/OSGeo/gdal/pull/3016#issuecomment-704563723">https://github.com/OSGeo/gdal/pull/3016#issuecomment-704563723</a><br></div><div><span style="color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji";font-size:14px">The thing is that the scripts in swig/python/scripts are typically installed in /usr/bin so they must be standalone, or use the content of the "osgeo" module where the bindings are installed. If you wanted to use helper methods/scripts, I guess they should go in the "osgeo" module as well.</span> <br></div><div><br></div><div><span style="color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji";font-size:14px">We might still need tiny wrapper scripts in swig/python/scripts</span> for maintaining backwards compatibility.</div><div><br></div><div>So I thought a good approach would be making a main function for each script (as many already do have), For most scripts that was fairly straightforward*:</div><div><a href="https://github.com/OSGeo/gdal/compare/master...talos-gis:python_scripts_organize">https://github.com/OSGeo/gdal/compare/master...talos-gis:python_scripts_organize</a><br></div><div></div><div><br></div><div>* for gdal_retile.py, gdal_merge.py I needed also to replace all the global variables with local variables so it would be easier to reuse these functions (apparently I broke some things in
gdal_retile but I'll fix that).</div><div><br></div><div>Then I made the tiny wrappers automatically using an auxiliary script.</div><div><a href="https://github.com/OSGeo/gdal/compare/master...talos-gis:python_scripts">https://github.com/OSGeo/gdal/compare/master...talos-gis:python_scripts</a><br></div><div>So essentially, each wrapper would be like:</div><div><br></div><div>from osgeo.gdal import deprecation_warn<br>deprecation_warn('gdal_calc')<br>from osgeo.gdal_calc import *<br>if __name__ == '__main__':<br> sys.exit(main(sys.argv))<br></div><div><br></div><div>What do you think?</div><div><br></div><div>Idan</div><div><br></div></div>