# Path: pysmi/writer/base.py
# class AbstractWriter(object):
#     def setOptions(self, **kwargs):
#         for k in kwargs:
#             setattr(self, k, kwargs[k])
#         return self
# 
#     def putData(self, mibname, data, comments=(), dryRun=False):
#         raise NotImplementedError()
# 
#     def getData(self, filename):
#         raise NotImplementedError()
# 
# Path: pysmi/compat.py
# def encode(s):
#     if isinstance(s, str):
#         s = s.encode('utf-8', 'ignore')
#     return s
# 
# def decode(s):
#     if isinstance(s, bytes):
#         s = s.decode('utf-8', 'ignore')
#     return s
# 
# Path: pysmi/debug.py
# class Printer(object):
#     class NullHandler(logging.Handler):
# class Debug(object):
#     def __init__(self, logger=None, handler=None, formatter=None):
#     def __call__(self, msg):
#     def __str__(self):
#     def getCurrentLogger(self):
#         def emit(self, record):
#     def __init__(self, *flags, **options):
#     def __str__(self):
#     def __call__(self, msg):
#     def __and__(self, flag):
#     def __rand__(self, flag):
#     def getCurrentPrinter(self):
#     def getCurrentLogger(self):
# def setLogger(l):
# 
# Path: pysmi/error.py
# class PySmiError(Exception):
# class PySmiLexerError(PySmiError):
# class PySmiParserError(PySmiLexerError):
# class PySmiSyntaxError(PySmiParserError):
# class PySmiSearcherError(PySmiError):
# class PySmiFileNotModifiedError(PySmiSearcherError):
# class PySmiFileNotFoundError(PySmiSearcherError):
# class PySmiReaderError(PySmiError):
# class PySmiReaderFileNotModifiedError(PySmiReaderError):
# class PySmiReaderFileNotFoundError(PySmiReaderError):
# class PySmiCodegenError(PySmiError):
# class PySmiSemanticError(PySmiCodegenError):
# class PySmiWriterError(PySmiError):
#     def __init__(self, *args, **kwargs):
#     def __repr__(self):
#     def __str__(self):
#     def __str__(self):


import os
import sys
import tempfile
from pysmi.writer.base import AbstractWriter
from pysmi.compat import encode, decode
from pysmi import debug
from pysmi import error
        if not os.path.exists(self._path):
            try:
                os.makedirs(self._path)

            except OSError:
                raise error.PySmiWriterError(
                    'failure creating destination directory %s: %s' % (self._path, sys.exc_info()[1]), writer=self)

        if comments:
            data = '#\n' + ''.join(['# %s\n' % x for x in comments]) + '#\n' + data

        filename = os.path.join(self._path, decode(mibname)) + self.suffix

        tfile = None

        try:
            fd, tfile = tempfile.mkstemp(dir=self._path)
            os.write(fd, encode(data))
            os.close(fd)
            os.rename(tfile, filename)

        except (OSError, IOError, UnicodeEncodeError):
            exc = sys.exc_info()
            if tfile:
                try:
                    os.unlink(tfile)

                except OSError:
                    pass


# Path: pysmi/writer/base.py
# class AbstractWriter(object):
#     def setOptions(self, **kwargs):
#         for k in kwargs:
#             setattr(self, k, kwargs[k])
#         return self
# 
#     def putData(self, mibname, data, comments=(), dryRun=False):
#         raise NotImplementedError()
# 
#     def getData(self, filename):
#         raise NotImplementedError()
# 
# Path: pysmi/compat.py
# def encode(s):
#     if isinstance(s, str):
#         s = s.encode('utf-8', 'ignore')
#     return s
# 
# def decode(s):
#     if isinstance(s, bytes):
#         s = s.decode('utf-8', 'ignore')
#     return s
# 
# Path: pysmi/debug.py
# class Printer(object):
#     class NullHandler(logging.Handler):
# class Debug(object):
#     def __init__(self, logger=None, handler=None, formatter=None):
#     def __call__(self, msg):
#     def __str__(self):
#     def getCurrentLogger(self):
#         def emit(self, record):
#     def __init__(self, *flags, **options):
#     def __str__(self):
#     def __call__(self, msg):
#     def __and__(self, flag):
#     def __rand__(self, flag):
#     def getCurrentPrinter(self):
#     def getCurrentLogger(self):
# def setLogger(l):
# 
# Path: pysmi/error.py
# class PySmiError(Exception):
# class PySmiLexerError(PySmiError):
# class PySmiParserError(PySmiLexerError):
# class PySmiSyntaxError(PySmiParserError):
# class PySmiSearcherError(PySmiError):
# class PySmiFileNotModifiedError(PySmiSearcherError):
# class PySmiFileNotFoundError(PySmiSearcherError):
# class PySmiReaderError(PySmiError):
# class PySmiReaderFileNotModifiedError(PySmiReaderError):
# class PySmiReaderFileNotFoundError(PySmiReaderError):
# class PySmiCodegenError(PySmiError):
# class PySmiSemanticError(PySmiCodegenError):
# class PySmiWriterError(PySmiError):
#     def __init__(self, *args, **kwargs):
#     def __repr__(self):
#     def __str__(self):
#     def __str__(self):


# Path: pysmi/writer/localfile.py
import os
import sys
import tempfile
from pysmi.writer.base import AbstractWriter
from pysmi.compat import encode, decode
from pysmi import debug
from pysmi import error

        if not os.path.exists(self._path):
            try:
                os.makedirs(self._path)

            except OSError:
                raise error.PySmiWriterError(
                    'failure creating destination directory %s: %s' % (self._path, sys.exc_info()[1]), writer=self)

        if comments:
            data = '#\n' + ''.join(['# %s\n' % x for x in comments]) + '#\n' + data

        filename = os.path.join(self._path, decode(mibname)) + self.suffix

        tfile = None

        try:
            fd, tfile = tempfile.mkstemp(dir=self._path)
            os.write(fd, encode(data))
            os.close(fd)
            os.rename(tfile, filename)

        except (OSError, IOError, UnicodeEncodeError):
            exc = sys.exc_info()
            if tfile:
                try:
                    os.unlink(tfile)

                except OSError:
                    pass


            raise error.PySmiWriterError('failure writing file %s: %s' % (filename, exc[1]), file=filename, writer=self)