Commit c40a74c7 authored by Laurent Peuch's avatar Laurent Peuch
Browse files

style: black project

parent 5d4193ac4730
"""the AIL package, macking available everything in the ail.ail module
"""
__revision__ = '$Id: __init__.py,v 1.3 2004-09-24 10:49:30 syt Exp $'
__revision__ = "$Id: __init__.py,v 1.3 2004-09-24 10:49:30 syt Exp $"
from .ail import *
......@@ -19,5 +19,5 @@
__revision__ = "$Id: __pkginfo__.py,v 1.16 2005-03-25 14:31:12 adim Exp $"
modname = 'ail'
modname = "ail"
numversion = (0, 3, 0)
......@@ -23,4 +23,4 @@
numversion = (0, 3, 0)
version = '.'.join([str(num) for num in numversion])
pyversions = ('2.3', '2.4', '2.5')
version = ".".join([str(num) for num in numversion])
pyversions = ("2.3", "2.4", "2.5")
......@@ -26,7 +26,7 @@
license = 'GPL'
copyright = '''Copyright (c) 2004-2006 LOGILAB S.A. (Paris, FRANCE).
http://www.logilab.fr/ -- mailto:contact@logilab.fr'''
license = "GPL"
copyright = """Copyright (c) 2004-2006 LOGILAB S.A. (Paris, FRANCE).
http://www.logilab.fr/ -- mailto:contact@logilab.fr"""
author = "Logilab"
author_email = "contact@logilab.fr"
......@@ -42,6 +42,6 @@
ftp = "http://ftp.logilab.org/pub/%s" % modname
mailinglist = "http://lists.logilab.org/mailman/listinfo/ai-projects"
scripts = ['bin/ail']
scripts = ["bin/ail"]
from os.path import join
......@@ -46,4 +46,5 @@
from os.path import join
include_dirs = [join('test', 'data')]
include_dirs = [join("test", "data")]
......@@ -49,5 +50,4 @@
debian_maintainer = 'Arthur Lutz'
debian_maintainer_email = 'arthur.lutz@logilab.fr'
debian_handler = 'python-library'
debian_maintainer = "Arthur Lutz"
debian_maintainer_email = "arthur.lutz@logilab.fr"
debian_handler = "python-library"
......@@ -32,6 +32,7 @@
from os.path import basename, dirname, abspath, join
from random import choice
def usage(status=1):
"""display usage and exit
......@@ -41,9 +42,10 @@
print(__doc__ % basename(sys.argv[0]))
sys.exit(status)
def run(args):
"""command line launcher
:type args: list
:param args: command line arguments, without the script name
"""
......@@ -44,8 +46,8 @@
def run(args):
"""command line launcher
:type args: list
:param args: command line arguments, without the script name
"""
long_list = ['help']
long_list = ["help"]
try:
......@@ -51,3 +53,3 @@
try:
optlist, args = getopt.getopt(args, 'h', long_list)
optlist, args = getopt.getopt(args, "h", long_list)
except getopt.error as ex:
......@@ -53,4 +55,4 @@
except getopt.error as ex:
print('Error:', ex)
print("Error:", ex)
usage(1)
for opt, val in optlist:
......@@ -55,8 +57,8 @@
usage(1)
for opt, val in optlist:
if opt in ('-h', '--help'):
if opt in ("-h", "--help"):
usage(0)
brain = Brain(AILBrainHandler())
brain.load(args)
try:
......@@ -58,10 +60,10 @@
usage(0)
brain = Brain(AILBrainHandler())
brain.load(args)
try:
while True :
input = input('>>> ')
while True:
input = input(">>> ")
try:
answer = brain.think(input)
except AilError as exc:
......@@ -65,6 +67,6 @@
try:
answer = brain.think(input)
except AilError as exc:
answer = 'Error: %s' % exc
if answer.startswith('assert') :
answer = "Error: %s" % exc
if answer.startswith("assert"):
print("I learned that", answer[6:])
......@@ -70,3 +72,3 @@
print("I learned that", answer[6:])
elif answer.startswith('search') :
elif answer.startswith("search"):
print("I search for", answer[6:])
......@@ -72,3 +74,3 @@
print("I search for", answer[6:])
else :
else:
print(answer)
......@@ -74,5 +76,5 @@
print(answer)
except (KeyboardInterrupt, EOFError) :
except (KeyboardInterrupt, EOFError):
# print "I'm dying..."
pass
......@@ -76,7 +78,10 @@
# print "I'm dying..."
pass
ENV_VAR = re.compile(r'\$\{(\w+)\}')
ENV_VAR = re.compile(r"\$\{(\w+)\}")
def expand_vars(text, env_vars):
"""replaces variables with their values
......@@ -88,9 +93,10 @@
"""
return ENV_VAR.sub(lambda m: env_vars.get(m.group(1), m.group(0)), text)
def read_from_stream(stream, base_dir='', env_context = None):
def read_from_stream(stream, base_dir="", env_context=None):
"""read a AIL formatted file and yield each REWRITE / TERMINAL rules,
formatted as a 3-uple (RULE TYPE, LHS REGEXP, RHS STRING)
"""
return read_from_lines(stream.readlines(), base_dir, env_context)
......@@ -92,8 +98,9 @@
"""read a AIL formatted file and yield each REWRITE / TERMINAL rules,
formatted as a 3-uple (RULE TYPE, LHS REGEXP, RHS STRING)
"""
return read_from_lines(stream.readlines(), base_dir, env_context)
def read_from_string(string, base_dir='', env_context = None):
def read_from_string(string, base_dir="", env_context=None):
return read_from_lines(string.splitlines(), base_dir, env_context)
......@@ -98,6 +105,7 @@
return read_from_lines(string.splitlines(), base_dir, env_context)
def read_from_lines(lines, base_dir='', env_context = None):
def read_from_lines(lines, base_dir="", env_context=None):
env_context = env_context or {}
for line in lines:
line = expand_vars(line.strip(), env_context)
......@@ -101,5 +109,5 @@
env_context = env_context or {}
for line in lines:
line = expand_vars(line.strip(), env_context)
if line.startswith('#') or not line :
if line.startswith("#") or not line:
continue
......@@ -105,4 +113,10 @@
continue
if '>>' in line :
left, right = line.split('>>')
if ">>" in line:
left, right = line.split(">>")
left, right = left.strip(), right.strip()
if not left.endswith("$"):
left += "$"
yield ("REWRITE", re.compile(left, re.I), right)
elif "::" in line:
left, right = line.split("::")
left, right = left.strip(), right.strip()
......@@ -108,14 +122,8 @@
left, right = left.strip(), right.strip()
if not left.endswith('$'):
left += '$'
yield ('REWRITE', re.compile(left, re.I), right)
elif '::' in line :
left, right = line.split('::')
left, right = left.strip(), right.strip()
right = right.replace('%', '%%')
right = re.sub(r'\\(\d+)', sub_hdlr, right)
if not left.endswith('$'):
left += '$'
yield ('TERMINAL', re.compile(left, re.I), right)
elif line.startswith('include '):
right = right.replace("%", "%%")
right = re.sub(r"\\(\d+)", sub_hdlr, right)
if not left.endswith("$"):
left += "$"
yield ("TERMINAL", re.compile(left, re.I), right)
elif line.startswith("include "):
filename = line[8:].strip()
......@@ -121,5 +129,7 @@
filename = line[8:].strip()
yield from read_from_stream(file(join(base_dir, filename)), env_context = env_context)
yield from read_from_stream(
file(join(base_dir, filename)), env_context=env_context
)
else:
raise BadAilRule(line)
......@@ -123,5 +133,6 @@
else:
raise BadAilRule(line)
def sub_hdlr(match):
"""handler function for rgx.sub to transform \n to %(n-1)s"""
......@@ -126,7 +137,8 @@
def sub_hdlr(match):
"""handler function for rgx.sub to transform \n to %(n-1)s"""
return r'%%(%s)s' % (int(match.group(1)) - 1)
return r"%%(%s)s" % (int(match.group(1)) - 1)
class AilError(Exception):
"""base ail exception"""
......@@ -129,7 +141,8 @@
class AilError(Exception):
"""base ail exception"""
class NoRewriteError(AilError):
"""exception used when no more rewrite rules are available"""
......@@ -133,6 +146,7 @@
class NoRewriteError(AilError):
"""exception used when no more rewrite rules are available"""
class InfiniteCycleError(AilError):
"""exception used when the thinking cycles and doesn't seem to find an end"""
......@@ -136,6 +150,7 @@
class InfiniteCycleError(AilError):
"""exception used when the thinking cycles and doesn't seem to find an end"""
class BadAilRule(AilError):
"""exception used when a malformatted rule is parsed"""
......@@ -139,6 +154,7 @@
class BadAilRule(AilError):
"""exception used when a malformatted rule is parsed"""
class BadAilCommand(AilError):
"""exception used when an unknown command was used"""
......@@ -142,5 +158,6 @@
class BadAilCommand(AilError):
"""exception used when an unknown command was used"""
class AILBrainHandler:
"""a default command handler used to interactivly play with ail"""
......@@ -145,3 +162,4 @@
class AILBrainHandler:
"""a default command handler used to interactivly play with ail"""
def command(self, cmdname, *args):
......@@ -147,6 +165,6 @@
def command(self, cmdname, *args):
return f'command : {cmdname} {str(args)}'
return f"command : {cmdname} {str(args)}"
def random(self, *args):
return choice(args)
......@@ -149,6 +167,7 @@
def random(self, *args):
return choice(args)
def normalize(input):
"""normalize a string by removing new lines an escaping quotes"""
......@@ -153,6 +172,7 @@
def normalize(input):
"""normalize a string by removing new lines an escaping quotes"""
return input.replace("\'", r"\'")
return input.replace("'", r"\'")
def type_cast(string, encoding=None):
"""type cast a string, handling None, int and float values"""
......@@ -156,7 +176,7 @@
def type_cast(string, encoding=None):
"""type cast a string, handling None, int and float values"""
if string == 'None':
if string == "None":
return None
try:
return int(string)
......@@ -168,4 +188,5 @@
string = str(string, encoding)
return string
class Brain:
......@@ -171,4 +192,3 @@
class Brain:
"""the ail brain: process text according to loaded rules
"""
"""the ail brain: process text according to loaded rules"""
......@@ -174,7 +194,7 @@
def __init__(self, cmds_hdlr, encoding='ISO-8859-1'):
def __init__(self, cmds_hdlr, encoding="ISO-8859-1"):
self._patterns = []
self._cmds_hdlr = cmds_hdlr
self.encoding = encoding
self.source_files = []
......@@ -176,9 +196,9 @@
self._patterns = []
self._cmds_hdlr = cmds_hdlr
self.encoding = encoding
self.source_files = []
def load(self, filenames, env_context = None):
def load(self, filenames, env_context=None):
"""load rules files"""
for name in filenames:
# print "plugin' brain %s..." % name
......@@ -192,7 +212,7 @@
self.source_files = []
self.load(filenames)
def load_stream(self, stream, base_dir='', env_context = None):
def load_stream(self, stream, base_dir="", env_context=None):
"""load rules from stream"""
self._add_patterns(read_from_stream(stream, base_dir, env_context))
......@@ -196,7 +216,7 @@
"""load rules from stream"""
self._add_patterns(read_from_stream(stream, base_dir, env_context))
def load_string(self, string, base_dir='', env_context = None):
def load_string(self, string, base_dir="", env_context=None):
"""load rules from string"""
self._add_patterns(read_from_string(string, base_dir, env_context))
......@@ -200,6 +220,6 @@
"""load rules from string"""
self._add_patterns(read_from_string(string, base_dir, env_context))
def _add_patterns(self, patterns) :
def _add_patterns(self, patterns):
self._patterns += patterns
......@@ -204,6 +224,6 @@
self._patterns += patterns
def _rewrite(self, input) :
def _rewrite(self, input):
for target, rgx, next in self._patterns:
match = rgx.match(input)
if match is not None:
......@@ -207,7 +227,7 @@
for target, rgx, next in self._patterns:
match = rgx.match(input)
if match is not None:
if target == 'REWRITE':
if target == "REWRITE":
next = rgx.sub(next, input)
else:
# -> Some of the elements in match.groups() might be None
......@@ -215,7 +235,7 @@
groups = []
for grp in match.groups():
if grp is None:
groups.append('')
groups.append("")
else:
groups.append(normalize(grp))
# print "NEXT = %r, dict = %s" % (next, dict([(str(key), val) for key, val in enumerate(groups)]))
......@@ -232,5 +252,5 @@
input = input.encode(self.encoding)
else:
encoding = None
rule_type, next = self._rewrite(' '.join(input.splitlines()))
rule_type, next = self._rewrite(" ".join(input.splitlines()))
cycle_stopper = 0
......@@ -236,5 +256,5 @@
cycle_stopper = 0
while rule_type == 'REWRITE' :
while rule_type == "REWRITE":
cycle_stopper += 1
if cycle_stopper > 1000:
raise InfiniteCycleError
......@@ -238,5 +258,5 @@
cycle_stopper += 1
if cycle_stopper > 1000:
raise InfiniteCycleError
## try:
## try:
rule_type, next = self._rewrite(next)
......@@ -242,10 +262,10 @@
rule_type, next = self._rewrite(next)
## except NoRewriteError :
## # FIXME (syt+adim): should not catch this exception, imo rewrite without
## # ending command is a bug in the rule file
## return next
## if rule_type == 'TERMINAL':
## except NoRewriteError :
## # FIXME (syt+adim): should not catch this exception, imo rewrite without
## # ending command is a bug in the rule file
## return next
## if rule_type == 'TERMINAL':
words = self.split(next)
try:
method = getattr(self._cmds_hdlr, words[0].lower())
except AttributeError:
......@@ -248,9 +268,9 @@
words = self.split(next)
try:
method = getattr(self._cmds_hdlr, words[0].lower())
except AttributeError:
raise AilError('Unknown action %s' % words[0].lower())
raise AilError("Unknown action %s" % words[0].lower())
return method(*(type_cast(w, encoding) for w in words[1:]))
def split(self, string):
shl = shlex.shlex(string, posix=True)
......@@ -253,8 +273,8 @@
return method(*(type_cast(w, encoding) for w in words[1:]))
def split(self, string):
shl = shlex.shlex(string, posix=True)
shl.wordchars += '+-'
shl.wordchars += "+-"
shl.escapedquotes += "'"
return list(shl)
......@@ -262,5 +282,5 @@
"""yield rules generating the given command with optional arguments"""
searching = [command] + list(args)
for target, rgx, next in self._patterns:
if target == 'REWRITE':
if target == "REWRITE":
continue
......@@ -266,5 +286,5 @@
continue
if self.split(next)[:len(searching)] == searching:
if self.split(next)[: len(searching)] == searching:
yield rgx.pattern
......@@ -268,6 +288,5 @@
yield rgx.pattern
if __name__ == '__main__' :
if __name__ == "__main__":
run(sys.argv[1:])
......@@ -18,7 +18,6 @@
""" Generic Setup script, takes package info from __pkginfo__.py file """
import os
import sys
import shutil
......@@ -28,8 +27,17 @@
from os.path import isdir, exists, join, walk
# import required features
from .__pkginfo__ import modname, version, license, short_desc, long_desc, \
web, author, author_email
from .__pkginfo__ import (
modname,
version,
license,
short_desc,
long_desc,
web,
author,
author_email,
)
# import optional features
try:
from .__pkginfo__ import distname
......@@ -56,9 +64,9 @@
except ImportError:
ext_modules = None
BASE_BLACKLIST = ('CVS', 'debian', 'dist', 'build', '__buildlog')
IGNORED_EXTENSIONS = ('.pyc', '.pyo', '.elc')
BASE_BLACKLIST = ("CVS", "debian", "dist", "build", "__buildlog")
IGNORED_EXTENSIONS = (".pyc", ".pyo", ".elc")
def ensure_scripts(linux_scripts):
"""
......@@ -66,11 +74,12 @@
(taken from 4Suite)
"""
from distutils import util
if util.get_platform()[:3] == 'win':
scripts_ = [script + '.bat' for script in linux_scripts]
if util.get_platform()[:3] == "win":
scripts_ = [script + ".bat" for script in linux_scripts]
else:
scripts_ = linux_scripts
return scripts_
def get_packages(directory, prefix):
......@@ -71,12 +80,11 @@
else:
scripts_ = linux_scripts
return scripts_
def get_packages(directory, prefix):
"""return a list of subpackages for the given directory
"""
"""return a list of subpackages for the given directory"""
result = []
for package in os.listdir(directory):
absfile = join(directory, package)
if isdir(absfile):
......@@ -79,7 +87,6 @@
result = []
for package in os.listdir(directory):
absfile = join(directory, package)
if isdir(absfile):
if exists(join(absfile, '__init__.py')) or \
package in ('test', 'tests'):
if exists(join(absfile, "__init__.py")) or package in ("test", "tests"):
if prefix:
......@@ -85,7 +92,7 @@
if prefix:
result.append(f'{prefix}.{package}')
result.append(f"{prefix}.{package}")
else:
result.append(package)
result += get_packages(absfile, result[-1])
return result
......@@ -87,11 +94,10 @@
else:
result.append(package)
result += get_packages(absfile, result[-1])
return result
def export(from_dir, to_dir,
blacklist=BASE_BLACKLIST,
ignore_ext=IGNORED_EXTENSIONS):
def export(from_dir, to_dir, blacklist=BASE_BLACKLIST, ignore_ext=IGNORED_EXTENSIONS):
"""make a mirror of from_dir in to_dir, omitting directories and files
listed in the black list
"""
......@@ -95,6 +101,7 @@
"""make a mirror of from_dir in to_dir, omitting directories and files
listed in the black list
"""
def make_mirror(arg, directory, fnames):
"""walk handler"""
for norecurs in blacklist:
......@@ -106,5 +113,5 @@
# don't include binary files
if filename[-4:] in ignore_ext:
continue
if filename[-1] == '~':
if filename[-1] == "~":
continue
......@@ -110,7 +117,7 @@
continue
src = f'{directory}/{filename}'
dest = to_dir + src[len(from_dir):]
print(src, '->', dest, file=sys.stderr)
src = f"{directory}/{filename}"
dest = to_dir + src[len(from_dir) :]
print(src, "->", dest, file=sys.stderr)
if os.path.isdir(src):
if not exists(dest):
os.mkdir(dest)
......@@ -118,8 +125,9 @@
if exists(dest):
os.remove(dest)
shutil.copy2(src, dest)
try:
os.mkdir(to_dir)
except OSError as ex:
# file exists ?
import errno
......@@ -121,8 +129,9 @@
try:
os.mkdir(to_dir)
except OSError as ex:
# file exists ?
import errno
if ex.errno != errno.EEXIST:
raise
walk(from_dir, make_mirror, None)
......@@ -130,4 +139,5 @@
EMPTY_FILE = '"""generated file, don\'t modify or your data will be lost"""\n'
class BuildScripts(command.install_lib.install_lib):
......@@ -133,5 +143,4 @@
class BuildScripts(command.install_lib.install_lib):
def run(self):
command.install_lib.install_lib.run(self)
# manually install included directories if any
......@@ -140,7 +149,8 @@
for directory in include_dirs:
dest = join(self.install_dir, base, directory)
export(directory, dest)
def install(**kwargs):
"""setup entry point"""
if subpackage_of:
......@@ -144,7 +154,7 @@
def install(**kwargs):
"""setup entry point"""
if subpackage_of:
package = subpackage_of + '.' + modname
kwargs['package_dir'] = {package : '.'}
package = subpackage_of + "." + modname
kwargs["package_dir"] = {package: "."}
packages = [package] + get_packages(os.getcwd(), package)
else:
......@@ -149,4 +159,4 @@
packages = [package] + get_packages(os.getcwd(), package)
else:
kwargs['package_dir'] = {modname : '.'}