"Fossies" - the Fresh Open Source Software Archive

Member "sk1-2.0rc4/src/uc2/formats/generic_filters.py" (25 May 2019, 7101 Bytes) of package /linux/misc/sk1-2.0rc4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "generic_filters.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.0rc3_vs_2.0rc4.

    1 # -*- coding: utf-8 -*-
    2 #
    3 #   Copyright (C) 2013-2017 by Igor E. Novikov
    4 #
    5 #   This program is free software: you can redistribute it and/or modify
    6 #   it under the terms of the GNU General Public License as published by
    7 #   the Free Software Foundation, either version 3 of the License, or
    8 #   (at your option) any later version.
    9 #
   10 #   This program is distributed in the hope that it will be useful,
   11 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
   12 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13 #   GNU General Public License for more details.
   14 #
   15 #   You should have received a copy of the GNU General Public License
   16 #   along with this program.  If not, see <https://www.gnu.org/licenses/>.
   17 
   18 import errno
   19 import logging
   20 import os
   21 import xml.sax
   22 from xml.sax import handler
   23 from xml.sax.xmlreader import InputSource
   24 
   25 from uc2 import _, events, msgconst, utils
   26 from uc2.utils.fsutils import get_fileptr, get_sys_path
   27 
   28 LOG = logging.getLogger(__name__)
   29 
   30 
   31 class AbstractLoader(object):
   32     name = 'Abstract Loader'
   33 
   34     presenter = None
   35     config = None
   36     model = None
   37 
   38     filepath = ''
   39     fileptr = None
   40     position = 0
   41     file_size = 0
   42 
   43     def __init__(self):
   44         pass
   45 
   46     def load(self, presenter, path=None, fileptr=None):
   47         self.presenter = presenter
   48         self.model = presenter.model
   49         self.config = self.presenter.config
   50         if path:
   51             self.filepath = path
   52             self.file_size = os.path.getsize(get_sys_path(path))
   53             self.fileptr = get_fileptr(path)
   54         elif fileptr:
   55             self.fileptr = fileptr
   56             self.fileptr.seek(0, 2)
   57             self.file_size = self.fileptr.tell()
   58             self.fileptr.seek(0)
   59         else:
   60             msg = _('There is no file for reading')
   61             raise IOError(errno.ENODATA, msg, '')
   62 
   63         try:
   64             self.init_load()
   65         except Exception:
   66             LOG.error('Error loading file content')
   67             raise
   68 
   69         self.fileptr.close()
   70         self.position = 0
   71         return self.model
   72 
   73     def init_load(self):
   74         self.do_load()
   75 
   76     def do_load(self):
   77         pass
   78 
   79     def readln(self, strip=True):
   80         line = self.fileptr.readline()
   81         if strip:
   82             line = line.strip()
   83         return line
   84 
   85     def check_loading(self):
   86         position = float(self.fileptr.tell()) / float(self.file_size) * 0.95
   87         if position - self.position > 0.05:
   88             self.position = position
   89             self.parsing_msg(position)
   90 
   91     def send_progress_message(self, msg, val):
   92         events.emit(events.FILTER_INFO, msg, val)
   93 
   94     def parsing_msg(self, val):
   95         msg = _('Parsing in progress...')
   96         self.send_progress_message(msg, val)
   97 
   98     def send_ok(self, msg):
   99         events.emit(events.MESSAGES, msgconst.OK, msg)
  100 
  101     def send_info(self, msg):
  102         events.emit(events.MESSAGES, msgconst.INFO, msg)
  103 
  104     def send_warning(self, msg):
  105         events.emit(events.MESSAGES, msgconst.WARNING, msg)
  106 
  107     def send_error(self, msg):
  108         events.emit(events.MESSAGES, msgconst.ERROR, msg)
  109 
  110 
  111 class AbstractBinaryLoader(AbstractLoader):
  112     def readbytes(self, size):
  113         return self.fileptr.read(size)
  114 
  115     def readbyte(self):
  116         return utils.byte2py_int(self.fileptr.read(1))
  117 
  118     def readword(self):
  119         return utils.word2py_int(self.fileptr.read(2))
  120 
  121     def readdword(self):
  122         return utils.dword2py_int(self.fileptr.read(4))
  123 
  124     def read_pair_dword(self):
  125         return utils.pair_dword2py_int(self.fileptr.read(8))
  126 
  127     def readstr(self, size):
  128         return utils.latin1_bytes_2str(self.fileptr.read(size))
  129 
  130     def readustr(self, size):
  131         return utils.utf_16_le_bytes_2str(self.fileptr.read(size * 2))
  132 
  133 
  134 class AbstractXMLLoader(AbstractLoader, handler.ContentHandler):
  135     xml_reader = None
  136     input_source = None
  137 
  138     def init_load(self):
  139         self.input_source = InputSource()
  140         self.input_source.setByteStream(self.fileptr)
  141         self.xml_reader = xml.sax.make_parser()
  142         self.xml_reader.setContentHandler(self)
  143         self.xml_reader.setErrorHandler(handler.ErrorHandler())
  144         self.xml_reader.setEntityResolver(handler.EntityResolver())
  145         self.xml_reader.setDTDHandler(handler.DTDHandler())
  146         self.xml_reader.setFeature(handler.feature_external_ges, False)
  147         self.do_load()
  148 
  149     def start_parsing(self):
  150         self.xml_reader.parse(self.input_source)
  151 
  152     def startElement(self, name, attrs):
  153         if isinstance(name, unicode):
  154             name = name.encode('utf-8')
  155             ret = {}
  156             for key,value in attrs._attrs.items():
  157                 ret[key.encode('utf-8')] = attrs[key].encode('utf-8')
  158             attrs._attrs = ret
  159         self.start_element(name, attrs)
  160 
  161     def endElement(self, name):
  162         if isinstance(name, unicode):
  163             name = name.encode('utf-8')
  164         self.end_element(name)
  165 
  166     def characters(self, data):
  167         if isinstance(data, unicode):
  168             data = data.encode('utf-8')
  169         self.element_data(data)
  170 
  171     def start_element(self, name, attrs): pass
  172 
  173     def end_element(self, name): pass
  174 
  175     def element_data(self, data): pass
  176 
  177 
  178 class AbstractSaver(object):
  179     name = 'Abstract Saver'
  180 
  181     presenter = None
  182     config = None
  183 
  184     filepath = ''
  185     fileptr = None
  186     position = 0
  187     file_size = 0
  188 
  189     model = None
  190 
  191     def __init__(self):
  192         pass
  193 
  194     def save(self, presenter, path=None, fileptr=None):
  195         self.presenter = presenter
  196         self.config = self.presenter.config
  197         self.model = presenter.model
  198         if path:
  199             self.fileptr = get_fileptr(path, True)
  200         elif fileptr:
  201             self.fileptr = fileptr
  202         else:
  203             msg = _('There is no file for writting')
  204             raise IOError(errno.ENODATA, msg, '')
  205 
  206         self.presenter.update()
  207         self.saving_msg(.01)
  208         try:
  209             self.do_save()
  210         except Exception as e:
  211             LOG.error('Error saving file content %s', e)
  212             raise
  213         self.saving_msg(.99)
  214         self.fileptr.close()
  215         self.fileptr = None
  216 
  217     def do_save(self):
  218         pass
  219 
  220     def writeln(self, line=''):
  221         self.fileptr.write(line + '\n')
  222 
  223     def write(self, data):
  224         self.fileptr.write(data)
  225 
  226     def field_to_str(self, val):
  227         val_str = val.__str__()
  228         if isinstance(val, str):
  229             val_str = val_str.replace("\n", " ").replace("\r", " ")
  230             val_str = "'%s'" % val_str.replace("'", "\\'")
  231         return val_str
  232 
  233     def send_progress_message(self, msg, val):
  234         events.emit(events.FILTER_INFO, msg, val)
  235 
  236     def saving_msg(self, val):
  237         msg = _('Saving in progress...')
  238         self.send_progress_message(msg, val)
  239 
  240     def send_ok(self, msg):
  241         events.emit(events.MESSAGES, msgconst.OK, msg)
  242 
  243     def send_info(self, msg):
  244         events.emit(events.MESSAGES, msgconst.INFO, msg)
  245 
  246     def send_warning(self, msg):
  247         events.emit(events.MESSAGES, msgconst.WARNING, msg)
  248 
  249     def send_error(self, msg):
  250         events.emit(events.MESSAGES, msgconst.ERROR, msg)