From c6db8a1f9ba7b6fc9dd117c0a699f4dfd339a375 Mon Sep 17 00:00:00 2001 From: Kali Kaneko Date: Mon, 18 Apr 2016 18:27:29 -0400 Subject: some fixes after review --- src/leap/bitmask/core/configurable.py | 174 ++-------------------------------- 1 file changed, 9 insertions(+), 165 deletions(-) (limited to 'src/leap/bitmask/core/configurable.py') diff --git a/src/leap/bitmask/core/configurable.py b/src/leap/bitmask/core/configurable.py index 3b97916d..8e33de95 100644 --- a/src/leap/bitmask/core/configurable.py +++ b/src/leap/bitmask/core/configurable.py @@ -18,15 +18,15 @@ Configurable Backend for Bitmask Service. """ import ConfigParser -import locale import os -import re -import sys from twisted.application import service -from twisted.python import log from leap.common import files +from leap.common.config import get_path_prefix + + +DEFAULT_BASEDIR = os.path.join(get_path_prefix(), 'leap') class MissingConfigEntry(Exception): @@ -40,7 +40,7 @@ class ConfigurableService(service.MultiService): config_file = u"bitmaskd.cfg" service_names = ('mail', 'eip', 'zmq', 'web') - def __init__(self, basedir='~/.config/leap'): + def __init__(self, basedir=DEFAULT_BASEDIR): service.MultiService.__init__(self) path = os.path.abspath(os.path.expanduser(basedir)) @@ -61,10 +61,9 @@ class ConfigurableService(service.MultiService): except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): if default is None: - fn = os.path.join(self.basedir, self.config_file) + fn = self._get_config_path() raise MissingConfigEntry("%s is missing the [%s]%s entry" - % (_quote_output(fn), - section, option)) + % fn, section, option) return default def set_config(self, section, option, value): @@ -82,12 +81,8 @@ class ConfigurableService(service.MultiService): if not os.path.isfile(bitmaskd_cfg): self._create_default_config(bitmaskd_cfg) - try: - with open(bitmaskd_cfg, "rb") as f: - self.config.readfp(f) - except EnvironmentError: - if os.path.exists(bitmaskd_cfg): - raise + with open(bitmaskd_cfg, "rb") as f: + self.config.readfp(f) def save_config(self): bitmaskd_cfg = self._get_config_path() @@ -109,154 +104,3 @@ eip = True zmq = True web = False """ - - -def canonical_encoding(encoding): - if encoding is None: - log.msg("Warning: falling back to UTF-8 encoding.", level=log.WEIRD) - encoding = 'utf-8' - encoding = encoding.lower() - if encoding == "cp65001": - encoding = 'utf-8' - elif (encoding == "us-ascii" or encoding == "646" or encoding == - "ansi_x3.4-1968"): - encoding = 'ascii' - - return encoding - - -def check_encoding(encoding): - # sometimes Python returns an encoding name that it doesn't support for - # conversion fail early if this happens - try: - u"test".encode(encoding) - except (LookupError, AttributeError): - raise AssertionError( - "The character encoding '%s' is not supported for conversion." % ( - encoding,)) - -filesystem_encoding = None -io_encoding = None -is_unicode_platform = False - - -def _reload(): - global filesystem_encoding, io_encoding, is_unicode_platform - - filesystem_encoding = canonical_encoding(sys.getfilesystemencoding()) - check_encoding(filesystem_encoding) - - if sys.platform == 'win32': - # On Windows we install UTF-8 stream wrappers for sys.stdout and - # sys.stderr, and reencode the arguments as UTF-8 (see - # scripts/runner.py). - io_encoding = 'utf-8' - else: - ioenc = None - if hasattr(sys.stdout, 'encoding'): - ioenc = sys.stdout.encoding - if ioenc is None: - try: - ioenc = locale.getpreferredencoding() - except Exception: - pass # work around - io_encoding = canonical_encoding(ioenc) - - check_encoding(io_encoding) - - is_unicode_platform = sys.platform in ["win32", "darwin"] - -_reload() - - -def _quote_output(s, quotemarks=True, quote_newlines=None, encoding=None): - """ - Encode either a Unicode string or a UTF-8-encoded bytestring for - representation on stdout or stderr, tolerating errors. If 'quotemarks' is - True, the string is always quoted; otherwise, it is quoted only if - necessary to avoid ambiguity or control bytes in the output. (Newlines are - counted as control bytes iff quote_newlines is True.) - - Quoting may use either single or double quotes. Within single quotes, all - characters stand for themselves, and ' will not appear. Within double - quotes, Python-compatible backslash escaping is used. - - If not explicitly given, quote_newlines is True when quotemarks is True. - """ - assert isinstance(s, (str, unicode)) - if quote_newlines is None: - quote_newlines = quotemarks - - if isinstance(s, str): - try: - s = s.decode('utf-8') - except UnicodeDecodeError: - return 'b"%s"' % ( - ESCAPABLE_8BIT.sub( - lambda m: _str_escape(m, quote_newlines), s),) - - must_double_quote = (quote_newlines and MUST_DOUBLE_QUOTE_NL or - MUST_DOUBLE_QUOTE) - if must_double_quote.search(s) is None: - try: - out = s.encode(encoding or io_encoding) - if quotemarks or out.startswith('"'): - return "'%s'" % (out,) - else: - return out - except (UnicodeDecodeError, UnicodeEncodeError): - pass - - escaped = ESCAPABLE_UNICODE.sub( - lambda m: _unicode_escape(m, quote_newlines), s) - return '"%s"' % ( - escaped.encode(encoding or io_encoding, 'backslashreplace'),) - - -def _unicode_escape(m, quote_newlines): - u = m.group(0) - if u == u'"' or u == u'$' or u == u'`' or u == u'\\': - return u'\\' + u - elif u == u'\n' and not quote_newlines: - return u - if len(u) == 2: - codepoint = ( - ord(u[0]) - 0xD800) * 0x400 + ord(u[1]) - 0xDC00 + 0x10000 - else: - codepoint = ord(u) - if codepoint > 0xFFFF: - return u'\\U%08x' % (codepoint,) - elif codepoint > 0xFF: - return u'\\u%04x' % (codepoint,) - else: - return u'\\x%02x' % (codepoint,) - - -def _str_escape(m, quote_newlines): - c = m.group(0) - if c == '"' or c == '$' or c == '`' or c == '\\': - return '\\' + c - elif c == '\n' and not quote_newlines: - return c - else: - return '\\x%02x' % (ord(c),) - -MUST_DOUBLE_QUOTE_NL = re.compile( - ur'[^\x20-\x26\x28-\x7E\u00A0-\uD7FF\uE000-\uFDCF\uFDF0-\uFFFC]', - re.DOTALL) -MUST_DOUBLE_QUOTE = re.compile( - ur'[^\n\x20-\x26\x28-\x7E\u00A0-\uD7FF\uE000-\uFDCF\uFDF0-\uFFFC]', - re.DOTALL) - -ESCAPABLE_8BIT = re.compile( - r'[^ !#\x25-\x5B\x5D-\x5F\x61-\x7E]', - re.DOTALL) - -# if we must double-quote, then we have to escape ", $ and `, but need not -# escape ' - -ESCAPABLE_UNICODE = re.compile( - ur'([\uD800-\uDBFF][\uDC00-\uDFFF])|' # valid surrogate pairs - ur'[^ !#\x25-\x5B\x5D-\x5F\x61-\x7E\u00A0-\uD7FF' - ur'\uE000-\uFDCF\uFDF0-\uFFFC]', - re.DOTALL) -- cgit v1.2.3