mavzermete 1
mavzermete
Test5410 1
Test5410
noisiv 1
noisiv
Manwe Work 1
Manwe Work
Reklam vermek için turkmmo@gmail.com

Automat remove define - Grzyb

  • Konuyu başlatan Konuyu başlatan muchomor1337
  • Başlangıç tarihi Başlangıç tarihi
  • Cevaplar Cevaplar 11
  • Görüntüleme Görüntüleme 2K
Merhaba Türk arkadaşlarım, tanımların taranması ve otomatik olarak silinmesi için betiği yakalayın
Hi Turkish friends, capture script to scan and auto-delete definitions

Remove .cpp
Kod:
# Script - Grzyb.ovh
import os

# Localition
folder_path = '/txt/src'

# Code
encoding = 'latin1'

# define to remove in source
definition_to_remove = '#ifdef __ENABLE_OFFLINE_SHOP__'

for root, dirs, files in os.walk(folder_path):
    for file in files:
        if file.endswith('.cpp') or file.endswith('.h'):
            file_path = os.path.join(root, file)
            with open(file_path, 'r', encoding=encoding) as f:
                lines = f.readlines()
            with open(file_path, 'w', encoding=encoding) as f:
                remove_lines = False
                for line in lines:
                    if definition_to_remove in line:
                        remove_lines = True
                    if remove_lines:
                        if '#endif' in line:
                            remove_lines = False
                        continue
                    f.write(line)

Remove if app in python

Kod:
# Script - Grzyb.ovh
import os

# Localition
directory = '/txt/src'

# Code
encoding = 'latin1'

# define to remove in source
target_if = 'if app.ENABLE_OFFLINE_SHOP:'

for filename in os.listdir(directory):
    if filename.endswith('.py'):
        filepath = os.path.join(directory, filename)
        with open(filepath, 'r', encoding=encoding) as f:
            lines = f.readlines()
        with open(filepath, 'w', encoding=encoding) as f:
            in_target_block = False
            target_indent = 0
            for line in lines:
                indent = len(line) - len(line.lstrip())
                if target_if in line:
                    in_target_block = True
                    target_indent = indent
                elif in_target_block and indent > target_indent:
                    continue
                else:
                    in_target_block = False
                    f.write(line)
 

En Çok Reaksiyon Alan Mesajlar

Thanks for sharing, this version of the code is better.

Disclaimer for c++: if your codes using inner ifdef like this;
C++:
#ifdef __ENABLE_OFFLINE_SHOP
[...]
#ifdef __transmutation
    something
#endif
[...]
#endif
it's gonna delete to the first endif, so it'll occours some errors.

For c++;
Python:
import os
directory = 'C:/Users/Reached/Desktop/workspace/txt/test'
encoding = "UTF-8"
definition_to_remove = '#ifdef __ENABLE_OFFLINE_SHOP__'
for file in os.scandir(directory):
    if file.name.endswith(('.cpp', '.h')) and file.is_file():
        file_path = file.path
 
        with open(file_path, 'r', encoding=encoding) as f:
            lines = f.readlines()
        with open(file_path, 'w', encoding=encoding) as f:
            remove_lines = False
            for line in lines:
                if definition_to_remove in line:
                    remove_lines = True
                if remove_lines:
                    if '#endif' in line:
                        remove_lines = False
                    continue
                f.write(line)

For python;
Python:
import os
directory = 'C:/Users/Reached/Desktop/workspace/txt/src'
encoding = "UTF-8"
target_if = 'if app.ENABLE_OFFLINE_SHOP:'
for file in os.scandir(directory):
    if file.name.endswith('.py') and file.is_file():
        file_path = file.path
        with open(file_path, 'r', encoding=encoding) as f:
            lines = f.readlines()
        with open(file_path, 'w', encoding=encoding) as f:
            in_target_block = False
            target_indent = 0
            for line in lines:
                indent = len(line) - len(line.lstrip())
                if target_if in line:
                    in_target_block = True
                    target_indent = indent
                elif in_target_block and indent > target_indent:
                    continue
                else:
                    in_target_block = False
                    f.write(line)

Şu an konuyu görüntüleyenler (Toplam : 0, Üye: 0, Misafir: 0)

Geri
Üst