Python os.path.decode() Examples
The following are 27
code examples of os.path.decode().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
os.path
, or try the search function
.
Example #1
Source File: __init__.py From UWGeodynamics with GNU General Public License v3.0 | 6 votes |
def get_home(): """Find user's home directory if possible. Otherwise, returns None. :see: http://mail.python.org/pipermail/python-list/2005-February/325395.html """ try: if six.PY2 and sys.platform == 'win32': path = os.path.expanduser(b"~").decode(sys.getfilesystemencoding()) else: path = os.path.expanduser("~") except ImportError: # This happens on Google App Engine (pwd module is not present). pass else: if os.path.isdir(path): return path for evar in ('HOME', 'USERPROFILE', 'TMP'): path = os.environ.get(evar) if path is not None and os.path.isdir(path): return path return None
Example #2
Source File: __init__.py From cadquery-freecad-module with GNU Lesser General Public License v3.0 | 6 votes |
def decode_path(path): """ Ensure `path` is Unicode. Return `nodes.reprunicode` object. Decode file/path string in a failsave manner if not already done. """ # see also http://article.gmane.org/gmane.text.docutils.user/2905 if isinstance(path, unicode): return path try: path = path.decode(sys.getfilesystemencoding(), 'strict') except AttributeError: # default value None has no decode method return nodes.reprunicode(path) except UnicodeDecodeError: try: path = path.decode('utf-8', 'strict') except UnicodeDecodeError: path = path.decode('ascii', 'replace') return nodes.reprunicode(path)
Example #3
Source File: __init__.py From bash-lambda-layer with MIT License | 6 votes |
def decode_path(path): """ Ensure `path` is Unicode. Return `nodes.reprunicode` object. Decode file/path string in a failsave manner if not already done. """ # see also http://article.gmane.org/gmane.text.docutils.user/2905 if isinstance(path, str): return path try: path = path.decode(sys.getfilesystemencoding(), 'strict') except AttributeError: # default value None has no decode method return nodes.reprunicode(path) except UnicodeDecodeError: try: path = path.decode('utf-8', 'strict') except UnicodeDecodeError: path = path.decode('ascii', 'replace') return nodes.reprunicode(path)
Example #4
Source File: __init__.py From AWS-Transit-Gateway-Demo-MultiAccount with MIT License | 6 votes |
def decode_path(path): """ Ensure `path` is Unicode. Return `nodes.reprunicode` object. Decode file/path string in a failsave manner if not already done. """ # see also http://article.gmane.org/gmane.text.docutils.user/2905 if isinstance(path, str): return path try: path = path.decode(sys.getfilesystemencoding(), 'strict') except AttributeError: # default value None has no decode method return nodes.reprunicode(path) except UnicodeDecodeError: try: path = path.decode('utf-8', 'strict') except UnicodeDecodeError: path = path.decode('ascii', 'replace') return nodes.reprunicode(path)
Example #5
Source File: __init__.py From AWS-Transit-Gateway-Demo-MultiAccount with MIT License | 6 votes |
def decode_path(path): """ Ensure `path` is Unicode. Return `nodes.reprunicode` object. Decode file/path string in a failsave manner if not already done. """ # see also http://article.gmane.org/gmane.text.docutils.user/2905 if isinstance(path, str): return path try: path = path.decode(sys.getfilesystemencoding(), 'strict') except AttributeError: # default value None has no decode method return nodes.reprunicode(path) except UnicodeDecodeError: try: path = path.decode('utf-8', 'strict') except UnicodeDecodeError: path = path.decode('ascii', 'replace') return nodes.reprunicode(path)
Example #6
Source File: http.py From vlcp with Apache License 2.0 | 6 votes |
def getrealpath(self, root, path): ''' Return the real path on disk from the query path, from a root path. The input path from URL might be absolute '/abc', or point to parent '../test', or even with UNC or drive '\\test\abc', 'c:\test.abc', which creates security issues when accessing file contents with the path. With getrealpath, these paths cannot point to files beyond the root path. :param root: root path of disk files, any query is limited in root directory. :param path: query path from URL. ''' if not isinstance(path, str): path = path.decode(self.encoding) # In windows, if the path starts with multiple / or \, the os.path library may consider it an UNC path # remove them; also replace \ to / path = pathrep.subn('/', path)[0] # The relative root is considered ROOT, eliminate any relative path like ../abc, which create security issues # We can use os.path.relpath(..., '/') but python2.6 os.path.relpath is buggy path = os.path.normpath(os.path.join('/', path)) # The normalized path can be an UNC path, or event a path with drive letter # Send bad request for these types if os.path.splitdrive(path)[0]: raise HttpInputException('Bad path') return os.path.join(root, path[1:])
Example #7
Source File: __init__.py From blackmamba with MIT License | 6 votes |
def decode_path(path): """ Ensure `path` is Unicode. Return `nodes.reprunicode` object. Decode file/path string in a failsave manner if not already done. """ # see also http://article.gmane.org/gmane.text.docutils.user/2905 if isinstance(path, str): return path try: path = path.decode(sys.getfilesystemencoding(), 'strict') except AttributeError: # default value None has no decode method return nodes.reprunicode(path) except UnicodeDecodeError: try: path = path.decode('utf-8', 'strict') except UnicodeDecodeError: path = path.decode('ascii', 'replace') return nodes.reprunicode(path)
Example #8
Source File: __init__.py From deepWordBug with Apache License 2.0 | 6 votes |
def decode_path(path): """ Ensure `path` is Unicode. Return `nodes.reprunicode` object. Decode file/path string in a failsave manner if not already done. """ # see also http://article.gmane.org/gmane.text.docutils.user/2905 if isinstance(path, str): return path try: path = path.decode(sys.getfilesystemencoding(), 'strict') except AttributeError: # default value None has no decode method return nodes.reprunicode(path) except UnicodeDecodeError: try: path = path.decode('utf-8', 'strict') except UnicodeDecodeError: path = path.decode('ascii', 'replace') return nodes.reprunicode(path)
Example #9
Source File: __init__.py From aws-extender with MIT License | 6 votes |
def decode_path(path): """ Ensure `path` is Unicode. Return `nodes.reprunicode` object. Decode file/path string in a failsave manner if not already done. """ # see also http://article.gmane.org/gmane.text.docutils.user/2905 if isinstance(path, unicode): return path try: path = path.decode(sys.getfilesystemencoding(), 'strict') except AttributeError: # default value None has no decode method return nodes.reprunicode(path) except UnicodeDecodeError: try: path = path.decode('utf-8', 'strict') except UnicodeDecodeError: path = path.decode('ascii', 'replace') return nodes.reprunicode(path)
Example #10
Source File: __init__.py From aws-builders-fair-projects with Apache License 2.0 | 6 votes |
def decode_path(path): """ Ensure `path` is Unicode. Return `nodes.reprunicode` object. Decode file/path string in a failsave manner if not already done. """ # see also http://article.gmane.org/gmane.text.docutils.user/2905 if isinstance(path, unicode): return path try: path = path.decode(sys.getfilesystemencoding(), 'strict') except AttributeError: # default value None has no decode method return nodes.reprunicode(path) except UnicodeDecodeError: try: path = path.decode('utf-8', 'strict') except UnicodeDecodeError: path = path.decode('ascii', 'replace') return nodes.reprunicode(path)
Example #11
Source File: __init__.py From faces with GNU General Public License v2.0 | 6 votes |
def decode_path(path): """ Ensure `path` is Unicode. Return `nodes.reprunicode` object. Decode file/path string in a failsave manner if not already done. """ # see also http://article.gmane.org/gmane.text.docutils.user/2905 if isinstance(path, unicode): return path try: path = path.decode(sys.getfilesystemencoding(), 'strict') except AttributeError: # default value None has no decode method return nodes.reprunicode(path) except UnicodeDecodeError: try: path = path.decode('utf-8', 'strict') except UnicodeDecodeError: path = path.decode('ascii', 'replace') return nodes.reprunicode(path)
Example #12
Source File: __init__.py From faces with GNU General Public License v2.0 | 6 votes |
def decode_path(path): """ Ensure `path` is Unicode. Return `nodes.reprunicode` object. Decode file/path string in a failsave manner if not already done. """ # see also http://article.gmane.org/gmane.text.docutils.user/2905 if isinstance(path, str): return path try: path = path.decode(sys.getfilesystemencoding(), 'strict') except AttributeError: # default value None has no decode method return nodes.reprunicode(path) except UnicodeDecodeError: try: path = path.decode('utf-8', 'strict') except UnicodeDecodeError: path = path.decode('ascii', 'replace') return nodes.reprunicode(path)
Example #13
Source File: reloader.py From CloudBot with GNU General Public License v3.0 | 5 votes |
def reload(self, path): """ Loads or reloads a module, given its file path. Thread safe. :type path: str """ if not os.path.isfile(path): # we check if the file still exists here because some programs modify a file before deleting return if isinstance(path, bytes): path = path.decode() self.bot.loop.call_soon_threadsafe(lambda: asyncio.async(self._reload(path), loop=self.bot.loop))
Example #14
Source File: reloader.py From CloudBot with GNU General Public License v3.0 | 5 votes |
def unload(self, path): """ Unloads a module, given its file path. Thread safe. :type path: str """ if isinstance(path, bytes): path = path.decode() self.bot.loop.call_soon_threadsafe(lambda: asyncio.async(self._unload(path), loop=self.bot.loop))
Example #15
Source File: cli.py From geofront-cli with GNU General Public License v3.0 | 5 votes |
def get_server_url(): for path in load_config_paths(CONFIG_RESOURCE): path = os.path.join(path.decode(), SERVER_CONFIG_FILENAME) if os.path.isfile(path): with open(path) as f: return f.read().strip() parser.exit('Geofront server URL is not configured yet.\n' 'Try `{0} start` command.'.format(parser.prog))
Example #16
Source File: cli.py From geofront-cli with GNU General Public License v3.0 | 5 votes |
def start(args): """Set up the Geofront server URL.""" for path in load_config_paths(CONFIG_RESOURCE): path = os.path.join(path.decode(), SERVER_CONFIG_FILENAME) if os.path.isfile(path): message = 'Geofront server URL is already configured: ' + path if args.force: print(message + '; overwriting...', file=sys.stderr) else: parser.exit(message) while True: server_url = input('Geofront server URL: ') if not server_url.startswith(('https://', 'http://')): print(server_url, 'is not a valid url.') continue elif not server_url.startswith('https://'): cont = input('It is not a secure URL. ' 'https:// is preferred over http://. ' 'Continue (y/N)? ') if cont.strip().lower() != 'y': continue break server_config_filename = os.path.join( save_config_path(CONFIG_RESOURCE).decode(), SERVER_CONFIG_FILENAME ) with open(server_config_filename, 'w') as f: print(server_url, file=f) authenticate.call(args)
Example #17
Source File: cli.py From geofront-cli with GNU General Public License v3.0 | 5 votes |
def fix_mac_codesign(): """If the running Python interpreter isn't property signed on macOS it's unable to get/set password using keyring from Keychain. In such case, we need to sign the interpreter first. https://github.com/jaraco/keyring/issues/219 """ global fix_mac_codesign logger = logging.getLogger(__name__ + '.fix_mac_codesign') p = subprocess.Popen(['codesign', '-dvvvvv', sys.executable], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() def prepend_lines(c, text): if not isinstance(text, str): text = text.decode() return ''.join(c + l for l in text.splitlines(True)) logger.debug('codesign -dvvvvv %s:\n%s\n%s', sys.executable, prepend_lines('| ', stdout), prepend_lines('> ', stderr)) if b'\nSignature=' in stderr: logger.debug('%s: already signed', sys.executable) return logger.info('%s: not signed yet; try signing...', sys.executable) p = subprocess.Popen(['codesign', '-f', '-s', '-', sys.executable], stdout=subprocess.PIPE, stderr=subprocess.PIPE) os.waitpid(p.pid, 0) logger.debug('%s: signed\n%s\n%s', sys.executable, prepend_lines('| ', stdout), prepend_lines('> ', stderr)) logger.debug('respawn the equivalent process...') raise SystemExit(subprocess.call(sys.argv))
Example #18
Source File: http.py From vlcp with Apache License 2.0 | 5 votes |
def cookietostr(self): "Cookie values are bytes in Python3. This function Convert bytes to string with env.encoding(default to utf-8)." self.cookies = dict((k, (v.decode(self.encoding) if not isinstance(v, str) else v)) for k,v in self.cookies.items()) return self.cookies
Example #19
Source File: http.py From vlcp with Apache License 2.0 | 5 votes |
def _tostr(self, arg): if isinstance(arg, list): return [self._tostr(v) for v in arg] elif not isinstance(arg, str): return arg.decode(self.encoding) else: return arg
Example #20
Source File: http.py From vlcp with Apache License 2.0 | 5 votes |
def __repr__(self, *args, **kwargs): r = self.method.upper() + b' ' + self.path + (b', ' + self.connection.remoteaddr[0].encode(self.encoding) if self.connection.remoteaddr else b'') if not isinstance(r, str): return r.decode(self.encoding) else: return r
Example #21
Source File: test_debugger_json.py From PyDev.Debugger with Eclipse Public License 1.0 | 5 votes |
def wait_for_thread_stopped(self, reason='breakpoint', line=None, file=None, name=None): ''' :param file: utf-8 bytes encoded file or unicode ''' stopped_event = self.wait_for_json_message(StoppedEvent) assert stopped_event.body.reason == reason json_hit = self.get_stack_as_json_hit(stopped_event.body.threadId) if file is not None: path = json_hit.stack_trace_response.body.stackFrames[0]['source']['path'] if IS_PY2: if isinstance(file, bytes): file = file.decode('utf-8') if isinstance(path, bytes): path = path.decode('utf-8') if not path.endswith(file): raise AssertionError('Expected path: %s to end with: %s' % (path, file)) if name is not None: assert json_hit.stack_trace_response.body.stackFrames[0]['name'] == name if line is not None: found_line = json_hit.stack_trace_response.body.stackFrames[0]['line'] if not isinstance(line, (tuple, list)): line = [line] assert found_line in line, 'Expect to break at line: %s. Found: %s' % (line, found_line) return json_hit
Example #22
Source File: __init__.py From UWGeodynamics with GNU General Public License v3.0 | 5 votes |
def _decode_filesystem_path(path): if isinstance(path, bytes): return path.decode(sys.getfilesystemencoding()) else: return path
Example #23
Source File: test_debugger_json.py From PyDev.Debugger with Eclipse Public License 1.0 | 4 votes |
def test_send_invalid_messages(case_setup): with case_setup.test_file('_debugger_case_local_variables.py') as writer: json_facade = JsonFacade(writer) writer.write_add_breakpoint(writer.get_line_index_with_content('Break 2 here')) json_facade.write_make_initial_run() stopped_event = json_facade.wait_for_json_message(StoppedEvent) thread_id = stopped_event.body.threadId json_facade.write_request( pydevd_schema.StackTraceRequest(pydevd_schema.StackTraceArguments(threadId=thread_id))) # : :type response: ModulesResponse # : :type modules_response_body: ModulesResponseBody # *** Check that we accept an invalid modules request (i.e.: without arguments). response = json_facade.wait_for_response(json_facade.write_request( {'type': 'request', 'command': 'modules'})) modules_response_body = response.body assert len(modules_response_body.modules) == 1 module = next(iter(modules_response_body.modules)) assert module['name'] == '__main__' assert module['path'].endswith('_debugger_case_local_variables.py') # *** Check that we don't fail on request without command. request = json_facade.write_request({'type': 'request'}) response = json_facade.wait_for_response(request, Response) assert not response.success assert response.command == '<unknown>' # *** Check that we don't crash if we can't decode message. json_facade.writer.write_with_content_len('invalid json here') # *** Check that we get a failure from a completions without arguments. response = json_facade.wait_for_response(json_facade.write_request( {'type': 'request', 'command': 'completions'})) assert not response.success json_facade.write_continue() writer.finished_ok = True
Example #24
Source File: test_debugger_json.py From PyDev.Debugger with Eclipse Public License 1.0 | 4 votes |
def test_source_mapping_just_my_code(case_setup): from _pydevd_bundle._debug_adapter.pydevd_schema import Source from _pydevd_bundle._debug_adapter.pydevd_schema import PydevdSourceMap case_setup.check_non_ascii = True with case_setup.test_file('_debugger_case_source_mapping_jmc.py') as writer: json_facade = JsonFacade(writer) json_facade.write_launch(justMyCode=True) map_to_cell_1_line1 = writer.get_line_index_with_content('map to cell1, line 1') map_to_cell_1_line6 = writer.get_line_index_with_content('map to cell1, line 6') map_to_cell_1_line7 = writer.get_line_index_with_content('map to cell1, line 7') cell1_map = PydevdSourceMap(map_to_cell_1_line1, map_to_cell_1_line7, Source(path='<cell1>'), 1) pydevd_source_maps = [cell1_map] # Set breakpoints before setting the source map (check that we reapply them). json_facade.write_set_breakpoints(map_to_cell_1_line6) test_file = writer.TEST_FILE if isinstance(test_file, bytes): # file is in the filesystem encoding (needed for launch) but protocol needs it in utf-8 test_file = test_file.decode(file_system_encoding) test_file = test_file.encode('utf-8') json_facade.write_set_pydevd_source_map( Source(path=test_file), pydevd_source_maps=pydevd_source_maps, ) json_facade.write_make_initial_run() json_hit = json_facade.wait_for_thread_stopped(line=map_to_cell_1_line6, file=os.path.basename(test_file)) for stack_frame in json_hit.stack_trace_response.body.stackFrames: assert stack_frame['source']['sourceReference'] == 0 # i.e.: Remove the source maps json_facade.write_set_pydevd_source_map( Source(path=test_file), pydevd_source_maps=[], ) json_facade.write_continue() writer.finished_ok = True
Example #25
Source File: test_debugger_json.py From PyDev.Debugger with Eclipse Public License 1.0 | 4 votes |
def test_source_mapping_base(case_setup, target, jmc): from _pydevd_bundle._debug_adapter.pydevd_schema import Source from _pydevd_bundle._debug_adapter.pydevd_schema import PydevdSourceMap case_setup.check_non_ascii = True with case_setup.test_file(target) as writer: json_facade = JsonFacade(writer) json_facade.write_launch(justMyCode=jmc) map_to_cell_1_line2 = writer.get_line_index_with_content('map to cell1, line 2') map_to_cell_2_line2 = writer.get_line_index_with_content('map to cell2, line 2') cell1_map = PydevdSourceMap(map_to_cell_1_line2, map_to_cell_1_line2 + 1, Source(path='<cell1>'), 2) cell2_map = PydevdSourceMap(map_to_cell_2_line2, map_to_cell_2_line2 + 1, Source(path='<cell2>'), 2) pydevd_source_maps = [ cell1_map, cell2_map, cell2_map, # The one repeated should be ignored. ] # Set breakpoints before setting the source map (check that we reapply them). json_facade.write_set_breakpoints(map_to_cell_1_line2) test_file = writer.TEST_FILE if isinstance(test_file, bytes): # file is in the filesystem encoding (needed for launch) but protocol needs it in utf-8 test_file = test_file.decode(file_system_encoding) test_file = test_file.encode('utf-8') json_facade.write_set_pydevd_source_map( Source(path=test_file), pydevd_source_maps=pydevd_source_maps, ) json_facade.write_make_initial_run() json_hit = json_facade.wait_for_thread_stopped(line=map_to_cell_1_line2, file=os.path.basename(test_file)) for stack_frame in json_hit.stack_trace_response.body.stackFrames: assert stack_frame['source']['sourceReference'] == 0 # Check that we no longer stop at the cell1 breakpoint (its mapping should be removed when # the new one is added and we should only stop at cell2). json_facade.write_set_breakpoints(map_to_cell_2_line2) for stack_frame in json_hit.stack_trace_response.body.stackFrames: assert stack_frame['source']['sourceReference'] == 0 json_facade.write_continue() json_hit = json_facade.wait_for_thread_stopped(line=map_to_cell_2_line2, file=os.path.basename(test_file)) json_facade.write_set_breakpoints([]) # Clears breakpoints json_facade.write_continue() writer.finished_ok = True
Example #26
Source File: __init__.py From UWGeodynamics with GNU General Public License v3.0 | 4 votes |
def _rc_params_in_file(fname, fail_on_error=False): """Return :class:`matplotlib.RcParams` from the contents of the given file. Unlike `rc_params_from_file`, the configuration class only contains the parameters specified in the file (i.e. default values are not filled in). """ cnt = 0 rc_temp = {} with open(fname, "r") as fd: try: for line in fd: cnt += 1 strippedline = line.split('#', 1)[0].strip() if not strippedline: continue tup = strippedline.split(':', 1) if len(tup) != 2: error_details = _error_details_fmt % (cnt, line, fname) warnings.warn('Illegal %s' % error_details) continue key, val = tup key = key.strip() val = val.strip() if key in rc_temp: warnings.warn('Duplicate key in file "%s", line #%d' % (fname, cnt)) rc_temp[key] = (val, line, cnt) except UnicodeDecodeError: warnings.warn( ('Cannot decode configuration file %s with ' 'encoding %s, check LANG and LC_* variables') % (fname, locale.getdefaultlocale()[1] or 'utf-8 (default)')) raise config = RcParams() for key, (val, line, cnt) in rc_temp.items(): if key in defaultParams: if fail_on_error: config[key] = val # try to convert to proper type or raise else: try: config[key] = val # try to convert to proper type or skip except Exception as msg: error_details = _error_details_fmt % (cnt, line, fname) warnings.warn('Bad val "%s" on %s\n\t%s' % (val, error_details, msg)) elif key in _deprecated_ignore_map: warnings.warn('%s is deprecated. Update your matplotlibrc to use ' '%s instead.' % (key, _deprecated_ignore_map[key])) else: print(""" Bad key "%s" on line %d in %s.""" % (key, cnt, fname)) return config
Example #27
Source File: rclonesync.py From rclonesync-V2 with MIT License | 4 votes |
def pathparse(path): """Handle variations in a path argument. Cloud: - Root of the defined cloud Cloud:some/path - Supported with our without path leading '/'s X: - Windows drive letter X:\\some\\path - Windows drive letter with absolute or relative path some/path - Relative path from cwd (and on current drive on Windows) //server/path - UNC paths are supported On Windows a one-character cloud name is not supported - it will be interprested as a drive letter. """ if is_Linux and is_Py27: path = path.decode("utf-8") _cloud = False if ':' in path: if len(path) == 1: # Handle corner case of ':' only passed in logging.error("ERROR Path argument <{}> not a legal path".format(path)); exit() if path[1] == ':' and is_Windows: # Windows drive letter case path_base = path if not path_base.endswith('\\'): # For consistency ensure the path ends with '/' path_base += '/' else: # Cloud case with optional path part path_FORMAT = re.compile(r'([\w-]+):(.*)') out = path_FORMAT.match(path) if out: _cloud = True cloud_name = out.group(1) + ':' if cloud_name not in clouds: logging.error("ERROR Path argument <{}> not in list of configured Clouds: {}" .format(cloud_name, clouds)); exit() path_part = out.group(2) if path_part: # if not path_part.startswith('/'): # For consistency ensure the cloud path part starts and ends with /'s # path_part = '/' + path_part if not (path_part.endswith('/') or path_part.endswith('\\')): # 2nd check is for Windows paths path_part += '/' path_base = cloud_name + path_part else: # Local path (without Windows drive letter) path_base = path if not (path_base.endswith('/') or path_base.endswith('\\')): path_base += '/' if not _cloud: if not os.path.exists(path_base): logging.error("ERROR Local path parameter <{}> cannot be accessed. Path error? Aborting" .format(path_base)); exit() return path_base