-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathreplace_line.py
More file actions
60 lines (48 loc) · 1.59 KB
/
replace_line.py
File metadata and controls
60 lines (48 loc) · 1.59 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from os.path import abspath, dirname, basename, join, exists
from os import walk
FROM_STRING = """
white_texture.jpg
"""
TO_STRING = """
furley_bg.png
"""
def replace(from_string, to_string, suffix=(
'py', 'htm', 'txt', 'conf', 'css', 'h', 'template', 'js', 'html', 'rst', 'coffee', 'yaml', 'mako', 'sh', 'wsgi',
'scss', 'less', 'plim', 'sass'
)):
from_string = from_string.strip()
to_string = to_string.strip()
for from_s, to_s in zip(filter(bool, from_string.split('\n')), filter(bool, to_string.split('\n'))):
_replace(from_s.strip(), to_s.strip(), suffix)
def _replace(from_string, to_string, suffix):
from_string = from_string.strip()
to_string = to_string.strip()
file = abspath(__file__)
for dirpath, dirnames, filenames in walk(dirname(file)):
dirbase = basename(dirpath)
if '/.hg/' in dirpath:
continue
if dirbase.startswith('.'):
continue
for filename in filenames:
_suffix = filename.rsplit('.', 1)[-1]
# if suffix not in ('css','html', 'htm', ):
if _suffix not in suffix:
continue
path = join(dirpath, filename)
if path == file:
continue
if not exists(path):
continue
with open(path) as f:
content = f.read()
t = content.replace(from_string, to_string)
if t != content:
with open(path, 'wb') as f:
f.write(t)
replace(
FROM_STRING,
TO_STRING,
)