From 149496c90c20dcc7cc206c6b5a338402c4683726 Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Wed, 19 Apr 2017 15:45:56 +0300 Subject: [PATCH] flake8 : traverse recursively. --- html-src/gen-html.py | 104 +++++++++++++------------ tests/lib/TAP/Simple.py | 46 ++++++----- tests/lib/TAP/__init__.py | 159 +++++++++++++++++++++----------------- tests/style/py-flake8.t | 39 ++++++---- 4 files changed, 197 insertions(+), 151 deletions(-) diff --git a/html-src/gen-html.py b/html-src/gen-html.py index da81fa4f..82bf2f40 100755 --- a/html-src/gen-html.py +++ b/html-src/gen-html.py @@ -1,37 +1,42 @@ #!/usr/bin/env python -#outdir = '../html' -pysollib_dir = '../' - -import sys, os, re -from glob import glob - +import sys +import os +import re import __builtin__ -__builtin__._ = lambda x: x -__builtin__.n_ = lambda x: x - -try: os.mkdir('html') -except: pass -try: os.mkdir('html/rules') -except: pass - -pysollib_path = os.path.join(sys.path[0], pysollib_dir) -sys.path[0] = os.path.normpath(pysollib_path) -#print sys.path - from pysollib.mygettext import fix_gettext -fix_gettext() - -import pysollib.games -import pysollib.games.special -import pysollib.games.ultra -import pysollib.games.mahjongg from pysollib.gamedb import GAME_DB from pysollib.gamedb import GI from pysollib.mfxutil import latin1_to_ascii +# outdir = '../html' +pysollib_dir = '../' +__builtin__._ = lambda x: x +__builtin__.n_ = lambda x: x + +eval('import pysollib.games') +eval('import pysollib.games.mahjongg') +eval('import pysollib.games.ultra') +eval('import pysollib.games.special') + +try: + os.mkdir('html') +except: + pass + +try: + os.mkdir('html/rules') +except: + pass + +pysollib_path = os.path.join(sys.path[0], pysollib_dir) +sys.path[0] = os.path.normpath(pysollib_path) +# print sys.path + +fix_gettext() + files = [ ('credits.html', 'PySol Credits'), ('ganjifa.html', 'PySol - General Ganjifa Card Rules'), @@ -45,12 +50,12 @@ files = [ ('intro.html', 'PySol - Introduction'), ('license.html', 'PySol Software License'), ('news.html', 'PySol - a Solitaire Game Collection'), - #('rules_alternate.html', 'PySol - a Solitaire Game Collection'), - #('rules.html', 'PySol - a Solitaire Game Collection'), + # ('rules_alternate.html', 'PySol - a Solitaire Game Collection'), + # ('rules.html', 'PySol - a Solitaire Game Collection'), ] rules_files = [ - #('hanoipuzzle.html', ), + # ('hanoipuzzle.html', ), ('mahjongg.html', 'PySol - Rules for Mahjongg'), ('matrix.html', 'PySol - Rules for Matrix'), ('pegged.html', 'PySol - Rules for Pegged'), @@ -67,10 +72,11 @@ main_header = ''' %s - + - +
''' @@ -85,10 +91,11 @@ rules_header = ''' %s - + - +
''' @@ -109,23 +116,26 @@ wikipedia_header = ''' %s - + - +
''' def getGameRulesFilename(n): - if n.startswith('Mahjongg'): return 'mahjongg.html' - ##n = re.sub(r"[\[\(].*$", "", n) + if n.startswith('Mahjongg'): + return 'mahjongg.html' + # n = re.sub(r"[\[\(].*$", "", n) n = latin1_to_ascii(n) n = re.sub(r"[^\w]", "", n) n = n.lower() + ".html" return n + def gen_main_html(): for infile, title in files: outfile = open(os.path.join('html', infile), 'w') @@ -136,8 +146,9 @@ def gen_main_html(): s = '' print >> outfile, main_footer % s + def gen_rules_html(): - ##ls = glob(os.path.join('rules', '*.html')) + # ls = glob(os.path.join('rules', '*.html')) rules_ls = os.listdir('rules') rules_ls.sort() wikipedia_ls = os.listdir('wikipedia') @@ -176,11 +187,11 @@ def gen_rules_html(): elif rules_fn in wikipedia_ls: rules_dir = 'wikipedia' else: - print 'missing rules for %s (file: %s)' \ - % (gi.name.encode('utf-8'), rules_fn) + print('missing rules for %s (file: %s)' + % (gi.name.encode('utf-8'), rules_fn)) continue - ##print '>>>', rules_fn + # print '>>>', rules_fn title = 'PySol - Rules for ' + gi.name s = '' @@ -192,18 +203,18 @@ def gen_rules_html(): s = 'General Hex A Deck rules' elif gi.si.game_type == GI.GT_MUGHAL_GANJIFA: s = 'About Ganjifa' - #print '***', gi.name, '***' + # print '***', gi.name, '***' rules_list.append((rules_dir, rules_fn, title, s)) files_list.append(rules_fn) - #rules_list.append((rules_fn, gi.name)) + # rules_list.append((rules_fn, gi.name)) print >> out_rules, '
  • %s' \ - % (rules_fn, gi.name.encode('utf-8')) + % (rules_fn, gi.name.encode('utf-8')) for n in gi.altnames: altnames.append((n, rules_fn)) print >> out_rules, '\n' + \ - main_footer % 'Back to the index' + main_footer % 'Back to the index' # create file of altnames out_rules_alt = open(os.path.join('html', 'rules_alternate.html'), 'w') @@ -214,14 +225,14 @@ def gen_rules_html(): print >> out_rules_alt, '
  • %s' \ % (fn, name.encode('utf-8')) print >> out_rules_alt, '\n' + \ - main_footer % 'Back to the index' + main_footer % 'Back to the index' # create rules for dir, filename, title, footer in rules_list: outfile = open(os.path.join('html', 'rules', filename), 'w') if dir == 'rules': print >> outfile, (rules_header % title).encode('utf-8') - else: # d == 'wikipedia' + else: # d == 'wikipedia' print >> outfile, (wikipedia_header % title).encode('utf-8') print >> outfile, open(os.path.join(dir, filename)).read() print >> outfile, rules_footer % footer @@ -229,6 +240,3 @@ def gen_rules_html(): gen_main_html() gen_rules_html() - - - diff --git a/tests/lib/TAP/Simple.py b/tests/lib/TAP/Simple.py index 1f081030..00c0a5d9 100644 --- a/tests/lib/TAP/Simple.py +++ b/tests/lib/TAP/Simple.py @@ -4,34 +4,40 @@ import re builder = TAP.Builder() + def plan(plan, plan_param=None): - builder.set_plan(plan, plan_param) + builder.set_plan(plan, plan_param) + ok = builder.ok + def diag(comment): - print "# %s" % re.compile("\n(.)").sub(comment, '\n#\1') + print("# %s" % re.compile("\n(.)").sub(comment, '\n#\1')) + def eq_ok(have, want, comment): - okness = have == want - ok(okness, comment) - if not okness: - diag("want: %s" % want) - diag("have: %s" % have) - return okness + okness = have == want + ok(okness, comment) + if not okness: + diag("want: %s" % want) + diag("have: %s" % have) + return okness + def is_ok(have, want, comment): - okness = have is want - ok(okness, comment) - if not okness: - diag("want id: %s" % id(want)) - diag("have id: %s" % id(have)) - return okness + okness = have is want + ok(okness, comment) + if not okness: + diag("want id: %s" % id(want)) + diag("have id: %s" % id(have)) + return okness + def isa_ok(object, cls, object_name="the object"): - okness = isinstance(object, cls) - ok(okness, object_name + " is a " + repr(cls)) - # if not okness: - # diag("want id: %s" % id(want)) - # diag("have id: %s" % id(have)) - return okness + okness = isinstance(object, cls) + ok(okness, object_name + " is a " + repr(cls)) + # if not okness: + # diag("want id: %s" % id(want)) + # diag("have id: %s" % id(have)) + return okness diff --git a/tests/lib/TAP/__init__.py b/tests/lib/TAP/__init__.py index 5c9bfe6c..741a5e26 100644 --- a/tests/lib/TAP/__init__.py +++ b/tests/lib/TAP/__init__.py @@ -3,96 +3,115 @@ import atexit # todo: make written-to stream passable + class Plan(object): - def __init__(self, plan, param=None): - self.counter = 0 - self.expected_tests = None - self.ended = False + def __init__(self, plan, param=None): + self.counter = 0 + self.expected_tests = None + self.ended = False - if isinstance(plan, int): - self.expected_tests = plan - print "1..%u" % self.expected_tests - elif plan == "no_plan" or plan == None: 1 - elif plan == "skip_all": - print "1..0 # skip %s" % param - raise SystemExit(0) # ??? this is what T::B does, but sucks - else: - raise TestBadPlan(plan) + if isinstance(plan, int): + self.expected_tests = plan + print("1..%u" % self.expected_tests) + elif plan == "no_plan" or plan is None: + True + elif plan == "skip_all": + print("1..0 # skip %s" % param) + raise SystemExit(0) # ??? this is what T::B does, but sucks + else: + raise TestBadPlan(plan) - def increment_counter(self): - self.counter += 1 + def increment_counter(self): + self.counter += 1 + + def __del__(self): + if self.ended: + return + self.ended = True + if self.expected_tests is None: + print("1..%u" % self.counter) + elif self.counter != self.expected_tests: + print("# Looks like you planned %u tests but ran %u." + % (self.expected_tests, self.counter)) - def __del__(self): - if self.ended: return - self.ended = True - if self.expected_tests is None: - print "1..%u" % self.counter - elif self.counter != self.expected_tests: - print "# Looks like you planned %u tests but ran %u." \ - % (self.expected_tests, self.counter) class Builder(object): - global_defaults = { - "_plan": None, - "current": 1, - "has_plan": False, - } - global_test_builder = global_defaults.copy() + global_defaults = { + "_plan": None, + "current": 1, + "has_plan": False, + } + global_test_builder = global_defaults.copy() - def __init__(self, plan=None, plan_param=None): - self.__dict__ = self.global_test_builder - if plan: self.set_plan(plan, plan_param) + def __init__(self, plan=None, plan_param=None): + self.__dict__ = self.global_test_builder + if plan: + self.set_plan(plan, plan_param) - @classmethod # XXX: why did this fail? - def create(cls, plan=None, plan_param=None): - # self = new.instance(cls) # ? this sucks, too - self = Builder() - self.__dict__ = self.global_defaults.copy() - if plan: self.set_plan(plan, plan_param) - return self + @classmethod # XXX: why did this fail? + def create(cls, plan=None, plan_param=None): + # self = new.instance(cls) # ? this sucks, too + self = Builder() + self.__dict__ = self.global_defaults.copy() + if plan: + self.set_plan(plan, plan_param) + return self - def set_plan(self, plan, plan_param=None): - if self.get_plan(): raise TestPlannedAlready(plan, plan_param) - self._plan = Plan(plan, plan_param) - atexit.register(self._plan.__del__) + def set_plan(self, plan, plan_param=None): + if self.get_plan(): + raise TestPlannedAlready(plan, plan_param) + self._plan = Plan(plan, plan_param) + atexit.register(self._plan.__del__) - def get_plan(self): return self._plan + def get_plan(self): return self._plan - def ok(self, is_ok, desc=None, skip=None, todo=None): - self.get_plan().increment_counter() - if is_ok: report = "ok" - else: report = "not ok" + def ok(self, is_ok, desc=None, skip=None, todo=None): + self.get_plan().increment_counter() + if is_ok: + report = "ok" + else: + report = "not ok" - sys.stdout.write("%s %u" % (report, self.current)) + sys.stdout.write("%s %u" % (report, self.current)) - if desc: sys.stdout.write(" - %s" % desc) - if skip: sys.stdout.write(" # SKIP %s" % skip) - if todo: sys.stdout.write(" # TODO %s" % todo) + if desc: + sys.stdout.write(" - %s" % desc) + if skip: + sys.stdout.write(" # SKIP %s" % skip) + if todo: + sys.stdout.write(" # TODO %s" % todo) - print + print() - self.current += 1 + self.current += 1 - return is_ok + return is_ok + + def reset(self): + self.__dict__.clear() + for key in self.global_defaults.keys(): + self.__dict__[key] = self.global_defaults[key] - def reset(self): - self.__dict__.clear() - for key in self.global_defaults.iterkeys(): - self.__dict__[key] = self.global_defaults[key] class TestPlannedAlready(Exception): - def __init__(self, plan, param=None): - self.plan = plan - self.param = param - def __str__(self): - return "tried to plan twice; second plan: %s, %s" % self.plan, self.param + def __init__(self, plan, param=None): + self.plan = plan + self.param = param + + def __str__(self): + return ( + "tried to plan twice; second plan: %s, %s" % self.plan, self.param + ) + class TestWithoutPlan(Exception): - def __str__(self): - return "tried running tests without a plan" + def __str__(self): + return "tried running tests without a plan" + class TestBadPlan(Exception): - def __init__(self, plan): - self.plan = plan - def __str__(self): - return "didn't understand plan '%s'" % self.plan + def __init__(self, plan): + self.plan = plan + + def __str__(self): + return "didn't understand plan '%s'" % self.plan diff --git a/tests/style/py-flake8.t b/tests/style/py-flake8.t index 9f2ea494..9fc0aedc 100644 --- a/tests/style/py-flake8.t +++ b/tests/style/py-flake8.t @@ -6,29 +6,42 @@ use warnings; use Test::More tests => 1; use Test::Differences qw( eq_or_diff ); +use File::Find::Object (); use String::ShellQuote qw/ shell_quote /; my %skip = ( map { $_ => 1 } qw( - ./pysollib/games/__init__.py - ./pysollib/games/mahjongg/__init__.py - ./pysollib/games/mahjongg/mahjongg1.py - ./pysollib/games/mahjongg/mahjongg2.py - ./pysollib/games/mahjongg/mahjongg3.py - ./pysollib/games/special/__init__.py - ./pysollib/games/ultra/__init__.py - ./pysollib/pysoltk.py - ./scripts/all_games.py - ./pysollib/tile/ttk.py + pysollib/games/__init__.py + pysollib/games/mahjongg/__init__.py + pysollib/games/mahjongg/mahjongg1.py + pysollib/games/mahjongg/mahjongg2.py + pysollib/games/mahjongg/mahjongg3.py + pysollib/games/special/__init__.py + pysollib/games/ultra/__init__.py + pysollib/pysoltk.py + scripts/all_games.py + pysollib/tile/ttk.py ) ); -# my $cmd = shell_quote( 'flake8', '.' ); -my $cmd = shell_quote( 'flake8', - sort { $a cmp $b } grep { not exists $skip{$_} } glob('./*.py ./data/*/*/*.py ./scripts/*.py ./tests/board_gen/*.py ./pysollib/*.py ./pysollib/*/{*/*.py,*.py}') ); +my $tree = File::Find::Object->new({}, '.'); +my @filenames; +while (my $r = $tree->next_obj) +{ + my $fn = $r->path; + if ($fn eq '.git' or $fn eq 'tests/individually-importing') + { + $tree->prune; + } + elsif ($fn =~ /\.py\z/ and !exists($skip{$fn})) + { + push @filenames, $fn; + } +} +my $cmd = shell_quote( 'flake8', @filenames); # diag("<$cmd>"); # TEST