-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompressor.py
More file actions
649 lines (522 loc) · 22.8 KB
/
compressor.py
File metadata and controls
649 lines (522 loc) · 22.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
import re
import json
import gzip
import base64
import hashlib
from pathlib import Path
from typing import Dict, List, Any, Optional
from dataclasses import dataclass, asdict
from collections import defaultdict
@dataclass
class CodeBlock:
type: str
name: str
purpose: str
key_logic: List[str]
dependencies: List[str]
class SemanticCompressor:
def __init__(self):
self.js_patterns = {
'function': r'(?:async\s+)?function\s+(\w+)\s*\([^)]*\)\s*{',
'arrow_func': r'const\s+(\w+)\s*=\s*(?:async\s*)?\([^)]*\)\s*=>',
'event_listener': r'(\w+)\.addEventListener\([\'"](\w+)[\'"],',
'api_call': r'fetch\([\'"]([^\'"]+)[\'"]',
'dom_query': r'document\.(?:getElementById|querySelector(?:All)?)\([\'"]([^\'"]+)[\'"]'
}
self.css_patterns = {
'class': r'\.([a-zA-Z0-9_-]+)\s*{',
'id': r'#([a-zA-Z0-9_-]+)\s*{',
'color': r'(#[0-9a-fA-F]{3,6}|rgba?\([^)]+\))',
'size': r'(\d+(?:\.\d+)?(?:px|rem|em|%))'
}
self.py_patterns = {
'class': r'class\s+(\w+)(?:\([^)]*\))?:',
'function': r'def\s+(\w+)\s*\([^)]*\):',
'decorator': r'@(\w+)',
'import': r'(?:from\s+[\w.]+\s+)?import\s+([\w,\s]+)',
'route': r'@app\.route\([\'"]([^\'"]+)[\'"]'
}
def compress_javascript(self, content: str, filename: str) -> Dict:
functions = {}
for match in re.finditer(self.js_patterns['function'], content):
func_name = match.group(1)
func_start = match.start()
func_body = self._extract_block(content, func_start)
functions[func_name] = {
'type': 'async' if 'async' in match.group(0) else 'sync',
'purpose': self._extract_purpose(func_body),
'api_calls': re.findall(self.js_patterns['api_call'], func_body),
'dom_interactions': re.findall(self.js_patterns['dom_query'], func_body),
'flow': self._extract_flow(func_body),
'lines': len(func_body.split('\n'))
}
for match in re.finditer(self.js_patterns['arrow_func'], content):
func_name = match.group(1)
functions[func_name] = {
'type': 'arrow',
'purpose': 'Arrow function',
'lines': 1
}
event_handlers = []
for match in re.finditer(self.js_patterns['event_listener'], content):
event_handlers.append(f"{match.group(1)}→{match.group(2)}")
return {
'file': filename,
'type': 'javascript',
'functions': functions,
'event_handlers': event_handlers,
'globals': self._extract_globals(content),
'complexity_score': len(functions),
'total_lines': len(content.split('\n'))
}
def compress_css(self, content: str, filename: str) -> Dict:
colors = list(set(re.findall(self.css_patterns['color'], content)))[:15]
classes = {}
for match in re.finditer(r'\.([a-zA-Z0-9_-]+)\s*{([^}]+)}', content):
class_name = match.group(1)
properties = match.group(2)
key_props = self._extract_key_properties(properties)
if key_props:
classes[class_name] = key_props
media_queries = re.findall(r'@media\s*([^{]+)', content)
return {
'file': filename,
'type': 'css',
'design_tokens': {
'colors': colors,
'spacing': self._extract_spacing(content),
'typography': self._extract_typography(content)
},
'components': classes,
'media_queries': media_queries[:5],
'total_lines': len(content.split('\n'))
}
def compress_python(self, content: str, filename: str) -> Dict:
classes = {}
for match in re.finditer(self.py_patterns['class'], content):
class_name = match.group(1)
class_start = match.start()
class_body = self._extract_block(content, class_start, indent_based=True)
methods = {}
for method_match in re.finditer(r'def\s+(\w+)\s*\([^)]*\):', class_body):
method_name = method_match.group(1)
method_body = self._extract_block(class_body, method_match.start(), indent_based=True)
methods[method_name] = {
'purpose': self._extract_docstring(method_body),
'logic': self._extract_logic_summary(method_body),
'lines': len(method_body.split('\n'))
}
classes[class_name] = {
'methods': methods,
'attributes': self._extract_attributes(class_body)
}
routes = {}
for match in re.finditer(self.py_patterns['route'], content):
route_path = match.group(1)
route_start = match.start()
route_func = self._extract_block(content, route_start, indent_based=True)
routes[route_path] = {
'method': self._extract_http_method(route_func),
'flow': self._extract_flow(route_func),
'returns': self._extract_return_type(route_func)
}
return {
'file': filename,
'type': 'python',
'classes': classes,
'routes': routes,
'imports': self._extract_imports(content),
'complexity_score': len(classes) + len(routes),
'total_lines': len(content.split('\n'))
}
def compress_html(self, content: str, filename: str) -> Dict:
tags = re.findall(r'<(\w+)', content)
tag_counts = defaultdict(int)
for tag in tags:
tag_counts[tag] += 1
scripts = re.findall(r'<script[^>]*src=[\'"]([^\'"]+)[\'"]', content)
styles = re.findall(r'<link[^>]*href=[\'"]([^\'"]+)[\'"]', content)
return {
'file': filename,
'type': 'html',
'structure': dict(tag_counts),
'external_scripts': scripts,
'external_styles': styles,
'total_lines': len(content.split('\n'))
}
def compress_generic(self, content: str, filename: str, language: str) -> Dict:
return {
'file': filename,
'type': language,
'total_lines': len(content.split('\n')),
'size': len(content),
'summary': 'Generic text file'
}
def _extract_block(self, content: str, start: int, indent_based: bool = False) -> str:
if indent_based:
lines = content[start:].split('\n')
if not lines:
return ''
base_indent = len(lines[0]) - len(lines[0].lstrip())
block_lines = [lines[0]]
for line in lines[1:]:
if line.strip() == '':
block_lines.append(line)
continue
current_indent = len(line) - len(line.lstrip())
if current_indent <= base_indent and line.strip():
break
block_lines.append(line)
return '\n'.join(block_lines)
else:
brace_count = 0
in_block = False
block_end = start
for i, char in enumerate(content[start:], start):
if char == '{':
brace_count += 1
in_block = True
elif char == '}':
brace_count -= 1
if brace_count == 0 and in_block:
block_end = i + 1
break
return content[start:block_end]
def _extract_purpose(self, code: str) -> str:
comment_match = re.search(r'/\*\*?\s*([^\n*]+)', code)
if comment_match:
return comment_match.group(1).strip()[:100]
single_comment = re.search(r'//\s*(.+)', code)
if single_comment:
return single_comment.group(1).strip()[:100]
return "Processing logic"
def _extract_flow(self, code: str) -> str:
steps = []
if re.search(r'\bif\b', code):
steps.append('conditional')
if re.search(r'\b(fetch|await)\b', code):
steps.append('async_call')
if re.search(r'\b(forEach|map|filter|reduce)\b', code):
steps.append('iteration')
if re.search(r'\breturn\b', code):
steps.append('return')
if re.search(r'\btry\b', code):
steps.append('error_handling')
return ' → '.join(steps) if steps else 'simple_execution'
def _extract_globals(self, content: str) -> List[str]:
globals_section = content.split('function')[0] if 'function' in content else content[:1000]
let_vars = re.findall(r'\blet\s+(\w+)', globals_section)
const_vars = re.findall(r'\bconst\s+(\w+)', globals_section)
return list(set(let_vars + const_vars))[:15]
def _extract_key_properties(self, properties: str) -> Dict:
key_props = {}
display_match = re.search(r'display:\s*([^;]+)', properties)
if display_match:
key_props['display'] = display_match.group(1).strip()
bg_match = re.search(r'background[^:]*:\s*([^;]+)', properties)
if bg_match:
key_props['background'] = bg_match.group(1).strip()[:50]
if 'padding' in properties or 'margin' in properties:
key_props['spacing'] = 'custom'
return key_props if len(key_props) > 0 else None
def _extract_spacing(self, content: str) -> str:
rem_values = re.findall(r'(\d+(?:\.\d+)?rem)', content)
if rem_values:
unique_values = sorted(set(rem_values))
return f"rem-based ({unique_values[0]} to {unique_values[-1]})"
return "pixel-based"
def _extract_typography(self, content: str) -> str:
font_match = re.search(r'font-family:\s*([^;]+)', content)
if font_match:
return font_match.group(1).strip()[:60]
return "default-font"
def _extract_docstring(self, code: str) -> str:
docstring_match = re.search(r'"""([^"]+)"""', code)
if docstring_match:
return docstring_match.group(1).strip()[:150]
comment_match = re.search(r'#\s*(.+)', code)
if comment_match:
return comment_match.group(1).strip()[:150]
return ""
def _extract_logic_summary(self, code: str) -> str:
keywords = {
'for': 'loop',
'while': 'loop',
'if': 'conditional',
'try': 'error_handling',
'return': 'return',
'yield': 'generator',
'async': 'async',
'await': 'async'
}
found = []
for kw, desc in keywords.items():
if re.search(rf'\b{kw}\b', code):
if desc not in found:
found.append(desc)
return ' + '.join(found[:4]) if found else "simple_processing"
def _extract_attributes(self, code: str) -> List[str]:
self_attrs = re.findall(r'self\.(\w+)\s*=', code)
return list(set(self_attrs))[:10]
def _extract_http_method(self, code: str) -> str:
if "methods=['POST']" in code or "method: 'POST'" in code:
return 'POST'
elif "methods=['GET']" in code:
return 'GET'
elif "methods=['PUT']" in code:
return 'PUT'
elif "methods=['DELETE']" in code:
return 'DELETE'
return 'GET'
def _extract_return_type(self, code: str) -> str:
if 'jsonify' in code:
return 'JSON'
elif 'send_file' in code:
return 'File'
elif 'render_template' in code:
return 'HTML'
elif 'redirect' in code:
return 'Redirect'
return 'Unknown'
def _extract_imports(self, content: str) -> List[str]:
imports = []
for match in re.finditer(self.py_patterns['import'], content):
imports.extend([imp.strip() for imp in match.group(1).split(',')])
return imports[:15]
def compress_markdown(self, md_content: str) -> Dict:
files_match = re.split(r'\n## (.+?)\n', md_content)
if len(files_match) < 2:
return {
'error': 'Invalid markdown format',
'metadata': {
'original_size': len(md_content),
'compressed_size': 0,
'compression_ratio': 0
}
}
header = files_match[0]
files = files_match[1:]
compressed_files = []
for i in range(0, len(files), 2):
if i + 1 >= len(files):
break
filename = files[i].strip()
content = files[i + 1]
code_match = re.search(r'```(\w+)\n(.*?)\n```', content, re.DOTALL)
if not code_match:
continue
language = code_match.group(1).lower()
code = code_match.group(2)
if language in ['javascript', 'js', 'jsx']:
compressed = self.compress_javascript(code, filename)
elif language == 'css':
compressed = self.compress_css(code, filename)
elif language == 'python':
compressed = self.compress_python(code, filename)
elif language == 'html':
compressed = self.compress_html(code, filename)
else:
compressed = self.compress_generic(code, filename, language)
compressed_files.append(compressed)
compressed_json = json.dumps(compressed_files, ensure_ascii=False, indent=2)
return {
'metadata': {
'original_size': len(md_content),
'compressed_size': len(compressed_json),
'compression_ratio': round(len(compressed_json) / len(md_content) * 100, 2),
'files_count': len(compressed_files),
'compression_type': 'semantic_lossy'
},
'files': compressed_files
}
class LosslessCompressor:
def compress_markdown(self, md_content: str) -> Dict:
compressed_bytes = gzip.compress(md_content.encode('utf-8'), compresslevel=9)
base64_encoded = base64.b64encode(compressed_bytes).decode('ascii')
file_hash = hashlib.sha256(md_content.encode()).hexdigest()[:16]
return {
'metadata': {
'original_size': len(md_content),
'compressed_size': len(base64_encoded),
'compression_ratio': round(len(base64_encoded) / len(md_content) * 100, 2),
'compression_type': 'gzip_lossless',
'hash': file_hash
},
'data': base64_encoded,
'encoding': 'gzip+base64'
}
def decompress_markdown(self, compressed_data: Dict) -> str:
base64_decoded = base64.b64decode(compressed_data['data'])
original_bytes = gzip.decompress(base64_decoded)
return original_bytes.decode('utf-8')
class HybridCompressor:
def __init__(self):
self.semantic_compressor = SemanticCompressor()
self.lossless_compressor = LosslessCompressor()
def compress_with_reference(self, md_content: str, storage_dir: Path) -> Dict:
structure = self.semantic_compressor.compress_markdown(md_content)
file_hash = hashlib.sha256(md_content.encode()).hexdigest()[:16]
storage_path = storage_dir / f"original_{file_hash}.md"
# Ensure storage directory exists
storage_dir.mkdir(exist_ok=True, parents=True)
with open(storage_path, 'w', encoding='utf-8') as f:
f.write(md_content)
# Calculate compression ratio
structure_size = len(json.dumps(structure))
original_size = len(md_content)
compression_ratio = round(structure_size / original_size * 100, 2) if original_size > 0 else 0
return {
'structure': structure,
'original_reference': {
'hash': file_hash,
'path': str(storage_path),
'size': len(md_content),
'filename': storage_path.name
},
'metadata': {
'compression_type': 'hybrid',
'structure_size': structure_size,
'original_size': original_size,
'compressed_size': structure_size,
'compression_ratio': compression_ratio
},
# Store the original content directly in the compressed data
'original_content': md_content
}
def get_original_content(self, file_reference: Dict, compressed_data: Dict = None) -> Optional[str]:
# First try to get from file system
storage_path = Path(file_reference['path'])
if storage_path.exists():
with open(storage_path, 'r', encoding='utf-8') as f:
return f.read()
# If file doesn't exist, try to get from compressed data
if compressed_data and 'original_content' in compressed_data:
return compressed_data['original_content']
return None
def get_file_content(self, file_reference: Dict, file_path: str, compressed_data: Dict = None) -> Optional[str]:
full_content = self.get_original_content(file_reference, compressed_data)
if not full_content:
return None
pattern = rf'\n## {re.escape(file_path)}\n.*?```\w+\n(.*?)\n```'
match = re.search(pattern, full_content, re.DOTALL)
return None
# =====================================================
# Smart Compression Support (New Feature)
# =====================================================
try:
from smart_compressor import SmartCompressor, SummaryLevel
SMART_COMPRESSOR_AVAILABLE = True
except ImportError:
SMART_COMPRESSOR_AVAILABLE = False
# Dummy classes for type hints when not available
SmartCompressor = None # type: ignore
class SummaryLevel: # type: ignore
MINIMAL = 'minimal'
COMPACT = 'compact'
DETAILED = 'detailed'
FULL = 'full'
class CompressionFactory:
"""
압축기 생성 팩토리
Usage:
factory = CompressionFactory()
compressor = factory.create('smart', level='compact')
result = compressor.compress_markdown(content)
"""
_instances = {} # 싱글톤 캐시
@classmethod
def create(
cls,
compression_type: str,
**kwargs
):
"""
압축기 인스턴스 생성
Args:
compression_type: 압축 타입
- 'none': 압축 없음
- 'semantic': 기존 시맨틱 압축
- 'lossless': gzip 압축
- 'hybrid': 하이브리드 압축
- 'signatures': 시그니처만 (5-10%)
- 'smart': 지능형 압축 (15-25%) ← 추천
- 'descriptive': 설명형 압축 (30-40%)
**kwargs: 추가 설정
"""
compression_type = compression_type.lower()
# 캐시 키 생성
cache_key = (compression_type, tuple(sorted(kwargs.items())))
if cache_key in cls._instances:
return cls._instances[cache_key]
# 기존 압축기
if compression_type == 'none':
return None # 압축 없음
elif compression_type == 'semantic':
compressor = SemanticCompressor()
elif compression_type == 'lossless':
compressor = LosslessCompressor()
elif compression_type == 'hybrid':
compressor = HybridCompressor()
# 새로운 Smart 압축기
elif compression_type in ('signatures', 'smart', 'descriptive'):
if not SMART_COMPRESSOR_AVAILABLE:
raise ImportError(
"SmartCompressor not available. "
"Check smart_compressor module installation."
)
# 레벨 매핑
level_map = {
'signatures': 'minimal',
'smart': 'compact',
'descriptive': 'detailed'
}
# kwargs에서 level 오버라이드
level = kwargs.get('level', level_map.get(compression_type, 'compact'))
# SummaryLevel 변환
level_enum = {
'minimal': SummaryLevel.MINIMAL,
'compact': SummaryLevel.COMPACT,
'detailed': SummaryLevel.DETAILED,
'full': SummaryLevel.FULL
}.get(level, SummaryLevel.COMPACT)
compressor = SmartCompressor(level=level_enum)
else:
raise ValueError(f"Unknown compression type: {compression_type}")
cls._instances[cache_key] = compressor
return compressor
@classmethod
def get_supported_types(cls) -> list:
"""지원하는 압축 타입 목록"""
types = ['none', 'semantic', 'lossless', 'hybrid']
if SMART_COMPRESSOR_AVAILABLE:
types.extend(['signatures', 'smart', 'descriptive'])
return types
def compress_markdown_unified(
md_content: str,
compression_type: str = 'none',
**kwargs
) -> tuple:
"""
통합 Markdown 압축 함수
Args:
md_content: Markdown 내용
compression_type: 압축 타입
**kwargs: 추가 설정
Returns:
(compressed_content, metadata)
"""
if compression_type == 'none':
return md_content, None
try:
compressor = CompressionFactory.create(compression_type, **kwargs)
if compression_type in ('signatures', 'smart', 'descriptive'):
# SmartCompressor 사용
result = compressor.compress_markdown(md_content)
return json.dumps(result, ensure_ascii=False, indent=2), result
else:
# 기존 압축기 사용
result = compressor.compress_markdown(md_content)
return json.dumps(result, ensure_ascii=False, indent=2), result
except Exception as e:
# 압축 실패 시 원본 반환
return md_content, {'error': str(e)}