Skip to content

Instantly share code, notes, and snippets.

@lvictorino
Created May 30, 2012 10:37
Show Gist options
  • Save lvictorino/2835406 to your computer and use it in GitHub Desktop.
Save lvictorino/2835406 to your computer and use it in GitHub Desktop.
A collegue asked for some help for her job. She needed a script to apply specific French langage needs (a nbsp and not a classical space after a number) to a large amount of files in several directories.
#!/usr/bin/python
import os
import re
import sys
if sys.version_info < (3,2):
print("Error: Python version 3.2 is required.")
if len(sys.argv) <= 1:
print("Not enough arguments.")
print("usage: nbsp-dealer path_to_directory")
exit()
#PATH definition
p=sys.argv[1]
PATH = p if p[-1] != '/' else p[:-1]
BACKUP_PATH=PATH+"_nbsp_backup"
#REGEXP definition
NUMBER_MATCH = r'([0-9]) '
NUMBER_SUB = "\\1\xa0"
SPEC_MATCH = r' ([^\s\d\w<])'
SPEC_SUB = "\xa0\\1"
LANG_MATCH = r'<\s*html\s+lang="en"\s*>'
LANG_SUB = '<html lang="fr">'
def get_file_path(path, filename):
return path + "/" + filename
def apply_nbsp(path, filename):
filepath = get_file_path(path,filename)
if os.path.exists(filepath) is False:
return
d = open(filepath, "r", -1, "utf-8").read()
open(filepath.replace(PATH,BACKUP_PATH),"w",-1,"utf-8").write(d)
r = d
r = re.sub(NUMBER_MATCH, NUMBER_SUB, r)
r = re.sub(SPEC_MATCH, SPEC_SUB, r)
r = re.sub(LANG_MATCH, LANG_SUB, r)
os.remove(filepath)
open(filepath, "w", -1, "utf-8").write(r)
# open(get_file_path("./results", filename), "w", -1, "utf-8").write(r)
return 1 if r != d else 0
def search_for_files(path):
if os.path.exists(path) is False:
return 0
change_counter = 0
for f in os.listdir(path):
filepath = get_file_path(path, f)
if os.path.isdir(filepath):
backup_dir_path = filepath.replace(PATH,BACKUP_PATH)
if os.path.exists(backup_dir_path) is False:
os.mkdir(backup_dir_path, 0o755)
change_counter += search_for_files(filepath)
else:
change_counter += apply_nbsp(path, f)
return change_counter
if os.path.isdir(PATH) is False:
print("Error: Path parameter has to be a directory (don't play with me, I am no toy!).")
exit()
if os.path.exists(BACKUP_PATH) is False:
os.mkdir(BACKUP_PATH, 0o755)
count = search_for_files(PATH)
if count > 0:
print("Lucky you! Changes are now applied and world has not been destroyed.")
print(str(count)+" file"+("s have" if count > 1 else " has")+" been modified.")
else:
print("Do you think I work for free?!")
print("Nothing has change.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment