## Copyright (C) 2006 Kovid Goyal kovid@kovidgoyal.net ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License along ## with this program; if not, write to the Free Software Foundation, Inc., ## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ''' E-book management software''' __version__ = "0.3.95" __docformat__ = "epytext" __author__ = "Kovid Goyal " __appname__ = 'libprs500' import sys, os, logging iswindows = 'win32' in sys.platform.lower() isosx = 'darwin' in sys.platform.lower() if iswindows: try: import WmfPlugin except: pass class CommandLineError(Exception): pass def setup_cli_handlers(logger, level): logger.setLevel(level) if level == logging.WARNING: handler = logging.StreamHandler(sys.stdout) handler.setFormatter(logging.Formatter('%(levelname)s: %(message)s')) handler.setLevel(logging.WARNING) elif level == logging.INFO: handler = logging.StreamHandler(sys.stdout) handler.setFormatter(logging.Formatter()) handler.setLevel(logging.INFO) elif level == logging.DEBUG: handler = logging.StreamHandler(sys.stderr) handler.setLevel(logging.DEBUG) handler.setFormatter(logging.Formatter('[%(levelname)s] %(filename)s:%(lineno)s: %(message)s')) logger.addHandler(handler) def load_library(name, cdll): if iswindows: return cdll.LoadLibrary(name) if isosx: name += '.dylib' if hasattr(sys, 'frameworks_dir'): return cdll.LoadLibrary(os.path.join(sys.frameworks_dir, name)) return cdll.LoadLibrary(name) return cdll.LoadLibrary(name+'.so') def filename_to_utf8(name): '''Return C{name} encoded in utf8. Unhandled characters are replaced. ''' codec = 'cp1252' if iswindows else 'utf8' return name.decode(codec, 'replace').encode('utf8') def extract(path, dir): import os ext = os.path.splitext(path)[1][1:].lower() extractor = None if ext == 'zip': from libprs500.libunzip import extract extractor = extract elif ext == 'rar': from libprs500.libunrar import extract extractor = extract if not extractor: raise Exception('Unknown archive type') extractor(path, dir)