Repository: dbrgn/fahrplan (language: python)

Issue:
IndexError in parser
I forgot how to talk to `fahrplan` properly so I tried something and was greeted with a stacktrace:

```
$ fahrplan rapperswil 'rüti, steinstrasse' 
Traceback (most recent call last):
  File "/usr/bin/fahrplan", line 11, in <module>
    load_entry_point('fahrplan==1.0.0', 'console_scripts', 'fahrplan')()
  File "/usr/lib/python3.7/site-packages/fahrplan/main.py", line 94, in main
    args, language = parse_input(options.request)
  File "/usr/lib/python3.7/site-packages/fahrplan/parser.py", line 248, in parse_input
    data, language = _process_tokens(tokens)
  File "/usr/lib/python3.7/site-packages/fahrplan/parser.py", line 106, in _process_tokens
    process_stack()
  File "/usr/lib/python3.7/site-packages/fahrplan/parser.py", line 93, in process_stack
    key = keywords.get(stack[0])
IndexError: list index out of range
```
IndexError in parser
I forgot how to talk to `fahrplan` properly so I tried something and was greeted with a stacktrace:

```
$ fahrplan rapperswil 'rüti, steinstrasse' 
Traceback (most recent call last):
  File "/usr/bin/fahrplan", line 11, in <module>
    load_entry_point('fahrplan==1.0.0', 'console_scripts', 'fahrplan')()
  File "/usr/lib/python3.7/site-packages/fahrplan/main.py", line 94, in main
    args, language = parse_input(options.request)
  File "/usr/lib/python3.7/site-packages/fahrplan/parser.py", line 248, in parse_input
    data, language = _process_tokens(tokens)
  File "/usr/lib/python3.7/site-packages/fahrplan/parser.py", line 106, in _process_tokens
    process_stack()
  File "/usr/lib/python3.7/site-packages/fahrplan/parser.py", line 93, in process_stack
    key = keywords.get(stack[0])
IndexError: list index out of range
```


Discussion / hints:
Good find, fixes welcome :slightly_smiling_face: 
Good find, fixes welcome :slightly_smiling_face: 

Fix patch:
diff --git a/fahrplan/parser.py b/fahrplan/parser.py
index 025fce9..f783ae9 100644
--- a/fahrplan/parser.py
+++ b/fahrplan/parser.py
@@ -99,6 +99,10 @@ def process_stack():
         elif not stack:
             continue
         stack.append(token)
+
+    if not stack:
+        return {}, None
+
     process_stack()
 
     # Validate data
@@ -243,7 +247,11 @@ def parse_input(tokens):
     # Process tokens, get data dict and language
     data, language = _process_tokens(tokens)
     if data == {}:
+        if len(tokens) == 2:
+            # Fallback when e.g. doing "fahrplan zurich basel"
+            return {'from': tokens[0], 'to': tokens[1]}, 'en'
         return data, language
+
     try:
         kws = keywords[language]
     except IndexError:


Test patch:
diff --git a/fahrplan/tests/test.py b/fahrplan/tests/test.py
index da67378..c485bd9 100644
--- a/fahrplan/tests/test.py
+++ b/fahrplan/tests/test.py
@@ -104,6 +104,12 @@ def testValidArgumentsFr(self):
         self.assertEqual(self.valid_expected_result, data)
         self.assertEqual('fr', language)
 
+    def testTwoArguments(self):
+        tokens = 'Zürich Basel'.split()
+        data, language = parser.parse_input(tokens)
+        self.assertEqual({'from': 'Zürich', 'to': 'Basel'}, data)
+        self.assertEqual('en', language)
+
     def testNotEnoughArgument(self):
         tokens = 'from basel via bern'.split()
         self.assertRaises(ValueError, parser.parse_input, tokens)