Skip to content

Commit

Permalink
Switch codebase to Colorama and cleanup
Browse files Browse the repository at this point in the history
Includes some changes which were left over after Russell left and not comitted.
  • Loading branch information
jrha committed Sep 11, 2018
1 parent 71fea67 commit f2937db
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 64 deletions.
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ python:
# Assume bundler is being used, therefore
# the `install` step will run `bundle install` by default.
script:
- ./pan_indent_checker.py Example-Before.pan --output Travis-Test.pan; diff Example-After.pan Travis-Test.pan
- ./pan_indent_checker.py Example-After.pan
- (./pan_indent_checker.py Example-Before.pan && exit 1 || exit 0)
- ./pan_indent_checker.py Example-After.pan --debug
- ./pan_indent_checker.py reformat Example-Before.pan --output Travis-Test.pan; diff Example-After.pan Travis-Test.pan
- ./pan_indent_checker.py check Example-After.pan
- (./pan_indent_checker.py check Example-Before.pan && exit 1 || exit 0)
- ./pan_indent_checker.py check Example-After.pan --debug

cache: bundler

Expand Down
122 changes: 62 additions & 60 deletions pan_indent_checker.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python

# Copyright 2018 Science & Technology Facilities Council
#
Expand All @@ -15,11 +15,20 @@
# limitations under the License.

import argparse
import os
import re
import sys

from colorama import Fore, Style, init as colorama_init


# Command line Debugging
DEBUG_LINE = Fore.CYAN + "%-16s " + Fore.MAGENTA + "|" + Fore.YELLOW + " %s" + Fore.RESET
DEBUG_DIVIDER = Style.DIM + '-' * 100 + Style.NORMAL

# Indentation to use when formatting
INDENT = ' '


def supports_color():
"""
Expand All @@ -35,110 +44,103 @@ def supports_color():
return True


# Command line Debugging
DEBUGNOCOLOUR = "%-16s | %s"
DEBUGCOLOR = "\033[36m%-16s \033[35m|\033[0m %s"
if supports_color():
DEBUG_LINE = DEBUGCOLOR
if not supports_color():
DEBUG_LINE = DEBUGNOCOLOUR
# Indent space to use to format the code
INDENT = ' '
def _print_debug(k, v):
print(DEBUG_DIVIDER)
print(DEBUG_LINE % (k, v))


def main():
failedlines = False
# Arg parser settings for commandlines
failedlines = 0

# Arg parser settings for commandlines
parser = argparse.ArgumentParser(description='Indent Checker')
parser.add_argument('action', type=str, choices=['check', 'reformat'], help='Action to perform')
parser.add_argument('input', type=str, help='Input File')
parser.add_argument('-o', '--output', type=str, help='Output File')
parser.add_argument('--debug', action='store_true', help='Enable debug logging.')
parser.add_argument('--diff', action='store_true', help='Enable diff showing.')
parser.add_argument('--debug', action='store_true', help='Enable debug logging')

args = parser.parse_args()
file_output = None
if args.output:
file_output = open(args.output, 'w+')
elif not args.debug:
else:
file_output = sys.stdout
indent_level = 0

colorama_init(strip=(not supports_color()))

with open(args.input, 'r+') as file_input:
for line_number, line in enumerate(file_input):
for line_number, line_raw in enumerate(file_input):

# Command line Debugging
# Command line Debugging
if args.debug:
DEBUGLINENOCOLOUR = '-' * 100
DEBUGLINECOLOUR = '\033[1;32m' + DEBUGLINENOCOLOUR + '\033[0m'
if supports_color():
print(DEBUGLINECOLOUR)
if not supports_color():
print(DEBUGLINENOCOLOUR)

def _print_debug(k, v, newline=True):
sys.stdout.write(DEBUG_LINE % (k, v))
if newline:
sys.stdout.write("\n")

else:
def _print_debug(k, v, newline=True):
pass

_print_debug("Line Number ", line_number)
_print_debug("Indent Level", indent_level)
_print_debug("Unedited line", line.rstrip())
oldline = line.rstrip()
# Strips line and resets indent_change
line = line.strip()
_print_debug("Line Number ", line_number)
_print_debug("Indent Level", indent_level)
_print_debug("Unedited line", line_raw.rstrip())

line_old = line_raw.rstrip()
line_stripped = line_raw.strip()
indent_change = 0

# Used to know when to send a line forward or backwards for formatting
cleanline = re.sub(r'(#.*$)', ' ', line)
# Determine whether to send the line forwards or backwards for formatting
cleanline = re.sub(r'(#.*$)', ' ', line_stripped)
startmatch = re.findall(r'[{(]', cleanline)
endmatch = re.findall(r'[})]', cleanline)
curlyout = re.search(r'(^[)}])(.*)([{(])', cleanline)

# This section is to set the direction to send the line
# Set the direction to send the line
if len(endmatch) > len(startmatch):
indent_change = -1

if len(startmatch) > len(endmatch):
indent_change = 1

# Command line Debugging
_print_debug("Indent Change", indent_change)
if args.debug:
_print_debug("Indent Change", indent_change)

# This is the section that applies the change
# Apply indent reductions before printing
if indent_change < 0:
indent_level += indent_change

if curlyout:
indent_level += -1

linenew = (INDENT * indent_level) + line
linenew = linenew.rstrip()
_print_debug("Edited Line", linenew)
if linenew != oldline:
failedlines = True
if args.diff:
print("")
print("Line %-5d:%s" % (line_number, line))
print("should be :%s" % linenew)

# Writes to file
if file_output:
file_output.write(linenew + "\n")
line_new = (INDENT * indent_level) + line_stripped
line_new = line_new.rstrip()

if args.debug:
_print_debug("Edited Line", line_new)

# Write output
if args.action == 'reformat' and file_output:
file_output.write(line_new + "\n")
file_output.flush()

elif args.action == 'check':
if line_new != line_old:
failedlines += 1
if args.action == 'check':
print(("%3d:" % line_number) + Fore.RED + ("%s" % line_old) + Fore.RESET)
print(("%3d:" % line_number) + Fore.GREEN + ("%s" % line_new) + Fore.RESET)
print("")

# Apply indent increases after printing
if indent_change > 0:
indent_level += indent_change

if curlyout:
indent_level += 1

# Command line Debugging
_print_debug("Ending Indent", indent_level)
if args.debug:
_print_debug("Ending Indent", indent_level)

if failedlines:
if args.action == 'check':
print("Found %d lines with incorrect indentation." % failedlines)
sys.exit(1)
else:
if args.action == 'check':
print("No problems found.")
sys.exit(0)


Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
colorama

0 comments on commit f2937db

Please sign in to comment.