diff --git a/thefuck/statistics/__init__.py b/thefuck/statistics/__init__.py new file mode 100644 index 00000000..75bed0cb --- /dev/null +++ b/thefuck/statistics/__init__.py @@ -0,0 +1 @@ +from .statistics import CommandRecords \ No newline at end of file diff --git a/thefuck/statistics/statistics.py b/thefuck/statistics/statistics.py new file mode 100644 index 00000000..d9573428 --- /dev/null +++ b/thefuck/statistics/statistics.py @@ -0,0 +1,46 @@ +import json +import thefuck.const as const + + +class CommandRecords(object): + """Object to interact with 'records.json' data""" + def __init__(self): + """Opens 'records.json' if available""" + try: + with open("records.json", "r") as records_file: + self.statistics = json.load(records.file) + except: + self.statistics = const.DEFAULT_RECORDS_DICT + + + def add_records(self, category, **kwargs): + """Creates new records from arguments + and dumps information to 'records.json' + + :type commands: Iterable[thefuck.types.CorrectedCommand] + :type category: Const string + """ + if category == const.APPLIED_RULES: + for command in kwargs['command_list']: + try: + self.statistics[const.APPLIED_RULES][command.script] +=1 + except KeyError: + self.statistics[const.APPLIED_RULES].update({command.script: 1}) + + elif category == const.SELECTED_RULES: + command = kwargs['command_single'] + try: + self.statistics[const.SELECTED_RULES][command.script] += 1 + except KeyError: + self.statistics[const.SELECTED_RULES].update({command.script: 1}) + + elif category == const.NO_FUCKS_GIVEN: + self.statistics[const.NO_FUCKS_GIVEN] += 1 + + def clear_records(self): + self.statistics = const.DEFAULT_RECORDS_DICT + self.save() + + def save(self): + with open("records.json", "w") as records_file: + json.dump(self.statistics, records_file) \ No newline at end of file diff --git a/thefuck/ui.py b/thefuck/ui.py index 9c05db33..19f1dbe9 100644 --- a/thefuck/ui.py +++ b/thefuck/ui.py @@ -6,6 +6,7 @@ from .exceptions import NoRuleMatched from .system import get_key from .utils import get_alias from . import logs, const +from .statistics import CommandRecords def read_actions(): @@ -67,21 +68,41 @@ def select_command(corrected_commands): :rtype: thefuck.types.CorrectedCommand | None """ + records = CommandRecords() try: selector = CommandSelector(corrected_commands) + records.add_records( + command_list = [command for command in corrected_commands], + category = const.APPLIED_RULES + ) + records.save() except NoRuleMatched: logs.failed('No fucks given' if get_alias() == 'fuck' else 'Nothing found') + records.add_records( + category = const.NO_FUCKS_GIVEN + ) + records.save() return if not settings.require_confirmation: logs.show_corrected_command(selector.value) + records.add_records( + command_single = selector.value, + category = const.SELECTED_RULES + ) + records.save() return selector.value logs.confirm_text(selector.value) for action in read_actions(): if action == const.ACTION_SELECT: + records.add_records( + command_single = selector.value, + category = const.SELECTED_RULES + ) + records.save() sys.stderr.write('\n') return selector.value elif action == const.ACTION_ABORT: @@ -93,3 +114,4 @@ def select_command(corrected_commands): elif action == const.ACTION_NEXT: selector.next() logs.confirm_text(selector.value) + \ No newline at end of file