Skip to content

Commit

Permalink
New distribution [0.4.0]
Browse files Browse the repository at this point in the history
 * revised `--linesep` CLI option
 * revised linesep determination
 * bumping version to 0.4.0
  • Loading branch information
JarryShaw committed Nov 9, 2019
1 parent 161a79d commit 7bf867e
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 73 deletions.
97 changes: 57 additions & 40 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 46 additions & 29 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.5'
__version__ = '0.4.0'

# from configparser
BOOLEAN_STATES = {'1': True, '0': False,
Expand Down Expand Up @@ -66,12 +66,12 @@ class EnvironError(EnvironmentError):


def predicate(filename): # pragma: no cover
if os.path.basename(filename) == 'poseur':
if os.path.basename(filename) == 'poseur.py':
return True
return ROOT in os.path.realpath(filename)


tbtrim.set_trim_rule(predicate, strict=True, target=ConvertError)
tbtrim.set_trim_rule(predicate, strict=True, target=(ConvertError, EnvironError))

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

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


def guess_linesep(node):
def guess_linesep(node): # pylint: disable=inconsistent-return-statements
"""Guess line separator based on source code.
Args:
Expand All @@ -548,37 +548,43 @@ def guess_linesep(node):
root = node.get_root_node()
code = root.get_code()

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

sort = sorted(pool, key=lambda k: pool[k])
if pool[sort[0]] == pool[sort[1]]:
return get_linesep()
return sort[0]

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
def get_linesep():
"""Get current line separator configuration.
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)
env = os.getenv('POSEUR_LINESEP', os.linesep)
env_name = env.upper()
if env_name == 'CR':
return '\r'
if env_name == 'CRLF':
return '\r\n'
if env_name == 'LF':
return '\n'
if env in ['\r', '\r\n', '\n']:
return env
raise EnvironError('invlid line separator %r' % env)


def process_module(node):
Expand Down Expand Up @@ -609,7 +615,7 @@ def process_module(node):
suffix += bufsuf

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

middle = POSEUR_LINESEP.join(_decorator) % POSEUR_DECORATOR
Expand Down Expand Up @@ -772,7 +778,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', 'CRLF' if os.linesep == '\r\n' else 'LF')
__poseur_linesep__ = os.getenv('POSEUR_LINESEP', os.linesep)
__poseur_decorator__ = os.getenv('POSEUR_DECORATOR', '__poseur_decorator')


Expand Down Expand Up @@ -879,7 +885,6 @@ def main(argv=None):
ARCHIVE = args.archive_path
os.environ['POSEUR_VERSION'] = args.python
os.environ['POSEUR_ENCODING'] = args.encoding
os.environ['POSEUR_LINESEP'] = args.linesep
os.environ['POSEUR_DECORATOR'] = args.decorator
POSEUR_QUIET = os.getenv('POSEUR_QUIET')
os.environ['POSEUR_QUIET'] = '1' if args.quiet else ('0' if POSEUR_QUIET is None else POSEUR_QUIET)
Expand All @@ -888,6 +893,18 @@ def main(argv=None):
POSEUR_LINTING = os.getenv('POSEUR_LINTING')
os.environ['POSEUR_LINTING'] = '1' if args.linting else ('0' if POSEUR_LINTING is None else POSEUR_LINTING)

linesep = args.linesep.upper()
if linesep == 'CR':
os.environ['POSEUR_LINESEP'] = '\r'
elif linesep == 'CRLF':
os.environ['POSEUR_LINESEP'] = '\r\n'
elif linesep == 'LF':
os.environ['POSEUR_LINESEP'] = '\n'
elif args.linesep in ['\r', '\r\n', '\n']:
os.environ['POSEUR_LINESEP'] = args.linesep
else:
raise EnvironError('invalid line separator %r' % args.linesep)

# make archive directory
if args.archive: # pragma: no cover
os.makedirs(ARCHIVE, exist_ok=True)
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.5'
__version__ = '0.4.0'

# 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.5'
__version__ = '0.4.0'

# 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 09, 2019" "v0.3.5" ""
.TH POSEUR 1 "November 09, 2019" "v0.4.0" ""
.SH NAME
poseur \- back-port compiler for Python 3.8 positional-only parameter syntax
.
Expand Down
2 changes: 1 addition & 1 deletion share/poseur.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ poseur
back-port compiler for Python 3.8 positional-only parameter syntax
------------------------------------------------------------------

:Version: v0.3.5
:Version: v0.4.0
:Date: November 09, 2019
:Manual section: 1
:Author:
Expand Down

0 comments on commit 7bf867e

Please sign in to comment.