-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f52c0cd
Showing
3 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# License | ||
|
||
***space-remover*** - Clean up unnecessary spaces in multiple files | ||
|
||
Copyright © 2018 by Ralf Kilian | ||
|
||
Distributed under the *MIT License*: | ||
|
||
``` | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# *space-remover* | ||
|
||
**Table of contents** | ||
* [Definition](#definition) | ||
* [Details](#details) | ||
* [Usage](#usage) | ||
* [Contact](#contact) | ||
|
||
---- | ||
|
||
## Definition | ||
|
||
Simple *Python* script to remove trailing spaces and tabs from files. | ||
|
||
[Top](#space-remover) | ||
|
||
## Details | ||
|
||
There often are code or config files that contain trailing spaces and tabs at the end of lines as well as in blank lines. This script will remove them without removing any blank lines themselves. | ||
|
||
The code was written on *Linux* and has only been tested on that platform, yet. | ||
|
||
[Top](#space-remover) | ||
|
||
## Usage | ||
|
||
Please backup the data before using this script. | ||
|
||
So, to remove all unnecessary spaces inside all *Python* files inside the directory `/tmp/project` as well as its sub-directories, type: | ||
|
||
```bash | ||
$ find /tmp/project -type f | grep "\.py$" > /tmp/foobar.tmp | ||
$ while read line; do | ||
./space-remover.py "$line" | ||
$ done < /tmp/foobar.tmp | ||
$ rm -f /tmp/foobar.tmp | ||
``` | ||
|
||
[Top](#space-remover) | ||
|
||
## Contact | ||
|
||
Any suggestions, questions, bugs to report or feedback to give? | ||
|
||
You can contact me by sending an email to <dev@urbanware.org>. | ||
|
||
[Top](#space-remover) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#!/usr/bin/env python | ||
|
||
# ============================================================================ | ||
# space-remover - Script to remove trailing spaces and tabs from files | ||
# Copyright (C) 2018 by Ralf Kilian | ||
# Distributed under the MIT License (https://opensource.org/licenses/MIT) | ||
# | ||
# GitHub: https://github.com/urbanware-org/space-remover | ||
# GitLab: https://gitlab.com/urbanware-org/space-remover | ||
# ============================================================================ | ||
|
||
__version__ = "1.1.0" | ||
|
||
import os | ||
import shutil | ||
import sys | ||
import tempfile | ||
|
||
temp = "spacerem_" + str(os.getpid()) + ".tmp" | ||
|
||
file_in = sys.argv[1] | ||
file_out = os.path.join(tempfile.gettempdir(), temp) | ||
|
||
if not os.path.exists(file_in): | ||
print "error: The file '%s' does not exist" % file_in | ||
sys.exit(1) | ||
|
||
if os.path.isfile(file_in): | ||
fh_input = open(file_in, "r") | ||
fh_output = open(file_out, "w") | ||
|
||
for line in fh_input: | ||
# Remove tabs at the end of lines | ||
while "\t\n" in line: | ||
line = line.replace("t\n", "\n") | ||
while "\t\r\n" in line: | ||
line = line.replace("\t\r\n", "\r\n") | ||
|
||
# Remove spaces at the end of lines | ||
while " \n" in line: | ||
line = line.replace(" \n", "\n") | ||
while " \r\n" in line: | ||
line = line.replace(" \r\n", "\r\n") | ||
|
||
# Write output | ||
fh_output.write(line) | ||
|
||
fh_input.close() | ||
fh_output.close() | ||
|
||
shutil.move(file_out, file_in) | ||
else: | ||
print "error: The path '%s' is not a file" % file_in | ||
sys.exit(1) | ||
|
||
# EOF |