import os

def compile_class_from_file(path, filename, system_class, universe):
    return _SourcecodeCompiler().compile(path, filename, system_class, universe)

def compile_class_from_string(stmt, system_class, universe):
    return _SourcecodeCompiler().compile_class_string(stmt, system_class, universe)

class _SourcecodeCompiler(object):
    
    def __init__(self):
        self._parser = None
    
    def compile(self, path, filename, system_class, universe):
        result = system_class
        
        fname = path + os.pathsep + filename + ".som"

        self._parser = Parser(FileReader(fname), universe)

        result = self._compile(system_class, universe)

        cname = result.get_name()
        cnameC = cname.get_string()

        if filename != cnameC:
            raise ValueError("File name " + filename + " does not match class name " + cnameC)
    
        return result

    def compile_class_string(self, stream, system_class, universe):
        self._parser = Parser(StringReader(stream), universe)

        result = self._compile(system_class, universe)
        return result

    def _compile(self, system_class, universe):
        cgc = ClassGenerationContext(universe)

        result = system_class
        self._parser.classdef(cgc)

        if not system_class:
            result = cgc.assemble()
        else:
            cgc.assemble_system_class(result)

        return result

import os
from StringIO import StringIO

def compile_class_from_file(path, filename, system_class, universe):
    return _SourcecodeCompiler().compile(path, filename, system_class, universe)

def compile_class_from_string(stmt, system_class, universe):
    return _SourcecodeCompiler().compile_class_string(stmt, system_class, universe)

class _SourcecodeCompiler(object):
    
    def __init__(self):
        self._parser = None
    
    def compile(self, path, filename, system_class, universe):
        fname = path + os.sep + filename + ".som"
        
        with open(fname, "r") as input_file:
            self._parser = Parser(input_file, universe)
            result = self._compile(system_class, universe)

        cname = result.get_name()
        cnameC = cname.get_string()

        if filename != cnameC:
            raise ValueError("File name " + filename + " does not match class name " + cnameC)
    
        return result

    def compile_class_string(self, stream, system_class, universe):
        self._parser = Parser(StringIO(stream), universe)

        result = self._compile(system_class, universe)
        return result

    def _compile(self, system_class, universe):
        cgc = ClassGenerationContext(universe)

        result = system_class
        self._parser.classdef(cgc)

        if not system_class:
            result = cgc.assemble()
        else:
            cgc.assemble_system_class(result)

        return result

Use Python file objects directly as input
Use Python file objects directly as input

- fix wrong separator between path and filename

Signed-off-by: Stefan Marr <46f1a0bd5592a2f9244ca321b129902a06b53e03@stefan-marr.de>

  import os
+ from StringIO import StringIO
  
  def compile_class_from_file(path, filename, system_class, universe):
      return _SourcecodeCompiler().compile(path, filename, system_class, universe)
  
  def compile_class_from_string(stmt, system_class, universe):
      return _SourcecodeCompiler().compile_class_string(stmt, system_class, universe)
  
  class _SourcecodeCompiler(object):
      
      def __init__(self):
          self._parser = None
      
      def compile(self, path, filename, system_class, universe):
-         result = system_class
+         fname = path + os.sep + filename + ".som"
          
+         with open(fname, "r") as input_file:
-         fname = path + os.pathsep + filename + ".som"
- 
-         self._parser = Parser(FileReader(fname), universe)
+             self._parser = Parser(input_file, universe)
- 
-         result = self._compile(system_class, universe)
+             result = self._compile(system_class, universe)
  
          cname = result.get_name()
          cnameC = cname.get_string()
  
          if filename != cnameC:
              raise ValueError("File name " + filename + " does not match class name " + cnameC)
      
          return result
  
      def compile_class_string(self, stream, system_class, universe):
-         self._parser = Parser(StringReader(stream), universe)
+         self._parser = Parser(StringIO(stream), universe)
  
          result = self._compile(system_class, universe)
          return result
  
      def _compile(self, system_class, universe):
          cgc = ClassGenerationContext(universe)
  
          result = system_class
          self._parser.classdef(cgc)
  
          if not system_class:
              result = cgc.assemble()
          else:
              cgc.assemble_system_class(result)
  
          return result
  
Use Python file objects directly as input
## Code Before:
import os

def compile_class_from_file(path, filename, system_class, universe):
    return _SourcecodeCompiler().compile(path, filename, system_class, universe)

def compile_class_from_string(stmt, system_class, universe):
    return _SourcecodeCompiler().compile_class_string(stmt, system_class, universe)

class _SourcecodeCompiler(object):
    
    def __init__(self):
        self._parser = None
    
    def compile(self, path, filename, system_class, universe):
        result = system_class
        
        fname = path + os.pathsep + filename + ".som"

        self._parser = Parser(FileReader(fname), universe)

        result = self._compile(system_class, universe)

        cname = result.get_name()
        cnameC = cname.get_string()

        if filename != cnameC:
            raise ValueError("File name " + filename + " does not match class name " + cnameC)
    
        return result

    def compile_class_string(self, stream, system_class, universe):
        self._parser = Parser(StringReader(stream), universe)

        result = self._compile(system_class, universe)
        return result

    def _compile(self, system_class, universe):
        cgc = ClassGenerationContext(universe)

        result = system_class
        self._parser.classdef(cgc)

        if not system_class:
            result = cgc.assemble()
        else:
            cgc.assemble_system_class(result)

        return result

## Instruction:
Use Python file objects directly as input
## Code After:
import os
from StringIO import StringIO

def compile_class_from_file(path, filename, system_class, universe):
    return _SourcecodeCompiler().compile(path, filename, system_class, universe)

def compile_class_from_string(stmt, system_class, universe):
    return _SourcecodeCompiler().compile_class_string(stmt, system_class, universe)

class _SourcecodeCompiler(object):
    
    def __init__(self):
        self._parser = None
    
    def compile(self, path, filename, system_class, universe):
        fname = path + os.sep + filename + ".som"
        
        with open(fname, "r") as input_file:
            self._parser = Parser(input_file, universe)
            result = self._compile(system_class, universe)

        cname = result.get_name()
        cnameC = cname.get_string()

        if filename != cnameC:
            raise ValueError("File name " + filename + " does not match class name " + cnameC)
    
        return result

    def compile_class_string(self, stream, system_class, universe):
        self._parser = Parser(StringIO(stream), universe)

        result = self._compile(system_class, universe)
        return result

    def _compile(self, system_class, universe):
        cgc = ClassGenerationContext(universe)

        result = system_class
        self._parser.classdef(cgc)

        if not system_class:
            result = cgc.assemble()
        else:
            cgc.assemble_system_class(result)

        return result
