kopipasta 0.28.0__py3-none-any.whl → 0.30.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
kopipasta/file.py ADDED
@@ -0,0 +1,47 @@
1
+ import fnmatch
2
+ import os
3
+ from typing import List, Optional, Tuple
4
+
5
+
6
+ FileTuple = Tuple[str, bool, Optional[List[str]], str]
7
+
8
+
9
+ def read_file_contents(file_path):
10
+ try:
11
+ with open(file_path, 'r') as file:
12
+ return file.read()
13
+ except Exception as e:
14
+ print(f"Error reading {file_path}: {e}")
15
+ return ""
16
+
17
+
18
+ def is_ignored(path, ignore_patterns):
19
+ path = os.path.normpath(path)
20
+ for pattern in ignore_patterns:
21
+ if fnmatch.fnmatch(os.path.basename(path), pattern) or fnmatch.fnmatch(path, pattern):
22
+ return True
23
+ return False
24
+
25
+
26
+ def is_binary(file_path):
27
+ try:
28
+ with open(file_path, 'rb') as file:
29
+ chunk = file.read(1024)
30
+ if b'\0' in chunk: # null bytes indicate binary file
31
+ return True
32
+ if file_path.lower().endswith(('.json', '.csv')):
33
+ return False
34
+ return False
35
+ except IOError:
36
+ return False
37
+
38
+
39
+ def get_human_readable_size(size):
40
+ for unit in ['B', 'KB', 'MB', 'GB', 'TB']:
41
+ if size < 1024.0:
42
+ return f"{size:.2f} {unit}"
43
+ size /= 1024.0
44
+
45
+
46
+ def is_large_file(file_path, threshold=102400): # 100 KB threshold
47
+ return os.path.getsize(file_path) > threshold