pedroburon / dotenv

python .env file handler
MIT License
36 stars 10 forks source link

Saxbophone/8 comments #9

Closed saxbophone closed 9 years ago

saxbophone commented 9 years ago

Implements the stripping out of bash-style # comments. Lines that start with # will be ignored while lines that define a variable and also have a comment will still work, everything after the comment is ignored.

saxbophone commented 9 years ago

Whoops, I've just realised this might break if the user wants to specify a variable with a # in it...

msabramo commented 9 years ago

I don't think it's valid to have a variable with a # in it:

monkey@docker:~$ export FOO=123
monkey@docker:~$ export FOO#BAR=123
-bash: export: `FOO#BAR=123': not a valid identifier
saxbophone commented 9 years ago

That's true, however it is valid to define a variable which itself contains a # (in quotes):

$ export FOO='#'
$ echo $FOO
#

I think my code will remove the # and make turn the first line into: export FOO=' which would cause an error.

pedroburon commented 9 years ago

Maybe a space character should be expected before inline comments.

FOO=# #comment

>>> print os.environ['FOO']
'#'
msabramo commented 9 years ago

@saxbophone: Good point.

@pedroburon: That seem reasonable.

saxbophone commented 9 years ago

Yes that solution should work, I will have to modify it to not remove # comments that are inside quote marks.

msabramo commented 9 years ago

Or what if the # only counts as a comment if it's at the beginning of the line? E.g.:

if line.startswith('#'):
    return {}
msabramo commented 9 years ago

Though if someone is indenting, the above code would not consider the line below to be a comment:

             # an indented comment

so maybe instead:

if line.strip().startswith('#'):
    return {}
saxbophone commented 9 years ago

Yes, that's exactly what I was thinking! Inline comments are a bit trickier as we'll need to make sure that it doesn't remove # symbols contained in quotes but I think I can sort that.

saxbophone commented 9 years ago

Okay so what I've done here is changed it to ignore comment symbols that are inside quote marks (for using in variable values). I have also made it ignore blank lines and allow for indented comments. Also see the new tests to confirm this behaviour :smile:

saxbophone commented 9 years ago

Thanks @pedroburon :smile: