From db3b5f5e5fcbb441fb047335f357504fef50d39f Mon Sep 17 00:00:00 2001 From: Jared Henry Oviatt Date: Wed, 6 May 2015 18:05:43 -0700 Subject: [PATCH 1/2] started learning abilities --- thefuck/custom_fuckups.py | 75 +++++++++++++++++++++++++++++++++++++ thefuck/rules/my_fuckups.py | 15 ++++++++ 2 files changed, 90 insertions(+) create mode 100644 thefuck/custom_fuckups.py create mode 100644 thefuck/rules/my_fuckups.py diff --git a/thefuck/custom_fuckups.py b/thefuck/custom_fuckups.py new file mode 100644 index 00000000..a416f647 --- /dev/null +++ b/thefuck/custom_fuckups.py @@ -0,0 +1,75 @@ + +# handle reading and writing local list of fuckups + +# fuckups are stored in ~/.thefuck/my_fuckups + +# file format: +# fucked up command +# fixed command +# fucked up command +# fixed command +# ... + +# rule is in rules/myfuckups.py + +# TODO default fuckups (could replace some existing rules with these, mostly typo rules), share fuckups +# TODO add command that calls add_fuckup() - something like -$ fuck --ifuckedup # not sure how to do this with all these aliases... +# TODO add suggestion to add a fuckup when 'no fucks given' +# TODO add command that calls remove_fuckup() - something like -$ fuck --remove 'cd..' # same problem as add_fuckup + + +def get_fuckups(): # returns a dictionary of your fuckups from ~/.thefuck/my_fuckups + + FILENAME = '~/.thefuck/my_fuckups' + FILE = open(FILENAME, 'r') + + IS_FUCKUP = True + + fuckups = dict() + + for line in FILE: + if IS_FUCKUP: + key = line + IS_FUCKUP = False + else + cmd = line + IS_FUCKUP = True + fuckups[key] = cmd + + FILE.close() + + return fuckups + +def add_fuckup(fucked, fixed) + FILENAME = '~/.thefuck/my_fuckups' + FILE = open(FILENAME, 'w') + + FILE.write(fucked + '\n') + FILE.write(fixed + '\n') + + FILE.close() + + return + +def remove_fuckup(fuckup) + + FILENAME = '~/.thefuck/my_fuckups' + FILE = open(FILENAME, 'r') + + fuckup_cmd = '' + + lines = FILE.readlines() + + FILE.close + + FILE = open(FILENAME, 'w') + + for number, line in enumerate(lines) + if line != fuckup and line != fuckup_cmd: + FILE.write(line) + elif line == fuckup: + fuckup_cmd = lines[number + 1] + + FILE.close() + + return diff --git a/thefuck/rules/my_fuckups.py b/thefuck/rules/my_fuckups.py new file mode 100644 index 00000000..611ccd5a --- /dev/null +++ b/thefuck/rules/my_fuckups.py @@ -0,0 +1,15 @@ + +import custom_fuckups + +fuckups = custom_fuckups.get_fuckups() + +def match(command, settings): + if command in fuckups: + return True + return False # not found + + +def get_new_command(command, settings): + return fuckups[command] + +# priority = 1000 # Lower first From 3ea8e721d989a910b5fea68cdf33a2d9849a7deb Mon Sep 17 00:00:00 2001 From: Jared Henry Oviatt Date: Wed, 13 May 2015 12:36:12 -0700 Subject: [PATCH 2/2] fuckups rule from branch ifuckedup --- thefuck/conf.py | 39 +++++++++++++++++++++++++++++++------ thefuck/custom_fuckups.py | 29 +++++++++++++-------------- thefuck/rules/my_fuckups.py | 2 +- 3 files changed, 48 insertions(+), 22 deletions(-) diff --git a/thefuck/conf.py b/thefuck/conf.py index 4bc01e70..82a4a2be 100644 --- a/thefuck/conf.py +++ b/thefuck/conf.py @@ -28,12 +28,16 @@ DEFAULT_PRIORITY = 1000 DEFAULT_SETTINGS = {'rules': DEFAULT_RULES, 'wait_command': 3, 'require_confirmation': False, - 'no_colors': False} + 'no_colors': False, + 'priority': {}} + +DEFAULT_FUCKUPS = {'cd..':'cd ..'} ENV_TO_ATTR = {'THEFUCK_RULES': 'rules', 'THEFUCK_WAIT_COMMAND': 'wait_command', 'THEFUCK_REQUIRE_CONFIRMATION': 'require_confirmation', - 'THEFUCK_NO_COLORS': 'no_colors'} + 'THEFUCK_NO_COLORS': 'no_colors', + 'THEFUCK_PRIORITY': 'priority'} SETTINGS_HEADER = u"""# ~/.thefuck/settings.py: The Fuck settings file @@ -66,16 +70,29 @@ def _rules_from_env(val): return val +def _priority_from_env(val): + """Gets priority pairs from env.""" + for part in val.split(':'): + try: + rule, priority = part.split('=') + yield rule, int(priority) + except ValueError: + continue + + def _val_from_env(env, attr): """Transforms env-strings to python.""" val = os.environ[env] if attr == 'rules': - val = _rules_from_env(val) + return _rules_from_env(val) + elif attr == 'priority': + return dict(_priority_from_env(val)) elif attr == 'wait_command': - val = int(val) + return int(val) elif attr in ('require_confirmation', 'no_colors'): - val = val.lower() == 'true' - return val + return val.lower() == 'true' + else: + return val def _settings_from_env(): @@ -115,3 +132,13 @@ def initialize_settings_file(user_dir): settings_file.write(SETTINGS_HEADER) for setting in DEFAULT_SETTINGS.items(): settings_file.write(u'# {} = {}\n'.format(*setting)) + + +def initialize_fuckups_file(user_dir): + fuckups_path = user_dir.joinpath('.my_fuckups') + if not fuckups_path.is_file(): + with fuckups_path.open(mode='w') as fuckups_file: + for key, fix in DEFAULT_FUCKUPS.iteritems(): + fuckups_file.write(key + '\n') + fuckups_file.write(fix + '\n') + return diff --git a/thefuck/custom_fuckups.py b/thefuck/custom_fuckups.py index a416f647..2152ff27 100644 --- a/thefuck/custom_fuckups.py +++ b/thefuck/custom_fuckups.py @@ -1,7 +1,7 @@ # handle reading and writing local list of fuckups -# fuckups are stored in ~/.thefuck/my_fuckups +# fuckups are stored locally in ~/.thefuck/my_fuckups # file format: # fucked up command @@ -12,15 +12,9 @@ # rule is in rules/myfuckups.py -# TODO default fuckups (could replace some existing rules with these, mostly typo rules), share fuckups -# TODO add command that calls add_fuckup() - something like -$ fuck --ifuckedup # not sure how to do this with all these aliases... -# TODO add suggestion to add a fuckup when 'no fucks given' -# TODO add command that calls remove_fuckup() - something like -$ fuck --remove 'cd..' # same problem as add_fuckup - - -def get_fuckups(): # returns a dictionary of your fuckups from ~/.thefuck/my_fuckups +def get_fuckups(): # returns a dictionary of your fuckups from ~/.thefuck/.my_fuckups - FILENAME = '~/.thefuck/my_fuckups' + FILENAME = '~/.thefuck/.my_fuckups' FILE = open(FILENAME, 'r') IS_FUCKUP = True @@ -31,7 +25,7 @@ def get_fuckups(): # returns a dictionary of your fuckups from ~/.thefuck/my_fuc if IS_FUCKUP: key = line IS_FUCKUP = False - else + else: cmd = line IS_FUCKUP = True fuckups[key] = cmd @@ -40,8 +34,11 @@ def get_fuckups(): # returns a dictionary of your fuckups from ~/.thefuck/my_fuc return fuckups -def add_fuckup(fucked, fixed) - FILENAME = '~/.thefuck/my_fuckups' +def add_fuckup(fucked, fixed): + + print ('Adding fucked: ' + fucked + ' as fixed: ' + fixed) + + FILENAME = '~/.thefuck/.my_fuckups' FILE = open(FILENAME, 'w') FILE.write(fucked + '\n') @@ -51,9 +48,11 @@ def add_fuckup(fucked, fixed) return -def remove_fuckup(fuckup) +def remove_fuckup(fuckup): - FILENAME = '~/.thefuck/my_fuckups' + print ('Removing fucked: ' + fucked + ' as fixed: ' + fixed) + + FILENAME = '~/.thefuck/.my_fuckups' FILE = open(FILENAME, 'r') fuckup_cmd = '' @@ -64,7 +63,7 @@ def remove_fuckup(fuckup) FILE = open(FILENAME, 'w') - for number, line in enumerate(lines) + for number, line in enumerate(lines): if line != fuckup and line != fuckup_cmd: FILE.write(line) elif line == fuckup: diff --git a/thefuck/rules/my_fuckups.py b/thefuck/rules/my_fuckups.py index 611ccd5a..e7d5fada 100644 --- a/thefuck/rules/my_fuckups.py +++ b/thefuck/rules/my_fuckups.py @@ -1,5 +1,5 @@ -import custom_fuckups +import thefuck.custom_fuckups fuckups = custom_fuckups.get_fuckups()