Skip to content

Commit

Permalink
New distribution [0.3.5]
Browse files Browse the repository at this point in the history
 * bugfix in linesep when conversion
 * revised linesep determination
  • Loading branch information
JarryShaw committed Nov 9, 2019
1 parent c1e6ec6 commit e57f2b0
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 35 deletions.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"Envs",
"NPROCESSORS",
"aktau",
"bufpre",
"bufsuf",
"cmds",
"flst",
"funcdef",
Expand Down
109 changes: 81 additions & 28 deletions poseur.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
del multiprocessing

# version string
__version__ = '0.3.4'
__version__ = '0.3.5'

# from configparser
BOOLEAN_STATES = {'1': True, '0': False,
Expand All @@ -51,7 +51,11 @@


class ConvertError(SyntaxError):
pass
"""Parso syntax error."""


class EnvironError(EnvironmentError):
"""Invalid environment."""


###############################################################################
Expand All @@ -72,29 +76,31 @@ def predicate(filename): # pragma: no cover
###############################################################################
# Positional-only decorator

_decorator = '''
def %s(*poseur):
"""Positional-only arguments runtime checker.
Args:
- `*poseur` -- `str`, name of positional-only arguments
Refs:
- https://mail.python.org/pipermail/python-ideas/2017-February/044888.html
"""
import functools
def caller(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
poseur_args = set(poseur).intersection(kwargs)
if poseur_args:
raise TypeError('%%s() got some positional-only arguments passed as keyword arguments: %%r' %%
(func.__name__, ', '.join(poseur_args)))
return func(*args, **kwargs)
return wrapper
return caller
'''.splitlines()
_decorator = [
'',
'def %s(*poseur):',
' """Positional-only arguments runtime checker.',
'',
' Args:',
' - `*poseur` -- `str`, name of positional-only arguments',
'',
' Refs:',
' - https://mail.python.org/pipermail/python-ideas/2017-February/044888.html',
'',
' """',
' import functools',
' def caller(func):',
' @functools.wraps(func)',
' def wrapper(*args, **kwargs):',
' poseur_args = set(poseur).intersection(kwargs)',
' if poseur_args:',
" raise TypeError('%%s() got some positional-only arguments passed as keyword arguments: %%r' %%",
" (func.__name__, ', '.join(poseur_args)))",
' return func(*args, **kwargs)',
' return wrapper',
' return caller',
'',
]

exec(os.linesep.join(_decorator) % 'decorator')

Expand Down Expand Up @@ -281,7 +287,7 @@ def decorate_funcdef(parameters, column, funcdef):
- `str` -- decorated function definition
"""
POSEUR_LINESEP = os.getenv('POSEUR_LINESEP', os.linesep)
POSEUR_LINESEP = get_linesep(parameters[0])
POSEUR_DECORATOR = os.getenv('POSEUR_DECORATOR', __poseur_decorator__)

prefix = ''
Expand Down Expand Up @@ -528,6 +534,53 @@ def check_suffix(string):
return prefix, suffix


def guess_linesep(node):
"""Guess line separator based on source code.
Args:
- `node` -- `Union[parso.python.tree.Module, parso.python.tree.PythonNode, parso.python.tree.PythonLeaf]`,
parso AST
Returns:
- `str` -- line separator
"""
root = node.get_root_node()
code = root.get_code()

pool = [0, 0] # LF, CRLF
for line in code.splitlines(True):
if line.endswith('\r\n'):
pool[1] += 1
else:
pool[0] += 1
if pool[0] >= pool[1]:
return '\n'
return '\r\n'


def get_linesep(node):
"""Get current line separator configuration.
Args:
- `node` -- `Union[parso.python.tree.Module, parso.python.tree.PythonNode, parso.python.tree.PythonLeaf]`,
parso AST
Returns:
- `str` -- line separator
"""
env = os.getenv('POSEUR_LINESEP')
if env is not None:
env_name = env.upper()
if env_name == 'LF':
return '\n'
if env_name == 'CRLF':
return '\r\n'
raise EnvironError('invlid line separator %r' % env)
return guess_linesep(node)


def process_module(node):
"""Walk top nodes of the AST module.
Expand Down Expand Up @@ -556,7 +609,7 @@ def process_module(node):
suffix += bufsuf

if postmt >= 0:
POSEUR_LINESEP = os.getenv('POSEUR_LINESEP', os.linesep)
POSEUR_LINESEP = get_linesep(node)
POSEUR_DECORATOR = os.getenv('POSEUR_DECORATOR', __poseur_decorator__)

middle = POSEUR_LINESEP.join(_decorator) % POSEUR_DECORATOR
Expand Down Expand Up @@ -719,7 +772,7 @@ def poseur(filename):
__archive__ = os.path.join(__cwd__, 'archive')
__poseur_version__ = os.getenv('POSEUR_VERSION', POSEUR_VERSION[-1])
__poseur_encoding__ = os.getenv('POSEUR_ENCODING', LOCALE_ENCODING)
__poseur_linesep__ = os.getenv('POSEUR_LINESEP', os.linesep)
__poseur_linesep__ = os.getenv('POSEUR_LINESEP', 'CRLF' if os.linesep == '\r\n' else 'LF')
__poseur_decorator__ = os.getenv('POSEUR_DECORATOR', '__poseur_decorator')


Expand Down
2 changes: 1 addition & 1 deletion scripts/setup.pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
long_desc = file.read()

# version string
__version__ = '0.3.4'
__version__ = '0.3.5'

# set-up script for pip distribution
setup(
Expand Down
2 changes: 1 addition & 1 deletion scripts/setup.pypitest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
long_desc = file.read()

# version string
__version__ = '0.3.4'
__version__ = '0.3.5'

# set-up script for pip distribution
setup(
Expand Down
2 changes: 1 addition & 1 deletion share/poseur.1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.\" Man page generated from reStructuredText.
.
.TH POSEUR 1 "November 08, 2019" "v0.3.4" ""
.TH POSEUR 1 "November 09, 2019" "v0.3.5" ""
.SH NAME
poseur \- back-port compiler for Python 3.8 positional-only parameter syntax
.
Expand Down
4 changes: 2 additions & 2 deletions share/poseur.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ poseur
back-port compiler for Python 3.8 positional-only parameter syntax
------------------------------------------------------------------

:Version: v0.3.4
:Date: November 08, 2019
:Version: v0.3.5
:Date: November 09, 2019
:Manual section: 1
:Author:
Jarry Shaw, a newbie programmer, is the author, owner and maintainer
Expand Down
4 changes: 2 additions & 2 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
os.environ['POSEUR_DECORATOR'] = '_poseur_decorator'
os.environ['POSEUR_QUIET'] = 'true'
os.environ['POSEUR_ENCODING'] = 'utf-8'
os.environ['POSEUR_LINESEP'] = '\n'
POSEUR_LINESEP = os.environ['POSEUR_LINESEP']
os.environ['POSEUR_LINESEP'] = 'LF'
POSEUR_LINESEP = '\n'


@contextlib.contextmanager
Expand Down

0 comments on commit e57f2b0

Please sign in to comment.