change block generator not to held block is they are empty + minor pep8 refacto (pylint:8.85/10)

This commit is contained in:
0rax 2013-11-07 11:23:51 +01:00
parent b264c19491
commit fc2bb05f7b
1 changed files with 19 additions and 16 deletions

35
cheat
View File

@ -61,11 +61,10 @@ def pretty_print(filename):
class CheatSheets(object): class CheatSheets(object):
"""Cheatsheets database class."""
dirs = None dirs = None
sheets = None sheets = None
def __init__(self): def __init__(self):
self.dirs = self.__cheat_directories() self.dirs = self.__cheat_directories()
# verify that we have at least one cheat directory # verify that we have at least one cheat directory
@ -155,22 +154,24 @@ class CheatSheets(object):
# Attempt to write the new cheatsheet to the user's ~/.cheat # Attempt to write the new cheatsheet to the user's ~/.cheat
# dir if it exists. If it does not exist, attempt to create it. # dir if it exists. If it does not exist, attempt to create it.
if os.access(DEFAULT_CHEAT_DIR, os.W_OK) or os.makedirs(DEFAULT_CHEAT_DIR): if os.access(DEFAULT_CHEAT_DIR, os.W_OK) or os.makedirs(DEFAULT_CHEAT_DIR):
subprocess.call(editor + [os.path.join(DEFAULT_CHEAT_DIR, cheat)]) subprocess.call(editor
+ [os.path.join(DEFAULT_CHEAT_DIR, cheat)])
# If the directory cannot be created, write to the python # If the directory cannot be created, write to the python
# package directory, though that will likely require the use # package directory, though that will likely require the use
# of sudo # of sudo
else: else:
if os.access(sheet_path, os.W_OK): if os.access(sheet_path, os.W_OK):
subprocess.call(editor + [os.path.join(cs.cheat_dir, cheat)]) subprocess.call(editor
+ [os.path.join(cs.cheat_dir, cheat)])
else: else:
print >> sys.stderr, ("Couldn't create '%s' cheatsheet.\n" error_msg = ("Couldn't create '%s' cheatsheet.\n"
"Please retry usig sudo." "Please retry usig sudo." % cheat)
% cheat) print >> sys.stderr, error_msg
exit(1) exit(1)
except OSError, e: except OSError, errno:
print >> sys.stderr, ("Could not launch `%s` as your editor : %s" print >> sys.stderr, ("Could not launch `%s` as your editor : %s"
% (editor[0], e.strerror)) % (editor[0], errno.strerror))
exit(1) exit(1)
def list(self): def list(self):
@ -183,14 +184,16 @@ class CheatSheets(object):
"""Parse text blocks inside specified sheet file""" """Parse text blocks inside specified sheet file"""
block = "" block = ""
path = os.path.join(self.sheets[cheat], cheat) path = os.path.join(self.sheets[cheat], cheat)
with open(path) as fp: with open(path) as cheat_fp:
for line in fp.readlines(): for line in cheat_fp.readlines():
if line == '\n': if line == '\n':
yield block if block:
yield block
block = "" block = ""
else: else:
block += line block += line
yield block if block:
yield block
def search(self, term): def search(self, term):
"""Search for a term in sheetcheats""" """Search for a term in sheetcheats"""
@ -236,7 +239,7 @@ class SearchSheet(argparse.Action):
def main(): def main():
"""Main execution function"""
global sheets global sheets
sheets = CheatSheets() sheets = CheatSheets()
@ -294,8 +297,8 @@ def main():
pretty_print(filename) pretty_print(filename)
else: else:
with open(filename) as istream: with open(filename) as istream:
for l in istream: for line in istream:
sys.stdout.write(l) sys.stdout.write(line)
# if it does not, say so # if it does not, say so
else: else: