mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-09 12:54:17 +00:00
this is much cleaner and might actually get all the coverage with out a bunch of work
This commit is contained in:
parent
845a3d5bdf
commit
d06a515ce1
1 changed files with 14 additions and 86 deletions
|
|
@ -6,95 +6,23 @@ from collections import OrderedDict
|
||||||
|
|
||||||
import simplejson
|
import simplejson
|
||||||
|
|
||||||
class SortableOrderedDict(OrderedDict):
|
|
||||||
"""Performs an in-place sort of the keys if you want."""
|
|
||||||
def sort(*args, **kwds):
|
|
||||||
self = args[0]
|
|
||||||
args = args[1:]
|
|
||||||
if 'key' not in kwds:
|
|
||||||
kwds['key'] = lambda x: x[0]
|
|
||||||
if len(args):
|
|
||||||
raise TypeError('expected no positional arguments got {0}'.format(len(args)))
|
|
||||||
sorted_od = sorted([x for x in self.items()], **kwds)
|
|
||||||
self.clear()
|
|
||||||
self.update(sorted_od)
|
|
||||||
|
|
||||||
class TrackedSod(SortableOrderedDict):
|
|
||||||
"""Tracks instances of the SortableOrderedDict."""
|
|
||||||
_instances = []
|
|
||||||
def __init__(self, *args, **kwds):
|
|
||||||
super(TrackedSod, self).__init__(*args, **kwds)
|
|
||||||
self.__track(self)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def __track(cls, obj):
|
|
||||||
cls._instances.append(obj)
|
|
||||||
|
|
||||||
|
|
||||||
def _get_pretty_format(contents, indent, sort_keys=True, top_keys=[]):
|
def _get_pretty_format(contents, indent, sort_keys=True, top_keys=[]):
|
||||||
class KeyToCmp(object):
|
def pairs_first(pairs):
|
||||||
def __init__(self, obj, *args):
|
before = [pair for pair in pairs if pair[0] in top_keys]
|
||||||
self.obj = obj[0]
|
before = sorted(before, key=lambda x: top_keys.index(x[0]))
|
||||||
def __lt__(self, other):
|
after = [pair for pair in pairs if pair[0] not in top_keys]
|
||||||
if self.obj in top_keys and other.obj in top_keys:
|
|
||||||
return top_keys.index(self.obj) < top_keys.index(other.obj)
|
|
||||||
elif self.obj in top_keys and other.obj not in top_keys:
|
|
||||||
return True
|
|
||||||
elif self.obj not in top_keys and other.obj in top_keys:
|
|
||||||
return False
|
|
||||||
else:
|
|
||||||
return self.obj < other.obj
|
|
||||||
def __gt__(self, other):
|
|
||||||
if self.obj in top_keys and other.obj in top_keys:
|
|
||||||
return top_keys.index(self.obj) > top_keys.index(other.obj)
|
|
||||||
elif self.obj in top_keys and other.obj not in top_keys:
|
|
||||||
return False
|
|
||||||
elif self.obj not in top_keys and other.obj in top_keys:
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
return self.obj > other.obj
|
|
||||||
def __eq__(self, other):
|
|
||||||
if self.obj in top_keys and other.obj in top_keys:
|
|
||||||
return top_keys.index(self.obj) == top_keys.index(other.obj)
|
|
||||||
elif self.obj in top_keys and other.obj not in top_keys:
|
|
||||||
return False
|
|
||||||
elif self.obj not in top_keys and other.obj in top_keys:
|
|
||||||
return False
|
|
||||||
else:
|
|
||||||
return self.obj == other.obj
|
|
||||||
def __le__(self, other):
|
|
||||||
if self.obj in top_keys and other.obj in top_keys:
|
|
||||||
return top_keys.index(self.obj) <= top_keys.index(other.obj)
|
|
||||||
elif self.obj in top_keys and other.obj not in top_keys:
|
|
||||||
return True
|
|
||||||
elif self.obj not in top_keys and other.obj in top_keys:
|
|
||||||
return False
|
|
||||||
else:
|
|
||||||
return self.obj <= other.obj
|
|
||||||
def __ge__(self, other):
|
|
||||||
if self.obj in top_keys and other.obj in top_keys:
|
|
||||||
return top_keys.index(self.obj) >= top_keys.index(other.obj)
|
|
||||||
elif self.obj in top_keys and other.obj not in top_keys:
|
|
||||||
return False
|
|
||||||
elif self.obj not in top_keys and other.obj in top_keys:
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
return self.obj >= other.obj
|
|
||||||
def __ne__(self, other):
|
|
||||||
if self.obj in top_keys and other.obj in top_keys:
|
|
||||||
return top_keys.index(self.obj) != top_keys.index(other.obj)
|
|
||||||
elif self.obj in top_keys and other.obj not in top_keys:
|
|
||||||
return False
|
|
||||||
elif self.obj not in top_keys and other.obj in top_keys:
|
|
||||||
return False
|
|
||||||
else:
|
|
||||||
return self.obj != other.obj
|
|
||||||
py_obj = simplejson.loads(contents, object_pairs_hook=TrackedSod)
|
|
||||||
if sort_keys:
|
if sort_keys:
|
||||||
for tsod in TrackedSod._instances:
|
after = sorted(after, key=lambda x: x[0])
|
||||||
tsod.sort(key=KeyToCmp)
|
return OrderedDict(before + after)
|
||||||
# dumps don't end with a newline
|
return simplejson.dumps(
|
||||||
return simplejson.dumps(py_obj, indent=indent) + "\n"
|
simplejson.loads(
|
||||||
|
contents,
|
||||||
|
object_pairs_hook=pairs_first,
|
||||||
|
),
|
||||||
|
indent=indent
|
||||||
|
) + "\n" # dumps don't end with a newline
|
||||||
|
|
||||||
def _autofix(filename, new_contents):
|
def _autofix(filename, new_contents):
|
||||||
print("Fixing file {0}".format(filename))
|
print("Fixing file {0}".format(filename))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue