File: filereader.py

package info (click to toggle)
cclib-data 1.6.2-2
  • links: PTS, VCS
  • area: non-free
  • in suites: bookworm, bullseye, sid
  • size: 87,912 kB
  • sloc: python: 16,440; sh: 131; makefile: 79; cpp: 31
file content (41 lines) | stat: -rw-r--r-- 1,185 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# -*- coding: utf-8 -*-
#
# Copyright (c) 2017, the cclib development team
#
# This file is part of cclib (http://cclib.github.io) and is distributed under
# the terms of the BSD 3-Clause License.

"""Generic file reader and related tools"""

from abc import ABCMeta, abstractmethod
from six import add_metaclass


@add_metaclass(ABCMeta)
class Reader:
    """Abstract class for reader objects."""

    def __init__(self, source, *args, **kwargs):
        """Initialize the Reader object.

        This should be called by a subclass in its own __init__ method.

        Inputs:
          source - A single filename, stream [TODO], or list of filenames/streams [TODO].
        """
        if isinstance(source, str):
            self.filename = source
        else:
            raise ValueError

    def parse(self):
        """Read the raw contents of the source into the Reader."""
        # TODO This cannot currently handle streams.
        with open(self.filename) as handle:
            self.filecontents = handle.read()

        return None

    @abstractmethod
    def generate_repr(self):
        """Convert the raw contents of the source into the internal representation."""