File size: 1,792 Bytes
2690221
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import re, pathlib

p = pathlib.Path("app/api.py")
s = p.read_text(encoding="utf-8")
lines = s.splitlines()

n = len(lines)
i = 0

# --- Shebang / encoding sətirlərini saxla
header = []
while i < n and (lines[i].startswith("#!") or re.search(r"coding[:=]\s*[-\w.]+", lines[i])):
    header.append(lines[i]); i += 1

# --- Docstring varsa götür (yalnız faylın əvvəlində olanda)
doc_block = []
if i < n and re.match(r'^\s*(?P<q>"""|\'\'\')', lines[i]):
    q = '"""' if lines[i].strip().startswith('"""') else "'''"
    doc_block.append(lines[i]); i += 1
    while i < n:
        doc_block.append(lines[i])
        if q in lines[i]:
            i += 1
            break
        i += 1

# --- Qalan hissədən __future__ importlarını çək
future_re = re.compile(r'^\s*from __future__ import ')
futures, rest = [], []
for ln in lines[i:]:
    if future_re.match(ln):
        futures.append(ln.rstrip())
    else:
        rest.append(ln)

# unikallaşdır
seen = set(); futures = [x for x in futures if not (x in seen or seen.add(x))]

# --- Köhnə storage importlarını təmizlə
rest = [ln for ln in rest if not re.match(r'^\s*from app\.storage import ', ln)]

storage_line = 'from app.storage import DATA_DIR, INDEX_DIR, HISTORY_JSON'

out = []
out += header
if header and (doc_block or futures or storage_line): out.append('')
out += doc_block
if doc_block and (futures or storage_line): out.append('')
out += futures
if futures: out.append(storage_line)
else:
    # futures yoxdursa, storage-ı docstringdən sonra qoyuruq
    if doc_block: out.append(storage_line)
    else: out.append(storage_line)
# importlardan sonra boş sətir
out.append('')

# qalan hissə
out += rest

txt = '\n'.join(out).rstrip() + '\n'
p.write_text(txt, encoding="utf-8")
print("OK: rearranged", p)