Skip to content

Commit

Permalink
cli: Stricter and safer config parsing.
Browse files Browse the repository at this point in the history
Resolves chrippa#432.
  • Loading branch information
chrippa authored and cheah committed Aug 17, 2014
1 parent 62bfac6 commit 256eeb9
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/livestreamer_cli/argparser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import argparse
import re

from string import printable
from textwrap import dedent

from .constants import (
Expand All @@ -14,23 +15,34 @@
(?P<modifier>[Kk]|[Mm])?
(?:[Bb])?
""", re.VERBOSE)
_printable_re = re.compile("[{0}]".format(printable))
_valid_option_start_re = re.compile("^[A-z]")
_valid_option_char_re = re.compile("[A-z\-]")


class ArgumentParser(argparse.ArgumentParser):
def sanitize_option(self, option):
return "".join(filter(_valid_option_char_re.match, option))

def convert_arg_line_to_args(self, line):
if len(line) == 0:
# Strip any non-printable characters that might be in the
# beginning of the line (e.g. Unicode BOM marker).
match = _printable_re.search(line)
if not match:
return
line = line[match.start():]

if line[0] == "#":
# Skip lines that do not start with a valid option character (e.g. comments)
if not _valid_option_start_re.match(line):
return

split = line.find("=")
if split > 0:
key = line[:split].strip()
val = line[split+1:].strip()
yield "--%s=%s" % (key, val)
option = line[:split].strip()
value = line[split+1:].strip()
yield "--{0}={1}".format(self.sanitize_option(option), value)
else:
yield "--%s" % line
yield "--{0}".format(self.sanitize_option(line))


class HelpFormatter(argparse.RawDescriptionHelpFormatter):
Expand Down

0 comments on commit 256eeb9

Please sign in to comment.