CadQuery / CQ-editor

CadQuery GUI editor based on PyQT
Apache License 2.0
723 stars 111 forks source link

set `__file__` variable when running from a file #408

Closed snoyer closed 11 months ago

snoyer commented 11 months ago

Setting the __file__ variable when running from a file would allow to locate files relative to the current script (eg. import(Path(__file__).parent / "foo.bar")).

codecov[bot] commented 11 months ago

Codecov Report

Merging #408 (28eb532) into master (bc05b6b) will increase coverage by 0.05%. The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #408      +/-   ##
==========================================
+ Coverage   88.55%   88.60%   +0.05%     
==========================================
  Files          19       19              
  Lines        1546     1553       +7     
  Branches      187      189       +2     
==========================================
+ Hits         1369     1376       +7     
  Misses        143      143              
  Partials       34       34              
Files Changed Coverage Δ
cq_editor/widgets/debugger.py 81.73% <100.00%> (+0.57%) :arrow_up:

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more

adam-urbanczyk commented 11 months ago

LGTM, is it ready?

BTW: you might be also interested in Add script dir to path option - there is not need to use __file__ explicitly.

snoyer commented 11 months ago

LGTM, is it ready?

it works for me :D but that doesn't mean much, does it? I'll defer to you for the final verdict.

BTW: you might be also interested in Add script dir to path option - there is not need to use __file__ explicitly.

Does that add to the search path for python imports? if so I believe that'd be a different use case...

__file__ would be used for:

  1. compatibility with regular python execution, ie. being able to run the same script from command line and in the editor
  2. locating files (mostly for IO I guess) relative to the current script

my basic use case goes something like:

# import input file sitting "next to" the .py file
drawing = import_dxf(Path(__file__).parent / 'drawing.dxf')

# work it
result = do_cq_stuff(drawing)

if __name__ == '__main__':
    # write to disk if running standalone
    export_stl(result, '/somwhere/somehow/result.stl')
elif __name__ == '__cq_main__':
    # visualize if running in editor
    show_object(result)
adam-urbanczyk commented 11 months ago

Thanks for the contribution @snoyer !