1
0
Fork 0
mirror of https://github.com/shlomif/PySolFC.git synced 2025-04-05 00:02:29 -04:00

flake8 : traverse recursively.

This commit is contained in:
Shlomi Fish 2017-04-19 15:45:56 +03:00
parent 8e89dd203c
commit 149496c90c
4 changed files with 197 additions and 151 deletions

View file

@ -1,37 +1,42 @@
#!/usr/bin/env python
import sys
import os
import re
import __builtin__
from pysollib.mygettext import fix_gettext
from pysollib.gamedb import GAME_DB
from pysollib.gamedb import GI
from pysollib.mfxutil import latin1_to_ascii
# outdir = '../html'
pysollib_dir = '../'
import sys, os, re
from glob import glob
import __builtin__
__builtin__._ = lambda x: x
__builtin__.n_ = lambda x: x
try: os.mkdir('html')
except: pass
try: os.mkdir('html/rules')
except: pass
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
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
files = [
('credits.html', 'PySol Credits'),
('ganjifa.html', 'PySol - General Ganjifa Card Rules'),
@ -67,10 +72,11 @@ main_header = '''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title>%s</title>
<meta name="license" content="Distributed under the terms of the GNU General Public License">
<meta name="license" content="GNU General Public License">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body text="#000000" bgcolor="#F7F3FF" link="#0000FF" vlink="#660099" alink="#FF0000">
<body text="#000000" bgcolor="#F7F3FF" link="#0000FF" vlink="#660099"
alink="#FF0000">
<img src="images/pysollogo03.gif" alt="">
<br>
'''
@ -85,10 +91,11 @@ rules_header = '''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title>%s</title>
<meta name="license" content="Distributed under the terms of the GNU General Public License">
<meta name="license" content="GNU General Public License">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body text="#000000" bgcolor="#F7F3FF" link="#0000FF" vlink="#660099" alink="#FF0000">
<body text="#000000" bgcolor="#F7F3FF" link="#0000FF" vlink="#660099"
link="#FF0000">
<img src="../images/pysollogo03.gif" alt="">
<br>
'''
@ -109,23 +116,26 @@ wikipedia_header = '''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title>%s</title>
<meta name="license" content="Distributed under the terms of the GNU Free Documentation License">
<meta name="license" content="GNU General Public License">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body text="#000000" bgcolor="#F7F3FF" link="#0000FF" vlink="#660099" alink="#FF0000">
<body text="#000000" bgcolor="#F7F3FF" link="#0000FF" vlink="#660099"
alink="#FF0000">
<img src="../images/pysollogo03.gif" alt="">
<br>
'''
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 = ''
@ -229,6 +240,3 @@ def gen_rules_html():
gen_main_html()
gen_rules_html()

View file

@ -4,13 +4,17 @@ import re
builder = TAP.Builder()
def plan(plan, plan_param=None):
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
@ -20,6 +24,7 @@ def eq_ok(have, want, comment):
diag("have: %s" % have)
return okness
def is_ok(have, want, comment):
okness = have is want
ok(okness, comment)
@ -28,6 +33,7 @@ def is_ok(have, want, comment):
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))

View file

@ -3,6 +3,7 @@ import atexit
# todo: make written-to stream passable
class Plan(object):
def __init__(self, plan, param=None):
self.counter = 0
@ -11,10 +12,11 @@ class Plan(object):
if isinstance(plan, int):
self.expected_tests = plan
print "1..%u" % self.expected_tests
elif plan == "no_plan" or plan == None: 1
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
print("1..0 # skip %s" % param)
raise SystemExit(0) # ??? this is what T::B does, but sucks
else:
raise TestBadPlan(plan)
@ -23,13 +25,15 @@ class Plan(object):
self.counter += 1
def __del__(self):
if self.ended: return
if self.ended:
return
self.ended = True
if self.expected_tests is None:
print "1..%u" % self.counter
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)
print("# Looks like you planned %u tests but ran %u."
% (self.expected_tests, self.counter))
class Builder(object):
global_defaults = {
@ -41,18 +45,21 @@ class Builder(object):
def __init__(self, plan=None, plan_param=None):
self.__dict__ = self.global_test_builder
if plan: self.set_plan(plan, plan_param)
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)
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)
if self.get_plan():
raise TestPlannedAlready(plan, plan_param)
self._plan = Plan(plan, plan_param)
atexit.register(self._plan.__del__)
@ -60,16 +67,21 @@ class Builder(object):
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"
if is_ok:
report = "ok"
else:
report = "not ok"
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
@ -77,22 +89,29 @@ class Builder(object):
def reset(self):
self.__dict__.clear()
for key in self.global_defaults.iterkeys():
for key in self.global_defaults.keys():
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
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"
class TestBadPlan(Exception):
def __init__(self, plan):
self.plan = plan
def __str__(self):
return "didn't understand plan '%s'" % self.plan

View file

@ -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