Skip to content

Commit

Permalink
Further changes for migrating from py2 to just py3.
Browse files Browse the repository at this point in the history
Trying to restore windows after having encoding issues.
  • Loading branch information
jsa34 committed Sep 20, 2024
1 parent d08fdfe commit 66569e7
Show file tree
Hide file tree
Showing 19 changed files with 123 additions and 112 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
matrix:
os:
- ubuntu-latest
# - windows-latest
- windows-latest
- macos-13
python-version:
- '3.12'
Expand Down
3 changes: 1 addition & 2 deletions python/gherkin-python.razor
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@
token.detach
expected_tokens = ["@Raw(string.Join("\", \"", expectedTokens))"]
error = UnexpectedEOFException(token, expected_tokens, state_comment) if token.eof() else UnexpectedTokenException(token, expected_tokens, state_comment)
if (self.stop_at_first_error):
if self.stop_at_first_error:
raise error
self.add_error(context, error)
return @state.Id</text>}
@helper MatchToken(TokenType tokenType)
{<text>match_@(tokenType)(context, token)</text>}
# This file is generated. Do not edit! Edit gherkin-python.razor instead.
import sys
from collections import deque
from .ast_builder import AstBuilder
from .token_matcher import TokenMatcher
Expand Down
4 changes: 4 additions & 0 deletions python/gherkin/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
from gherkin.stream.gherkin_events import GherkinEvents
from gherkin.stream.source_events import SourceEvents

# Ensure UTF-8 encoding for stdout (needed on Windows where the default may not be UTF-8)
sys.stdout = open(sys.stdout.fileno(), mode='w', encoding='utf-8', buffering=1)


parser = OptionParser()
parser.add_option("--no-source", action="store_false", dest="print_source", default=True, help="don't print source events")
parser.add_option("--no-ast", action="store_false", dest="print_ast", default=True, help="don't print ast events")
Expand Down
19 changes: 12 additions & 7 deletions python/gherkin/ast_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ def get_result(self):
def current_node(self):
return self.stack[-1]

def get_location(self, token, column=None):
@staticmethod
def get_location(token, column=None):
return (token.location if not column else
{'line': token.location['line'], 'column': column})

Expand All @@ -61,7 +62,8 @@ def get_table_rows(self, node):
self.ensure_cell_count(rows)
return rows

def ensure_cell_count(self, rows):
@staticmethod
def ensure_cell_count(rows):
if not rows:
return

Expand All @@ -76,10 +78,12 @@ def get_cells(self, table_row_token):
{'location': self.get_location(table_row_token, cell_item['column']),
'value': cell_item['text']}) for cell_item in table_row_token.matched_items]

def get_description(self, node):
@staticmethod
def get_description(node):
return node.get_single('Description', '')

def get_steps(self, node):
@staticmethod
def get_steps(node):
return node.get_items('Step')

def transform_node(self, node):
Expand Down Expand Up @@ -194,7 +198,7 @@ def transform_node(self, node):

children = []
background = node.get_single('Background')
if (background):
if background:
children.append({'background': background})
children = children + [{'scenario': i} for i in node.get_items('ScenarioDefinition')]
description = self.get_description(header)
Expand All @@ -220,7 +224,7 @@ def transform_node(self, node):

children = []
background = node.get_single('Background')
if (background):
if background:
children.append({'background': background})
children = children + [{'scenario': i} for i in node.get_items('ScenarioDefinition')]
children = children + [{'rule': i} for i in node.get_items('Rule')]
Expand All @@ -246,5 +250,6 @@ def transform_node(self, node):
else:
return node

def reject_nones(self, values):
@staticmethod
def reject_nones(values):
return {k: v for k, v in values.items() if v is not None}
1 change: 0 additions & 1 deletion python/gherkin/dialect.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import io
import os
import json

Expand Down
5 changes: 3 additions & 2 deletions python/gherkin/gherkin_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def table_cells(self):
cells.append({'column': col + self.indent + cell_indent, 'text': re.sub(r"[^\S\n]*$", "", lstripped_cell, flags=re.U)})
return cells

def split_table_cells(self, row):
@staticmethod
def split_table_cells(row):
"""
An iterator returning all the table cells in a row with their positions,
accounting for escaping.
Expand All @@ -55,7 +56,7 @@ def split_table_cells(self, row):
# First cell (content before the first |) is skipped
first_cell = False
else:
yield (cell, start_col)
yield cell, start_col
cell = ''
start_col = col + 1
elif char == '\\':
Expand Down
11 changes: 6 additions & 5 deletions python/gherkin/inout.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ def __init__(self, print_source, print_ast, print_pickles):
def process(self, input, output):
line = input.readline().rstrip()
event = json.loads(line)
if (event['type'] == 'source'):
if event['type'] == 'source':
uri = event['uri']
source = event['data']
token_scanner = TokenScanner(source)

try:
gherkin_document = self.parser.parse(token_scanner)
if (self.print_source):
if self.print_source:
print(line, file=output)
if (self.print_ast):
if self.print_ast:
print(json.dumps(gherkin_document), file=output)
if (self.print_pickles):
if self.print_pickles:
pickles = compile(gherkin_document, uri)
for pickle in pickles:
print(json.dumps(pickle), file=output)
Expand All @@ -35,7 +35,8 @@ def process(self, input, output):
except ParserException as e:
self.print_errors(output, [e], uri)

def print_errors(self, output, errors, uri):
@staticmethod
def print_errors(output, errors, uri):
for error in errors:
attachment = {
'type': "attachment",
Expand Down
Loading

0 comments on commit 66569e7

Please sign in to comment.