Mercurial > p > roundup > code
comparison roundup/init.py @ 8088:1045425c23b2
refactor!: replace os.listdir() with os.scandir()
In many places we did a listdir() then a stat to see if it's a file or
directory. This change removes the need for the stat call. Also for
larger directories, scandir() is an iterator, so less memory use.
There is one remnant of listdir used in an error handler. That
requires a stat on each element in the directory, so there is no
benefit to using scandir() other than a slight memory saving on a
rarely used piece of code.
BREAKING CHANGE:
Python 2 requires installation of scandir pip package after this
commit.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Tue, 16 Jul 2024 01:05:49 -0400 |
| parents | 07ce4e4110f5 |
| children | 586f76eb33e8 |
comparison
equal
deleted
inserted
replaced
| 8087:0cb81ee2e572 | 8088:1045425c23b2 |
|---|---|
| 42 | 42 |
| 43 This was copied from shutil.py in std lib. | 43 This was copied from shutil.py in std lib. |
| 44 """ | 44 """ |
| 45 | 45 |
| 46 # Prevent 'hidden' files (those starting with '.') from being considered. | 46 # Prevent 'hidden' files (those starting with '.') from being considered. |
| 47 names = [f for f in os.listdir(src) if not f.startswith('.')] | 47 names = [f.name for f in os.scandir(src) if not f.name.startswith('.')] |
| 48 try: | 48 try: |
| 49 os.mkdir(dst) | 49 os.mkdir(dst) |
| 50 except OSError as error: | 50 except OSError as error: |
| 51 if error.errno != errno.EEXIST: raise # noqa | 51 if error.errno != errno.EEXIST: raise # noqa |
| 52 for name in names: | 52 for name in names: |
| 109 if not os.path.isfile(config_ini_file): | 109 if not os.path.isfile(config_ini_file): |
| 110 config = CoreConfig(settings=settings) | 110 config = CoreConfig(settings=settings) |
| 111 config.save(config_ini_file) | 111 config.save(config_ini_file) |
| 112 | 112 |
| 113 | 113 |
| 114 def listTemplates(dir): | 114 def listTemplates(directory): |
| 115 ''' List all the Roundup template directories in a given directory. | 115 ''' List all the Roundup template directories in a given directory. |
| 116 | 116 |
| 117 Find all the dirs that contain a TEMPLATE-INFO.txt and parse it. | 117 Find all the dirs that contain a TEMPLATE-INFO.txt and parse it. |
| 118 | 118 |
| 119 Return a list of dicts of info about the templates. | 119 Return a list of dicts of info about the templates. |
| 120 ''' | 120 ''' |
| 121 ret = {} | 121 ret = {} |
| 122 for idir in os.listdir(dir): | 122 for dir_entry in os.scandir(directory): |
| 123 idir = os.path.join(dir, idir) | 123 ti = loadTemplateInfo(dir_entry.path) |
| 124 ti = loadTemplateInfo(idir) | |
| 125 if ti: | 124 if ti: |
| 126 ret[ti['name']] = ti | 125 ret[ti['name']] = ti |
| 127 return ret | 126 return ret |
| 128 | 127 |
| 129 | 128 |
