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