From 69428a72798276fe0b6624187e34a4d847166291 Mon Sep 17 00:00:00 2001 From: 0rax Date: Thu, 7 Nov 2013 00:34:37 +0100 Subject: [PATCH] Some minor modification (open inside the generator for better readability) + one bug fixe (the last block wasnt yiel by the generator) --- cheat | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/cheat b/cheat index fa19657..0e66ebc 100755 --- a/cheat +++ b/cheat @@ -179,26 +179,29 @@ class CheatSheets(object): return ('\n'.join(sorted(['%s [%s]' % (key.ljust(max_command), value) for key, value in self.sheets.items()]))) - def __parse_cheat_command_block(self, cheat_fp): + def __parse_cheat_command_block(self, cheat): + """Parse text blocks inside specified sheet file""" block = "" - for line in cheat_fp.readlines(): - if line == '\n' or line == '': - yield block - block = "" - else: - block += line + path = os.path.join(self.sheets[cheat], cheat) + with open(path) as fp: + for line in fp.readlines(): + if line == '\n': + yield block + block = "" + else: + block += line + yield block def search(self, term): - for sheet, sheet_dir in self.sheets.iteritems(): - path = os.path.join(sheet_dir, sheet) - with open(path) as f: - output = '' - for block in self.__parse_cheat_command_block(f): - if term in block: - if not output: - output = sheet + ":\n" - output += ''.join([" " + line + '\n' for line - in block.split('\n')]) + """Search for a term in sheetcheats""" + for cheat in self.sheets.keys(): + output = '' + for block in self.__parse_cheat_command_block(cheat): + if term in block: + if not output: + output = cheat + ":\n" + output += ''.join([" " + line + '\n' for line + in block.split('\n')]) if output: print output,