Skip to content

Commit a087e88

Browse files
committed
Prevent unnecessary writes and the -m option
1 parent 9ac2b0c commit a087e88

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,36 @@ pip install sde
9696

9797
## Notes
9898

99+
### Quoting in JSON
100+
99101
Quoting is avoided for `null`, `true`, `false`, and numeric values.
100102
To ensure that a given value is quoted, use `-s` (or `--string`) option:
101103

102104
```bash
103105
sde -s key null file.json
104106
```
105107

108+
### Force-fail on missing keys
109+
106110
If you must *edit* the file, by ensuring to update only the existing key, use `-e` (`--must-exist`)
107111
option. The program will exit without adding the key which doesn't exist.
108112

109113
```bash
110114
sde -e key val file.json
111115
```
112116

117+
### Force-fail on unchanged file
118+
119+
If the data is unchanged after running `sde` (values already match), you can force
120+
a failure exit code `2` by passing the `-m` option:
121+
122+
```bash
123+
sde -m key sameval file.json
124+
# > exit code 0
125+
sde -m key sameval file.json
126+
# > exit code 2
127+
```
128+
113129
## TODO
114130

115131
### Work with stdin

sde/__about__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '1.1.8'
1+
__version__ = '1.1.9'

sde/sde.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,17 @@
2424

2525
def edit_file(key, value, file, fmt, must_exist=False):
2626
data = DottedDict(read_file(file, fmt))
27-
if must_exist:
28-
try:
29-
# this is the way I found it works for array vals too
30-
# e.g. fruits.0.name
31-
data[key]
32-
except KeyError:
27+
try:
28+
# this is the way I found it works for array vals too
29+
# e.g. fruits.0.name
30+
if data[key] == value and type(data[key]) == type(value):
31+
# prevents writing to the file is value is matching already
32+
return False
33+
except KeyError:
34+
if must_exist:
3335
raise ValueError('{} is not present in {}'.format(key, file))
3436
data[key] = value
35-
write_file(file, fmt, data)
37+
return write_file(file, fmt, data)
3638

3739

3840
def read_file(file, fmt):
@@ -60,6 +62,7 @@ def write_file(file, fmt, data):
6062
fd.write(to())
6163
fd.close()
6264
os.rename(tmp, file)
65+
return True
6366
except Exception:
6467
# We can assume we can remove it, because we successfully created
6568
# it with O_EXCL above.
@@ -103,11 +106,13 @@ def main():
103106
parser.add_argument('file', metavar='<filename>', help='Filename to edit')
104107
parser.add_argument('-e', '--must-exist', dest='must_exist', action='store_true',
105108
help='Throw error and exit if the key does not already exist')
109+
parser.add_argument('-m', '--must-change', dest='must_change', action='store_true',
110+
help='Exit with status code 2 if the values already match and file unchanged')
106111
parser.add_argument('-s', '--string', dest='is_string', action='store_true',
107112
help='Always treat value as a string by quoting it')
108113
parser.add_argument('--version', action='version',
109114
version='%(prog)s {version}'.format(version=__version__))
110-
parser.set_defaults(is_string=False, must_exist=False)
115+
parser.set_defaults(is_string=False, must_exist=False, must_change=False)
111116
args = parser.parse_args()
112117

113118
if args.is_string:
@@ -123,7 +128,9 @@ def main():
123128
sys.exit(1)
124129

125130
try:
126-
edit_file(args.key, val, args.file, fmt, must_exist=args.must_exist)
131+
res = edit_file(args.key, val, args.file, fmt, must_exist=args.must_exist)
132+
if args.must_change and res is False:
133+
sys.exit(2)
127134
except ValueError as e:
128135
print("\033[91mError: \033[0m" + str(e), file=sys.stderr)
129136
sys.exit(1)

0 commit comments

Comments
 (0)