Skip to content

Commit

Permalink
use itertools.zip_longest to simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-monch committed Oct 28, 2023
1 parent 2913989 commit 892243c
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions datalad_next/iter_collections/annexworktree.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import logging
from dataclasses import dataclass
from enum import Enum
from itertools import zip_longest
from pathlib import (
Path,
PurePath,
Expand Down Expand Up @@ -158,31 +159,20 @@ def iter_annexworktree(
common_args = dict(cwd=path, terminate_time=3, kill_time=1)
gaf_store = dict()
glf_store = dict()
read_gaf = True
read_glf = True
with \
run(git_annex_find_cmd, protocol_class=GeneratorAnnexJsonNoStderrProtocol, **common_args) as git_annex_find, \
run(git_ls_files_cmd, protocol_class=GitLsFilesProtocol, **common_args) as git_ls_files, \
annexjson_batchcommand(['git', 'annex', 'examinekey', '--json', '--batch'], **common_args) as examine_key:

while read_gaf or read_glf:
if read_gaf:
try:
gaf_item = next(git_annex_find)
gaf_store[PurePath(PurePosixPath(gaf_item['file']))] = gaf_item
except StopIteration:
read_gaf = False

if read_glf:
try:
glf_item = next(git_ls_files)
glf_store[glf_item.name] = glf_item
except StopIteration:
read_glf = False
for gaf_item, glf_item in zip_longest(git_annex_find, git_ls_files):
if gaf_item:
gaf_store[PurePath(PurePosixPath(gaf_item['file']))] = gaf_item
glf_store[glf_item.name] = glf_item

remove = []
for path, glf_item in glf_store.items():
if path in gaf_store:
gaf_item = gaf_store.get(path)
if gaf_item:
remove.append(path)
key_properties = examine_key(gaf_item['key'].encode() + b'\n')
yield AnnexWorktreeItem(
Expand All @@ -197,7 +187,8 @@ def iter_annexworktree(
del glf_store[path]
del gaf_store[path]

# Yield non-annex files
# Remaining git ls-files results are al unannexed, yield them
assert len(gaf_store) == 0
for path, glf_item in glf_store.items():
yield AnnexWorktreeItem(
name=glf_item.name,
Expand Down

0 comments on commit 892243c

Please sign in to comment.