Skip to content

Commit d0b9526

Browse files
committed
✨ implement CountableFileIterator
1 parent fe1acd6 commit d0b9526

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from collections.abc import Iterator
2+
import os
3+
# !/usr/bin/env python3
4+
# -*- coding: utf-8 -*-
5+
6+
from code_counter.core.countable.file import CountableFile
7+
from code_counter.conf.config import Config
8+
from collections import deque
9+
10+
11+
class CountableFileIterator(Iterator):
12+
def __init__(self, path):
13+
self._ignore = Config().ignore
14+
self._suffix = Config().suffix
15+
self._file_queue = deque()
16+
self.__search(path)
17+
18+
def __search(self, input_path):
19+
if os.path.isdir(input_path):
20+
files = os.listdir(input_path)
21+
for file in files:
22+
file_path = os.path.join(input_path, file)
23+
if os.path.isdir(file_path):
24+
if file_path in self._ignore:
25+
continue
26+
self.__search(file_path)
27+
else:
28+
suffix = os.path.splitext(file)[1]
29+
if len(suffix) == 0 or suffix[1:] not in self._suffix:
30+
continue
31+
file_path = os.path.join(input_path, file)
32+
self._file_queue.append(CountableFile(file_path))
33+
elif os.path.isfile(input_path):
34+
if (suffix := os.path.splitext(input_path)[1]) and len(suffix) > 0 and suffix[1:] in self._suffix:
35+
self._file_queue.append(CountableFile(input_path))
36+
37+
def __iter__(self):
38+
return self
39+
40+
def __next__(self) -> CountableFile:
41+
if self._file_queue:
42+
fc = self._file_queue.popleft()
43+
return fc
44+
else:
45+
raise StopIteration

0 commit comments

Comments
 (0)