def _nets(self, jc, spec): nets = pkcollections.Dict() for n, v in spec.items(): v = copy.deepcopy(v) # Avoid this error: # Did you pass in a bytes (str in Python 2) instead of a unicode object? n = ipaddress.ip_network(pkcompat.locale_str(n)) v.name = n v.ip = str(n.network_address) v.netmask = str(n.netmask) v.pksetdefault( # only needed for debugging gateway='', is_global=n.is_global, ) v.pksetdefault(is_private=lambda: not v.is_global and n.is_private) if v.gateway: assert ipaddress.ip_network( pkcompat.locale_str(v.gateway + '/32') ).subnet_of(n), \ '{}: gateway is not subnet of {}'.format(v.gateway, n) if 'nameservers' in v: v.nameservers = sorted([ipaddress.ip_address(x) for x in v.nameservers]) nets[n] = v return nets
def test_locale_str_2(): """Invalid utf8""" from pykern import pkcompat with pytest.raises(UnicodeDecodeError): #TODO(robngler) set the locale? pkcompat.locale_str(b'\x80')
def _fixup(obj): """Convert all objects to locale strings""" if isinstance(obj, dict): res = pkcollections.Dict() for k in obj: res[pkcompat.locale_str(k)] = _fixup(obj[k]) return res if isinstance(obj, list): res = [] for v in obj: res.append(_fixup(v)) return res if type(obj) == bytes or type(obj) == str and hasattr(obj, 'decode'): return pkcompat.locale_str(obj) return obj
def _net_check(self, ip): nip = ipaddress.ip_network(pkcompat.locale_str(ip + '/32')) #TODO(robnagler) untrusted networks are supernets of potentially trusted_nets # the only reason to add them is to find them for devices for nets in self.__untrusted_nets, self.__trusted_nets: for n in nets: if nip.subnet_of(n): return nets[n] raise AssertionError('{}: ip not found in un/trusted_networks'.format(nip))
def test_locale_str_1(): """Verify proper conversions""" from pykern import pkcompat s = pkcompat.locale_str(b'\xc2\xb0') if six.PY2: assert isinstance(s, unicode), \ 'When locale_str is converted in PY2, it should return unicode' else: assert isinstance(s, str), \ 'When locale_str is converted in not PY2, it should return str' assert u'°' == s, \ 'Conversion should be same as literal unicode value' if six.PY2: before = unicode(b'\xc2\xb0', 'utf8') assert before == pkcompat.locale_str(before), \ 'When string is already unicode, conversion yields same string' before = str(123) assert unicode(before) == pkcompat.locale_str(before), \ 'When string is already unicode, conversion yields same string' before = str(None) assert unicode(before) == pkcompat.locale_str(before), \ 'When string is already unicode, conversion yields same string' else: before = str(123) assert before == pkcompat.locale_str(before), \ 'When string is already unicode, conversion yields same string' before = str(None) assert before == pkcompat.locale_str(before), \ 'When string is already unicode, conversion yields same string'
def test_locale_str_1(): """Verify proper conversions""" s = pkcompat.locale_str(b'\xc2\xb0') if six.PY2: assert isinstance(s, unicode), \ 'When locale_str is converted in PY2, it should return unicode' else: assert isinstance(s, str), \ 'When locale_str is converted in not PY2, it should return str' assert u'°' == s, \ 'Conversion should be same as literal unicode value' if six.PY2: before = unicode(b'\xc2\xb0', 'utf8') assert before == pkcompat.locale_str(before), \ 'When string is already unicode, conversion yields same string' before = str(123) assert unicode(before) == pkcompat.locale_str(before), \ 'When string is already unicode, conversion yields same string' before = str(None) assert unicode(before) == pkcompat.locale_str(before), \ 'When string is already unicode, conversion yields same string' else: before = str(123) assert unicode(before) == pkcompat.locale_str(before), \ 'When string is already unicode, conversion yields same string' before = str(None) assert unicode(before) == pkcompat.locale_str(before), \ 'When string is already unicode, conversion yields same string'
def write_text(filename, contents): """Open file, write text with preferred encoding, and close. Args: filename (str or py.path.Local): File to open contents (str): New contents Returns: py.path.local: `filename` as :class:`py.path.Local` """ fn = py.path.local(filename) with io.open(str(fn), 'w', encoding=locale.getpreferredencoding()) as f: f.write(pkcompat.locale_str(contents)) return fn
def write_text(path, contents): """Open file, write text with preferred encoding, and close. Args: path (str or py.path.Local): Path of file to write to contents (str): New contents Returns: py.path.local: `filename` as :class:`py.path.Local` """ from pykern import pkcompat fn = py_path(path) with io.open(str(fn), 'w', encoding=locale.getpreferredencoding()) as f: f.write(pkcompat.locale_str(contents)) return fn
def _setup_tls_host(self, j2_ctx, z): import socket import ipaddress c, ca = _self_signed_crt(j2_ctx, j2_ctx.rsconf_db.host) self.install_access(mode='700', owner=z.run_u) self.install_directory(_TLS_DIR) self.install_access(mode='400', owner=z.run_u) z.tls = pkcollections.Dict() # just to match docker's documentation, not really important for k, b in ('crt', 'cert'), ('key', 'key'): z.tls[k] = _TLS_DIR.join(b + '.pem') self.install_abspath(c[k], z.tls[k]) z.tls.ca_crt = _TLS_DIR.join('cacert.pem') self.install_abspath(ca['crt'], z.tls.ca_crt) z.tls.ip = socket.gethostbyname(z.tls_host) assert ipaddress.ip_address(pkcompat.locale_str(z.tls.ip)).is_private, \ 'tls_host={} is on public ip={}'.format(z.tls_host, z.tls.ip) z.daemon_hosts.append('tcp://{}:{}'.format(z.tls.ip, _DAEMON_PORT))
def test_locale_str_2(): """Invalid utf8""" with pytest.raises(UnicodeDecodeError): #TODO(robngler) set the locale? pkcompat.locale_str(b'\x80')