Python file wrapper, best design?
Question: I need to parse a domain specific configuration file, but before I begin
pulling the gold out of it, I want to remove the comments.

Once comments are removed, I still want to be able to use things like
`getline()`, `seek()` and `tell()`

(the offsets in seeking due to large chunks removed is fine because all
parsing is encapsulated within a single parsing object)

**Inherit from`file` ?:**

My first thought was to create an object like `MyFile(file)`, and override
`__init__` to create a `self.content` string, then override everything with
file access... but it looked far too tricky (I saw many forum posts of people
having trouble trying this)

**Delegate to`file` object ?:**

Create a `MyFile(file)` class that creates a `file` class, and delegate calls
from the `MyFile` instance to the internal `file` instance. This makes sense
if the calls to `MyFile` expect a return beyond the comment find/replace
search parameters... for example: `getline()` would be fine for single-line
comments, but for a file where the 1st 3 lines form a single block comment,
`getline()` `getline()` isn't going to work without a lot of messy code (not
to mention keeping track of `seek()` offsets)

**Re-create in temporary file?:**

Finally, I felt like I was taking the lazy option in parsing the whole file,
then re-saving it as a temporary file... then returning a file-handle to that
(demo code below)

    
    
    import re
    import tempfile
    
    FILE_NAME = 'some_file.ldf'
    
    # ideal case
    fh = open(FILE_NAME)
    
    # my example
    
    def getHandle(self, filename, mode='r'):
        # ----- Remove comments -----
        with open(filename, mode=mode) as fh:
            content = fh.read()
        # remove /* ... */ comment blocks
        content = re.sub(re.compile(r'/\*.*?\*/', re.DOTALL | re.MULTILINE), '', content, re.DOTALL | re.MULTILINE)
        # remove // ... line comments
        content = re.sub(r'//.*', '', content)
    
        # ----- Write to Temporary File -----
        fh = tempfile.TemporaryFile()
        fh.write(content)
        fh.seek(0)
    
    fh = getHandle(FILE_NAME)
    

**Better Design?:**

Does anybody have a better design for this?... or even a completely different
angle on this sort of problem.


Answer: Honestly it sounds like your DSL parser may be the problem here. Basically if
you've designed a DSL, it should include the recognition of comments and just
ignore them.

Taking the route of writing this as a file filter is an interesting but
complex for the sake of complexity - it's just covering up for the fact that
your DSL parser is incomplete.

Depending on how you've written your parser this may be easy, or not so easy
to fix. But, take this pyparsing example of a verilog parser. It simply has
the definition of cStyleComment and tells the parser to just ignore them
completely. No muss, no fuss.

<http://pyparsing.wikispaces.com/file/view/verilogParse.py/241112725/verilogParse.py>

