sys.getfilesystemencoding

Here are the examples of the python api sys.getfilesystemencoding taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

142 Examples 7

Example 1

Project: beets Source File: test_library.py
    def test_non_mbcs_characters_on_windows(self):
        oldfunc = sys.getfilesystemencoding
        sys.getfilesystemencoding = lambda: 'mbcs'
        try:
            self.i.title = u'h\u0259d'
            self.lib.path_formats = [(u'default', u'$title')]
            p = self.i.destination()
            self.assertFalse(b'?' in p)
            # We use UTF-8 to encode Windows paths now.
            self.assertTrue(u'h\u0259d'.encode('utf-8') in p)
        finally:
            sys.getfilesystemencoding = oldfunc

Example 2

Project: beets Source File: test_util.py
    def _windows_bytestring_path(self, path):
        old_gfse = sys.getfilesystemencoding
        sys.getfilesystemencoding = lambda: 'mbcs'
        try:
            with _common.platform_windows():
                return util.bytestring_path(path)
        finally:
            sys.getfilesystemencoding = old_gfse

Example 3

Project: veusz Source File: veusz_main.py
def convertArgsUnicode(args):
    '''Convert set of arguments to unicode.
    Arguments in argv use current file system encoding
    '''
    enc = sys.getfilesystemencoding()
    # bail out if not supported
    if enc is None:
        return args
    out = []
    for a in args:
        if isinstance(a, cbytes):
            out.append( a.decode(enc) )
        else:
            out.append(a)
    return out

Example 4

Project: PlotSummary Source File: interactive.py
Function: fsencoding
    def _fsencoding(self):
        """Get the system's filesystem encoding. On Windows, this is always
        UTF-8 (not MBCS).
        """
        encoding = sys.getfilesystemencoding() or sys.getdefaultencoding()
        if encoding == 'mbcs':
            # On Windows, a broken encoding known to Python as "MBCS" is
            # used for the filesystem. However, we only use the Unicode API
            # for Windows paths, so the encoding is actually immaterial so
            # we can avoid dealing with this nastiness. We arbitrarily
            # choose UTF-8.
            encoding = 'utf8'
        return encoding

Example 5

Project: SublimeIPythonNotebook Source File: py3compat.py
        def execfile(fname, *where):
            if isinstance(fname, unicode):
                filename = fname.encode(sys.getfilesystemencoding())
            else:
                filename = fname
            __builtin__.execfile(filename, *where)

Example 6

Project: django-gears Source File: finders.py
    def get_app_assets_dirs(self):
        if not six.PY3:
            fs_encoding = sys.getfilesystemencoding() or sys.getdefaultencoding()
        app_assets_dirs = []
        for app in settings.INSTALLED_APPS:
            try:
                mod = import_module(app)
            except ImportError as e:
                raise ImproperlyConfigured('ImportError %s: %s' % (app, e.args[0]))
            assets_dir = os.path.join(os.path.dirname(mod.__file__), 'assets')
            if os.path.isdir(assets_dir):
                if not six.PY3:
                    assets_dir = assets_dir.decode(fs_encoding)
                app_assets_dirs.append(assets_dir)
        app_assets_dirs = tuple(app_assets_dirs)
        return app_assets_dirs

Example 7

Project: knitpy Source File: py3compat.py
Function: exec_file
        def execfile(fname, glob=None, loc=None, compiler=None):
            if isinstance(fname, unicode):
                filename = fname.encode(sys.getfilesystemencoding())
            else:
                filename = fname
            where = [ns for ns in [glob, loc] if ns is not None]
            if compiler is None:
                builtin_mod.execfile(filename, *where)
            else:
                scripttext = builtin_mod.open(fname).read().rstrip() + '\n'
                exec(compiler(scripttext, filename, 'exec'), glob, loc)

Example 8

Project: pmatic Source File: glob.py
def glob1(dirname, pattern):
    if not dirname:
        dirname = os.curdir
    if isinstance(pattern, _unicode) and not isinstance(dirname, unicode):
        dirname = unicode(dirname, sys.getfilesystemencoding() or
                                   sys.getdefaultencoding())
    try:
        names = os.listdir(dirname)
    except os.error:
        return []
    if pattern[0] != '.':
        names = filter(lambda x: x[0] != '.', names)
    return fnmatch.filter(names, pattern)

Example 9

Project: hae Source File: process.py
Function: decode
	def decode(self, bytes):
		if self.encoding:
			return str(bytes, self.encoding)
		try:
			return str(bytes, sys.getfilesystemencoding())
		except:
			pass
		try:
			return str(bytes, sys.getdefaultencoding())
		except:
			pass
		return str(bytes)[2:-1]

Example 10

Project: sumatra Source File: _git.py
    def content(self, digest, file=None):
        """Get the file content from repository."""
        repo = git.Repo(self.path)
        curtree = repo.commit(digest).tree
        if file is None:
            return curtree.blobs[0].data_stream.read() # Get the latest added file content.
        dirname, filename = os.path.split(file)
        if dirname != '':
            for dname in dirname.split(os.path.sep):
                for subtree in curtree.trees:
                    if subtree.name == dname:
                        curtree = subtree
                        break
        for blob in curtree.blobs:
            if blob.name == filename:
                expected_encoding = sys.getfilesystemencoding()
                file_content = blob.data_stream.read().decode(expected_encoding)
                return file_content
        return 'File content not found.'

Example 11

Project: CythonCTypesBackend Source File: Utils.py
Function: decode_filename
def decode_filename(filename):
    if isinstance(filename, unicode):
        return filename
    try:
        filename_encoding = sys.getfilesystemencoding()
        if filename_encoding is None:
            filename_encoding = sys.getdefaultencoding()
        filename = filename.decode(filename_encoding)
    except UnicodeDecodeError:
        pass
    return filename

Example 12

Project: openmetadata Source File: utils.py
Function: make_str
def make_str(value):
    """Converts a value into a valid string."""
    if isinstance(value, bytes):
        try:
            return value.decode(sys.getfilesystemencoding())
        except UnicodeError:
            return value.decode('utf-8', 'replace')
    return text_type(value)

Example 13

Project: eavatar-me Source File: agent.py
def _mygetfilesystemencoding():
    old = sys.getfilesystemencoding

    def inner_func():
        ret = old()
        if ret is None:
            return 'utf-8'
        else:
            return ret
    return inner_func

Example 14

Project: geofront-cli Source File: utils.py
Function: get_env_path
def get_env_path(key, default):
    """Get a UTF-8 encoded path from an environment variable."""
    if key in os.environ:
        # on windows, environment variables are mbcs bytes
        # so we must turn them into utf-8 Syncdaemon paths
        try:
            path = os.environb.get(key.encode('utf-8'))
        except AttributeError:
            path = os.environ[key]
        return path.decode(sys.getfilesystemencoding()).encode('utf-8')
    else:
        if not isinstance(default, bytes):
            return default.encode('utf-8')
        return default

Example 15

Project: ionyweb Source File: __init__.py
def get_ionyweb_path():
    """
        Return full patt of ionyweb
    """
    fs_encoding = sys.getfilesystemencoding() or sys.getdefaultencoding()
    template_dir = os.path.dirname(__file__)
    return template_dir.decode(fs_encoding)

Example 16

Project: zine Source File: __init__.py
    def test_instance_folder(self, request):
        """Check if the instance folder exists."""
        _ = request.translations.gettext
        if not path.exists(self.instance_folder):
            folder = self.instance_folder
            if not isinstance(folder, unicode):
                folder = folder.decode(sys.getfilesystemencoding() or 'utf-8',
                                       'ignore')
            return {'error': _(u'Instance folder does not exist.  You have to '
                    u'create the folder “%s” before proceeding.') % folder}

Example 17

Project: Tardis Source File: RemoteDB.py
Function: fs_encode
def fs_encode(val):
    """ Turn filenames into str's (ie, series of bytes) rather than Unicode things """
    if not isinstance(val, bytes):
        #return val.encode(sys.getfilesystemencoding())
        return val.encode(sys.getfilesystemencoding())
    else:
        return val

Example 18

Project: ComicStreamer Source File: folders.py
    @staticmethod
    def userFolder():
        filename_encoding = sys.getfilesystemencoding()
        if platform.system() == "Windows":
            folder = os.path.join( AppFolders.windowsAppDataFolder(), 'ComicStreamer' )
        elif platform.system() == "Darwin":
            folder = os.path.join( os.path.expanduser('~') , 'Library/Application Support/ComicStreamer')
        else:
            folder = os.path.join( os.path.expanduser('~') , '.ComicStreamer')
        if folder is not None:
            folder = folder.decode(filename_encoding)
        return folder

Example 19

Project: makina-states.pack1 Source File: check_inotify.py
    def get_openfiles_counters(self):
        fs_enc = sys.getfilesystemencoding()
        ret, ps = popen('lsof')
        lines = ret[0].decode(fs_enc).splitlines()
        open_files = len(lines) - 1
        inot_files = len([line for line in lines  if 'anon_inode' in line])
        return {'open_files': open_files,
                'inotify_openfiles': inot_files}

Example 20

Project: rez Source File: misc.py
Function: fs_encode
    def fsencode(filename):
        if isinstance(filename, bytes):
            return filename
        elif isinstance(filename, str):
            return filename.encode(sys.getfilesystemencoding())
        else:
            raise TypeError("expect bytes or str, not %s" %
                            type(filename).__name__)

Example 21

Project: winpython Source File: utils.py
def decode_fs_string(string):
    """Convert string from file system charset to unicode"""
    charset = sys.getfilesystemencoding()
    if charset is None:
        charset = locale.getpreferredencoding()
    return string.decode(charset)

Example 22

Project: mavelous Source File: sessions.py
Function: init
    def __init__(self, path=None, filename_template='werkzeug_%s.sess',
                 session_class=None, renew_missing=False, mode=0644):
        SessionStore.__init__(self, session_class)
        if path is None:
            path = tempfile.gettempdir()
        self.path = path
        if isinstance(filename_template, unicode):
            filename_template = filename_template.encode(
                sys.getfilesystemencoding() or 'utf-8')
        assert not filename_template.endswith(_fs_transaction_suffix), \
            'filename templates may not end with %s' % _fs_transaction_suffix
        self.filename_template = filename_template
        self.renew_missing = renew_missing
        self.mode = mode

Example 23

Project: sublime-wakatime Source File: stats.py
def number_lines_in_file(file_name):
    lines = 0
    try:
        with open(file_name, 'r', encoding='utf-8') as fh:
            for line in fh:
                lines += 1
    except:  # pragma: nocover
        try:
            with open(file_name, 'r', encoding=sys.getfilesystemencoding()) as fh:
                for line in fh:
                    lines += 1
        except:
            return None
    return lines

Example 24

Project: entropy Source File: misc.py
Function: encode_path
    def _encode_path(self, path):
        """
        Encode path using proper encoding for use with os functions.
        """
        try:
            path = const_convert_to_rawstring(
                path, from_enctype=etpConst['conf_encoding'])
        except (UnicodeEncodeError,):
            path = const_convert_to_rawstring(
                path, from_enctype=sys.getfilesystemencoding())
        return path

Example 25

Project: spotter Source File: spotter.py
Function: process_default
    def process_default(self, event):
        """Run the commands that have a pattern matching the events path

        Stops running commands once one fails or is marked as final"""
        path = event.pathname.decode(sys.getfilesystemencoding())

        for watchlist in self.watchlists:
            for watch in watchlist:
                if watch.pattern_matches(os.path.relpath(path)):
                    success = self.run(
                        watch.command,
                        filename=path, **watchlist.definitions)
                    # Always stop if the watch was final
                    if watch.final:
                        break
                    # Stop if we failed without --continue-on-fail
                    if not (success or self.options.continue_on_fail):
                        break

Example 26

Project: django-icanhaz Source File: finders.py
Function: get_app_template_dirs
def _get_app_template_dirs():
    fs_encoding = sys.getfilesystemencoding() or sys.getdefaultencoding()
    ret = []
    for app in conf.INSTALLED_APPS:
        try:
            mod = import_module(app)
        except ImportError:
            e = sys.exc_info()[1]
            raise ImproperlyConfigured("ImportError %s: %s" % (app, e.args[0]))
        app_dir = os.path.dirname(mod.__file__)
        for dirname in conf.ICANHAZ_APP_DIRNAMES:
            template_dir = os.path.join(app_dir, dirname)
            if os.path.isdir(template_dir):
                # Only python 2.x needs decoding
                if isinstance(template_dir, bytes):
                    template_dir = template_dir.decode(fs_encoding)
                ret.append(template_dir)
    return ret

Example 27

Project: databus Source File: glob.py
def glob1(dirname, pattern):
    if not dirname:
        dirname = os.curdir
    if isinstance(pattern, unicode) and not isinstance(dirname, unicode):
        dirname = unicode(dirname, sys.getfilesystemencoding() or
                                   sys.getdefaultencoding())
    try:
        names = os.listdir(dirname)
    except os.error:
        return []
    if pattern[0] != '.':
        names = filter(lambda x: x[0] != '.', names)
    return fnmatch.filter(names, pattern)

Example 28

Project: Veil-Evasion Source File: test_archive_util.py
def can_fs_encode(filename):
    """
    Return True if the filename can be saved in the file system.
    """
    if os.path.supports_unicode_filenames:
        return True
    try:
        filename.encode(sys.getfilesystemencoding())
    except UnicodeEncodeError:
        return False
    return True

Example 29

Project: vim-epub Source File: path.py
    @classmethod
    def _always_unicode(cls, path):
        """
        Ensure the path as retrieved from a Python API, such as os.listdir,
        is a proper Unicode string.
        """
        if PY3 or isinstance(path, unicode):
            return path
        return path.decode(sys.getfilesystemencoding(), 'surrogateescape')

Example 30

Project: komodo-wakatime Source File: git.py
Function: branch
    def branch(self):
        base = self._project_base()
        if base:
            head = os.path.join(self._project_base(), '.git', 'HEAD')
            try:
                with open(head, 'r', encoding='utf-8') as fh:
                    return u(fh.readline().strip().rsplit('/', 1)[-1])
            except UnicodeDecodeError:  # pragma: nocover
                try:
                    with open(head, 'r', encoding=sys.getfilesystemencoding()) as fh:
                        return u(fh.readline().strip().rsplit('/', 1)[-1])
                except:
                    log.traceback('warn')
            except IOError:  # pragma: nocover
                log.traceback('warn')
        return u('master')

Example 31

Project: ZeroNet Source File: SiteStorage.py
    def __init__(self, site, allow_create=True):
        self.site = site
        self.directory = "%s/%s" % (config.data_dir, self.site.address)  # Site data diretory
        self.allowed_dir = os.path.abspath(self.directory.decode(sys.getfilesystemencoding()))  # Only serve file within this dir
        self.log = site.log
        self.db = None  # Db class
        self.db_checked = False  # Checked db tables since startup
        self.event_db_busy = None  # Gevent AsyncResult if db is working on rebuild
        self.has_db = self.isFile("dbschema.json")  # The site has schema

        if not os.path.isdir(self.directory):
            if allow_create:
                os.mkdir(self.directory)  # Create directory if not found
            else:
                raise Exception("Directory not exists: %s" % self.directory)

Example 32

Project: Gooey Source File: processor.py
  def run(self, command):
    env = os.environ.copy()
    env["GOOEY"] = "1"
    self._process = subprocess.Popen(
      command.encode(sys.getfilesystemencoding()),
      bufsize=1, stdout=subprocess.PIPE,
      stderr=subprocess.STDOUT, shell=True, env=env)
    Pool(1).apply_async(self._forward_stdout, (self._process,))

Example 33

Project: pandocfilters Source File: pandocfilters.py
def get_filename4code(module, content, ext=None):
    """Generate filename based on content

    The function ensures that the (temporary) directory exists, so that the
    file can be written.

    Example:
        filename = get_filename4code("myfilter", code)
    """
    imagedir = module + "-images"
    fn = hashlib.sha1(content.encode(sys.getfilesystemencoding())).hexdigest()
    try:
        os.mkdir(imagedir)
        sys.stderr.write('Created directory ' + imagedir + '\n')
    except OSError:
        pass
    if ext:
        fn += "." + ext
    return os.path.join(imagedir, fn)

Example 34

Project: scandir Source File: test_scandir.py
Function: test_bytes
    def test_bytes(self):
        # Check that unicode filenames are returned correctly as bytes in output
        path = os.path.join(TEST_PATH, 'subdir').encode(sys.getfilesystemencoding(), 'replace')
        self.assertTrue(isinstance(path, bytes))
        if IS_PY3 and sys.platform == 'win32':
            self.assertRaises(TypeError, self.scandir_func, path)
            return

        entries = [e for e in self.scandir_func(path) if e.name.startswith(b'unicod')]
        self.assertEqual(len(entries), 1)
        entry = entries[0]

        self.assertTrue(isinstance(entry.name, bytes))
        self.assertTrue(isinstance(entry.path, bytes))

        # b'unicod?.txt' on Windows, b'unicod\xc6\x8f.txt' (UTF-8) or similar on POSIX
        entry_name = 'unicod\u018f.txt'.encode(sys.getfilesystemencoding(), 'replace')
        self.assertEqual(entry.name, entry_name)
        self.assertEqual(entry.path, os.path.join(path, entry_name))

Example 35

Project: powerline Source File: encoding.py
def get_preferred_file_name_encoding():
	'''Get preferred file name encoding
	'''
	return (
		sys.getfilesystemencoding()
		or locale.getpreferredencoding()
		or 'utf-8'
	)

Example 36

Project: PySyncObj Source File: atomic_replace.py
Function: atomic_replace
    def atomicReplace(oldPath, newPath):
        if not isinstance(oldPath, str):
            oldPath = str(oldPath, sys.getfilesystemencoding())
        if not isinstance(newPath, str):
            newPath = str(newPath, sys.getfilesystemencoding())
        ta = CreateTransaction(None, 0, 0, 0, 0, 1000, 'atomic_replace')
        if ta == -1:
            return False
        res = MoveFileTransacted(oldPath, newPath, None, None, MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH, ta)
        if not res:
            CloseHandle(ta)
            return False
        res = CommitTransaction(ta)
        CloseHandle(ta)
        return bool(res)

Example 37

Project: cython Source File: Utils.py
Function: decode_filename
def decode_filename(filename):
    if isinstance(filename, bytes):
        try:
            filename_encoding = sys.getfilesystemencoding()
            if filename_encoding is None:
                filename_encoding = sys.getdefaultencoding()
            filename = filename.decode(filename_encoding)
        except UnicodeDecodeError:
            pass
    return filename

Example 38

Project: OpenSesame Source File: plugins.py
def load_mod(path, mod, pkg=None):

	"""
	Dynamically loads a module from a path.

	Arguments:
	path		--	A folder or file name. If a filename is specified, the
					file's directory will be used as path.
	mod			--	The name of a module, which corresponds to the name of the
					source file without the `.py` extension.
	pkg			--	The module's package. This is effectively a subfolder that
					is added to the path. (default=None)

	Returns:
	A Python module.
	"""

	import imp
	path = safe_decode(path, enc=sys.getfilesystemencoding())
	if not os.path.isdir(path):
		path = os.path.dirname(path)
	if pkg is not None:
		path = os.path.join(path, pkg)
	path = os.path.join(path, mod+u'.py')
	if not os.path.exists(path):
		raise osexception(u'%s does not exist' % path)
	debug.msg(u'loading module from %s' % path)
	if not py3:
		mod = safe_encode(mod, enc=sys.getfilesystemencoding())
		path = safe_encode(path, enc=sys.getfilesystemencoding())
	return imp.load_source(mod, path)

Example 39

Project: luci-py Source File: arfile_test.py
Function: assert_cli
  def assertCLI(self, *initems, **kw):
    extra_args = kw.get('extra_args', [])

    indir = None
    ardir = None
    outdir = None
    try:
      indir = tempfile.mkdtemp().decode(sys.getfilesystemencoding())
      ardir = tempfile.mkdtemp().decode(sys.getfilesystemencoding())
      outdir = tempfile.mkdtemp().decode(sys.getfilesystemencoding())

      arp = os.path.join(ardir, 'out.ar')
      assert not os.path.exists(arp)

      # Write out a directory tree
      files = []
      for fp, contents in initems:
        fn = os.path.join(indir, fp)
        dn = os.path.dirname(fn)
        if not os.path.exists(dn):
          os.makedirs(dn)

        with file(fn, 'wb') as f:
          f.write(contents)

        files.append(fp)

      files.sort()
      fileslist = '\n'.join(files)

      # Create an archive from a directory
      self.runCLI(['create', '--filename', arp, indir] + extra_args)
      self.assertTrue(
          os.path.exists(arp), '%s file should exists' % arp)

      # List the archive contents
      output, _ = self.runCLI(['list', '--filename', arp])
      filesoutput = '\n'.join(sorted(output[:-1].split('\n')))
      self.assertMultiLineEqual(fileslist, filesoutput)

      # Extract the archive
      os.chdir(outdir)
      self.runCLI(['extract', '--filename', arp] + extra_args)

      # Walk the directory tree and collect the extracted output
      outitems = []
      for root, _, files in os.walk(outdir):
        for fn in files:
          fp = os.path.join(root, fn)
          outitems.append([fp[len(outdir)+1:], file(fp, 'rb').read()])

      # Check the two are equal
      self.assertSequenceEqual(sorted(initems), sorted(outitems))

    finally:
      if indir:
        shutil.rmtree(indir, ignore_errors=True)
      if ardir:
        shutil.rmtree(ardir, ignore_errors=True)
      if outdir:
        shutil.rmtree(outdir, ignore_errors=True)

Example 40

Project: peppy Source File: utils.py
def normalize(ref, base=None):
    """Normalize a url string into a reference and fix windows shenanigans"""
    if not isinstance(ref, Reference):
        if ref.startswith('file:'):
            # URLs always use /, so change windows path separators to forward
            # slashes
            try:
                ref = unicode(ref)
            except UnicodeDecodeError:
                try:
                    ref = str(ref).decode(sys.getfilesystemencoding())
                except UnicodeDecodeError:
                    ref = str(ref).decode('utf-8')
            #dprint(repr(ref))
            if os.path.sep == '\\':
                ref = ref.replace(os.path.sep, '/')
        ref = get_reference(ref)
    # If the reference is absolute (i.e.  contains a scheme), we return;
    # otherwise we assume it's a file:// URI
    #dprint(str(ref))
    if ref.scheme:
        return ref

    # Default to the current working directory
    if base is None:
        try:
            base = os.getcwd().decode(sys.getfilesystemencoding())
        except UnicodeDecodeError:
            base = os.getcwd().decode('utf-8')

    # URLs always use /
    if os.path.sep == '\\':
        base = base.replace(os.path.sep, '/')
    #dprint(base)
    # Check windows drive letters and add extra slash for correct URL syntax
    if len(base) > 1 and base[1] == ':':
        base = "/%s:%s" % (base[0].lower(), base[2:])
    baseref = get_reference(u'file://%s/' % base)
    try:
        path = unicode(ref.path)
    except UnicodeDecodeError:
        try:
            path = str(ref.path).decode(sys.getfilesystemencoding())
        except UnicodeDecodeError:
            path = str(ref.path).decode('utf-8')
    #dprint(repr(path))
    
    # Add the query string and fragment if they exist
    newref = baseref.resolve(path)
    newref.query = ref.query
    newref.fragment = ref.fragment
    #dprint(newref)
    return newref

Example 41

Project: PySide Source File: pysideinclude.py
    def run(self):
        docuement = self.state.docuement
        filename = self.arguments[0]
        if not docuement.settings.file_insertion_enabled:
            return [docuement.reporter.warning('File insertion disabled',
                                              line=self.lineno)]
        env = docuement.settings.env
        if filename.startswith('/') or filename.startswith(os.sep):
            rel_fn = filename[1:]
        else:
            docdir = path.dirname(env.doc2path(env.docname, base=None))
            rel_fn = path.join(docdir, filename)
        try:
            fn = path.join(env.srcdir, rel_fn)
        except UnicodeDecodeError:
            # the source directory is a bytestring with non-ASCII characters;
            # let's try to encode the rel_fn in the file system encoding
            rel_fn = rel_fn.encode(sys.getfilesystemencoding())
            fn = path.join(env.srcdir, rel_fn)

        if 'pyobject' in self.options and 'lines' in self.options:
            return [docuement.reporter.warning(
                'Cannot use both "pyobject" and "lines" options',
                line=self.lineno)]

        encoding = self.options.get('encoding', env.config.source_encoding)
        codec_info = codecs.lookup(encoding)
        try:
            f = codecs.StreamReaderWriter(open(fn, 'U'),
                    codec_info[2], codec_info[3], 'strict')
            lines = f.readlines()
            f.close()
        except (IOError, OSError):
            return [docuement.reporter.warning(
                'Include file %r not found or reading it failed' % filename,
                line=self.lineno)]
        except UnicodeError:
            return [docuement.reporter.warning(
                'Encoding %r used for reading included file %r seems to '
                'be wrong, try giving an :encoding: option' %
                (encoding, filename))]

        objectname = self.options.get('pyobject')
        if objectname is not None:
            from sphinx.pycode import ModuleAnalyzer
            analyzer = ModuleAnalyzer.for_file(fn, '')
            tags = analyzer.find_tags()
            if objectname not in tags:
                return [docuement.reporter.warning(
                    'Object named %r not found in include file %r' %
                    (objectname, filename), line=self.lineno)]
            else:
                lines = lines[tags[objectname][1]-1 : tags[objectname][2]-1]

        linespec = self.options.get('lines')
        if linespec is not None:
            try:
                linelist = parselinenos(linespec, len(lines))
            except ValueError, err:
                return [docuement.reporter.warning(str(err), line=self.lineno)]
            lines = [lines[i] for i in linelist]

        startafter = self.options.get('start-after')
        endbefore  = self.options.get('end-before')
        prepend    = self.options.get('prepend')
        append     = self.options.get('append')
        snippet    = self.options.get('snippet')

        if snippet:
            startafter = "//![%s]" % snippet
            endbefore = "//![%s]" % snippet

        if startafter is not None or endbefore is not None:
            use = not startafter
            res = []
            for line in lines:
                if not use and startafter and startafter in line:
                    use = True
                elif use and endbefore and endbefore in line:
                    use = False
                    break
                elif use:
                    if not line.startswith("//!"):
                        res.append(line)
            lines = res

        if prepend:
           lines.insert(0, prepend + '\n')
        if append:
           lines.append(append + '\n')

        text = ''.join(lines)
        if self.options.get('tab-width'):
            text = text.expandtabs(self.options['tab-width'])
        retnode = nodes.literal_block(text, text, source=fn)
        retnode.line = 1
        retnode.attributes['line_number'] = self.lineno
        if self.options.get('language', ''):
            retnode['language'] = self.options['language']
        if 'linenos' in self.options:
            retnode['linenos'] = True
        docuement.settings.env.note_dependency(rel_fn)
        return [retnode]

Example 42

Project: Pyfa Source File: config.py
def defPaths(customSavePath):
    global debug
    global pyfaPath
    global savePath
    global saveDB
    global gameDB
    global saveInRoot

    if debug:
        logLevel = logging.DEBUG
    else:
        logLevel = logging.WARN

    # The main pyfa directory which contains run.py
    # Python 2.X uses ANSI by default, so we need to convert the character encoding
    pyfaPath = getattr(configforced, "pyfaPath", pyfaPath)
    if pyfaPath is None:
        pyfaPath = getPyfaRoot()

    # Where we store the saved fits etc, default is the current users home directory
    if saveInRoot is True:
        savePath = getattr(configforced, "savePath", None)
        if savePath is None:
            savePath = os.path.join(pyfaPath, "saveddata")
    else:
        savePath = getattr(configforced, "savePath", None)
        if savePath is None:
            if customSavePath is None: # customSavePath is not overriden
                savePath = unicode(os.path.expanduser(os.path.join("~", ".pyfa")),
                               sys.getfilesystemencoding())
            else:
                savePath = customSavePath

    __createDirs(savePath)

    if isFrozen():
        os.environ["REQUESTS_CA_BUNDLE"] = os.path.join(pyfaPath, "cacert.pem")
        os.environ["SSL_CERT_FILE"] = os.path.join(pyfaPath, "cacert.pem")

    format = '%(asctime)s %(name)-24s %(levelname)-8s %(message)s'
    logging.basicConfig(format=format, level=logLevel)
    handler = logging.handlers.RotatingFileHandler(os.path.join(savePath, "log.txt"), maxBytes=1000000, backupCount=3)
    formatter = logging.Formatter(format)
    handler.setFormatter(formatter)
    logging.getLogger('').addHandler(handler)

    logging.info("Starting pyfa")

    if hasattr(sys, 'frozen'):
        stdout_logger = logging.getLogger('STDOUT')
        sl = StreamToLogger(stdout_logger, logging.INFO)
        sys.stdout = sl

        # This interferes with cx_Freeze's own handling of exceptions. Find a way to fix this.
        #stderr_logger = logging.getLogger('STDERR')
        #sl = StreamToLogger(stderr_logger, logging.ERROR)
        #sys.stderr = sl

    # The database where we store all the fits etc
    saveDB = os.path.join(savePath, "saveddata.db")

    # The database where the static EVE data from the datadump is kept.
    # This is not the standard sqlite datadump but a modified version created by eos
    # maintenance script
    gameDB = os.path.join(pyfaPath, "eve.db")

    ## DON'T MODIFY ANYTHING BELOW ##
    import eos.config

    #Caching modifiers, disable all gamedata caching, its unneeded.
    eos.config.gamedataCache = False
    # saveddata db location modifier, shouldn't ever need to touch this
    eos.config.saveddata_connectionstring = "sqlite:///" + saveDB + "?check_same_thread=False"
    eos.config.gamedata_connectionstring = "sqlite:///" + gameDB + "?check_same_thread=False"

Example 43

Project: pyspeckit Source File: ah_bootstrap.py
Function: init
    def __init__(self, path=None, index_url=None, use_git=None, offline=None,
                 download_if_needed=None, auto_upgrade=None):

        if path is None:
            path = PACKAGE_NAME

        if not (isinstance(path, _str_types) or path is False):
            raise TypeError('path must be a string or False')

        if PY3 and not isinstance(path, _text_type):
            fs_encoding = sys.getfilesystemencoding()
            path = path.decode(fs_encoding)  # path to unicode

        self.path = path

        # Set other option attributes, using defaults where necessary
        self.index_url = index_url if index_url is not None else INDEX_URL
        self.offline = offline if offline is not None else OFFLINE

        # If offline=True, override download and auto-upgrade
        if self.offline:
            download_if_needed = False
            auto_upgrade = False

        self.download = (download_if_needed
                         if download_if_needed is not None
                         else DOWNLOAD_IF_NEEDED)
        self.auto_upgrade = (auto_upgrade
                             if auto_upgrade is not None else AUTO_UPGRADE)

        # If this is a release then the .git directory will not exist so we
        # should not use git.
        git_dir_exists = os.path.exists(os.path.join(os.path.dirname(__file__), '.git'))
        if use_git is None and not git_dir_exists:
            use_git = False

        self.use_git = use_git if use_git is not None else USE_GIT
        # Declared as False by default--later we check if astropy-helpers can be
        # upgraded from PyPI, but only if not using a source distribution (as in
        # the case of import from a git submodule)
        self.is_submodule = False

Example 44

Project: python-future Source File: server.py
Function: list_directory
    def list_directory(self, path):
        """Helper to produce a directory listing (absent index.html).

        Return value is either a file object, or None (indicating an
        error).  In either case, the headers are sent, making the
        interface the same as for send_head().

        """
        try:
            list = os.listdir(path)
        except os.error:
            self.send_error(404, "No permission to list directory")
            return None
        list.sort(key=lambda a: a.lower())
        r = []
        displaypath = html.escape(urllib_parse.unquote(self.path))
        enc = sys.getfilesystemencoding()
        title = 'Directory listing for %s' % displaypath
        r.append('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" '
                 '"http://www.w3.org/TR/html4/strict.dtd">')
        r.append('<html>\n<head>')
        r.append('<meta http-equiv="Content-Type" '
                 'content="text/html; charset=%s">' % enc)
        r.append('<title>%s</title>\n</head>' % title)
        r.append('<body>\n<h1>%s</h1>' % title)
        r.append('<hr>\n<ul>')
        for name in list:
            fullname = os.path.join(path, name)
            displayname = linkname = name
            # Append / for directories or @ for symbolic links
            if os.path.isdir(fullname):
                displayname = name + "/"
                linkname = name + "/"
            if os.path.islink(fullname):
                displayname = name + "@"
                # Note: a link to a directory displays with @ and links with /
            r.append('<li><a href="%s">%s</a></li>'
                    % (urllib_parse.quote(linkname), html.escape(displayname)))
            # # Use this instead:
            # r.append('<li><a href="%s">%s</a></li>'
            #         % (urllib.quote(linkname), cgi.escape(displayname)))
        r.append('</ul>\n<hr>\n</body>\n</html>\n')
        encoded = '\n'.join(r).encode(enc)
        f = io.BytesIO()
        f.write(encoded)
        f.seek(0)
        self.send_response(200)
        self.send_header("Content-type", "text/html; charset=%s" % enc)
        self.send_header("Content-Length", str(len(encoded)))
        self.end_headers()
        return f

Example 45

Project: PokemonGo-Bot-Desktop Source File: posixpath.py
Function: expand_vars
def expandvars(path):
    """Expand shell variables of form $var and ${var}.  Unknown variables
    are left unchanged."""
    global _varprog, _uvarprog
    if '$' not in path:
        return path
    if isinstance(path, _unicode):
        if not _uvarprog:
            import re
            _uvarprog = re.compile(ur'\$(\w+|\{[^}]*\})', re.UNICODE)
        varprog = _uvarprog
        encoding = sys.getfilesystemencoding()
    else:
        if not _varprog:
            import re
            _varprog = re.compile(r'\$(\w+|\{[^}]*\})')
        varprog = _varprog
        encoding = None
    i = 0
    while True:
        m = varprog.search(path, i)
        if not m:
            break
        i, j = m.span(0)
        name = m.group(1)
        if name.startswith('{') and name.endswith('}'):
            name = name[1:-1]
        if encoding:
            name = name.encode(encoding)
        if name in os.environ:
            tail = path[j:]
            value = os.environ[name]
            if encoding:
                value = value.decode(encoding)
            path = path[:i] + value
            i = len(path)
            path += tail
        else:
            i = j
    return path

Example 46

Project: pyfilesystem Source File: runner.py
    def run(self):
        parser = self.get_optparse()
        options, args = parser.parse_args()
        self.options = options

        if options.listopeners:
            self.list_openers()
            return 0

        ilocals = {}
        if options.fs:
            for import_opener in options.fs:
                module_name, opener_class = import_opener.rsplit('.', 1)
                try:
                    opener_module = __import__(module_name, globals(), ilocals, [opener_class], -1)
                except ImportError:
                    self.error("Unable to import opener %s\n" % import_opener)
                    return 0

                new_opener = getattr(opener_module, opener_class)

                try:
                    if not issubclass(new_opener, Opener):
                        self.error('%s is not an fs.opener.Opener\n' % import_opener)
                        return 0
                except TypeError:
                    self.error('%s is not an opener class\n' % import_opener)
                    return 0

                if options.verbose:
                    self.output('Imported opener %s\n' % import_opener)

                opener.add(new_opener)

        if not six.PY3:
            args = [unicode(arg, sys.getfilesystemencoding()) for arg in args]
        self.verbose = options.verbose
        try:
            return self.do_run(options, args) or 0
        except FSError, e:
            self.error(self.wrap_error(unicode(e)) + '\n')
            if options.debug:
                raise
            return 1
        except KeyboardInterrupt:
            if self.is_terminal():
                self.output("\n")
            return 0
        except SystemExit:
            return 0
        except Exception, e:
            self.error(self.wrap_error('Error - %s\n' % unicode(e)))
            if options.debug:
                raise
            return 1

Example 47

Project: myks-gallery Source File: views.py
def serve_private_media(request, path):
    """Serve a private media file with the webserver's "sendfile" if possible.

    Here's an example of how to use this function. The 'Docuement' model tracks
    files. It provides a 'get_file_path' method returning the absolute path to
    the actual file. The following view serves file only to users having the
    'can_download' permission::

        @permission_required('docuements.can_download')
        def download_docuement(request, docuement_id):
            path = Docuement.objects.get(pk=docuement_id).get_file_path()
            return serve_private_media(request, path)

    If ``DEBUG`` is ``False`` and ``settings.GALLERY_SENDFILE_HEADER`` is set,
    this function sets a header and doesn't send the actual contents of the
    file. Use ``'X-Accel-Redirect'`` for nginx and ``'X-SendFile'`` for Apache
    with mod_xsendfile. Otherwise, this function behaves like Django's static
    serve view.

    ``path`` must be an absolute path. Depending on your webserver's
    configuration, you might want a full path or a relative path in the
    header's value. ``settings.GALLERY_SENDFILE_ROOT`` will be stripped from
    the beginning of the path to create the header's value.
    """

    if not os.path.exists(path):
        # Don't reveal the file name on the filesystem.
        raise Http404("Requested file doesn't exist.")

    # begin copy-paste from django.views.static.serve
    statobj = os.stat(path)
    content_type, encoding = mimetypes.guess_type(path)
    content_type = content_type or 'application/octet-stream'
    if not was_modified_since(request.META.get('HTTP_IF_MODIFIED_SINCE'),
                              statobj.st_mtime, statobj.st_size):   # pragma: no cover
        return HttpResponseNotModified()
    # pause copy-paste from django.views.static.serve

    sendfile_header = getattr(settings, 'GALLERY_SENDFILE_HEADER', '')
    sendfile_root = getattr(settings, 'GALLERY_SENDFILE_ROOT', '')

    if settings.DEBUG or not sendfile_header:
        response = StreamingHttpResponse(open(path, 'rb'), content_type=content_type)
    else:
        response = HttpResponse('', content_type=content_type)
        if sendfile_root:
            if not path.startswith(sendfile_root):
                raise ValueError("Requested file isn't under GALLERY_SENDFILE_ROOT.")
            path = path[len(sendfile_root):]
        response[sendfile_header] = path.encode(sys.getfilesystemencoding())

    # resume copy-paste from django.views.static.serve
    response["Last-Modified"] = http_date(statobj.st_mtime)
    if stat.S_ISREG(statobj.st_mode):                       # pragma: no cover
        response["Content-Length"] = statobj.st_size
    if encoding:                                            # pragma: no cover
        response["Content-Encoding"] = encoding
    # end copy-paste from django.views.static.serve

    return response

Example 48

Project: PokemonGo-Bot-Desktop Source File: ntpath.py
def expandvars(path):
    """Expand shell variables of the forms $var, ${var} and %var%.

    Unknown variables are left unchanged."""
    if '$' not in path and '%' not in path:
        return path
    import string
    varchars = string.ascii_letters + string.digits + '_-'
    if isinstance(path, _unicode):
        encoding = sys.getfilesystemencoding()
        def getenv(var):
            return os.environ[var.encode(encoding)].decode(encoding)
    else:
        def getenv(var):
            return os.environ[var]
    res = ''
    index = 0
    pathlen = len(path)
    while index < pathlen:
        c = path[index]
        if c == '\'':   # no expansion within single quotes
            path = path[index + 1:]
            pathlen = len(path)
            try:
                index = path.index('\'')
                res = res + '\'' + path[:index + 1]
            except ValueError:
                res = res + c + path
                index = pathlen - 1
        elif c == '%':  # variable or '%'
            if path[index + 1:index + 2] == '%':
                res = res + c
                index = index + 1
            else:
                path = path[index+1:]
                pathlen = len(path)
                try:
                    index = path.index('%')
                except ValueError:
                    res = res + '%' + path
                    index = pathlen - 1
                else:
                    var = path[:index]
                    try:
                        res = res + getenv(var)
                    except KeyError:
                        res = res + '%' + var + '%'
        elif c == '$':  # variable or '$$'
            if path[index + 1:index + 2] == '$':
                res = res + c
                index = index + 1
            elif path[index + 1:index + 2] == '{':
                path = path[index+2:]
                pathlen = len(path)
                try:
                    index = path.index('}')
                    var = path[:index]
                    try:
                        res = res + getenv(var)
                    except KeyError:
                        res = res + '${' + var + '}'
                except ValueError:
                    res = res + '${' + path
                    index = pathlen - 1
            else:
                var = ''
                index = index + 1
                c = path[index:index + 1]
                while c != '' and c in varchars:
                    var = var + c
                    index = index + 1
                    c = path[index:index + 1]
                try:
                    res = res + getenv(var)
                except KeyError:
                    res = res + '$' + var
                if c != '':
                    index = index - 1
        else:
            res = res + c
        index = index + 1
    return res

Example 49

Project: pyload Source File: pyload_app.py
@route("/filechooser")
@route("/pathchooser")
@route("/filechooser/:file#.+#")
@route("/pathchooser/:path#.+#")
@login_required('STATUS')
def path(file="", path=""):
    if file:
        type = "file"
    else:
        type = "folder"

    path = os.path.normpath(unquotepath(path))

    if os.path.isfile(path):
        oldfile = path
        path = os.path.dirname(path)
    else:
        oldfile = ''

    abs = False

    if os.path.isdir(path):
        if os.path.isabs(path):
            cwd = os.path.abspath(path)
            abs = True
        else:
            cwd = relpath(path)
    else:
        cwd = os.getcwd()

    try:
        cwd = cwd.encode("utf8")
    except:
        pass

    cwd = os.path.normpath(os.path.abspath(cwd))
    parentdir = os.path.dirname(cwd)
    if not abs:
        if os.path.abspath(cwd) == "/":
            cwd = relpath(cwd)
        else:
            cwd = relpath(cwd) + os.path.sep
        parentdir = relpath(parentdir) + os.path.sep

    if os.path.abspath(cwd) == "/":
        parentdir = ""

    try:
        folders = os.listdir(cwd)
    except:
        folders = []

    files = []

    for f in folders:
        try:
            f = f.decode(getfilesystemencoding())
            data = {'name': f, 'fullpath': join(cwd, f)}
            data['sort'] = data['fullpath'].lower()
            data['modified'] = datetime.fromtimestamp(int(os.path.getmtime(join(cwd, f))))
            data['ext'] = os.path.splitext(f)[1]
        except:
            continue

        if os.path.isdir(join(cwd, f)):
            data['type'] = 'dir'
        else:
            data['type'] = 'file'

        if os.path.isfile(join(cwd, f)):
            data['size'] = os.path.getsize(join(cwd, f))

            power = 0
            while (data['size'] / 1024) > 0.3:
                power += 1
                data['size'] /= 1024.
            units = ('', 'K', 'M', 'G', 'T')
            data['unit'] = units[power] + 'Byte'
        else:
            data['size'] = ''

        files.append(data)

    files = sorted(files, key=itemgetter('type', 'sort'))

    return render_to_response('pathchooser.html',
            {'cwd': cwd, 'files': files, 'parentdir': parentdir, 'type': type, 'oldfile': oldfile,
             'absolute': abs}, [])

Example 50

Project: pymo Source File: mac_scrap.py
Function: put
def put(scrap_type, thing):
    board = NSPasteboard.generalPasteboard()
    if scrap_type == SCRAP_TEXT:
        board.declareTypes_owner_([NSStringPboardType, ScrapPboardType], None)
        if isinstance(thing, unicode):
            text_thing = thing
        else:
            text_thing = unicode(thing, 'utf-8')
        board.setString_forType_(text_thing, NSStringPboardType)
        board.setString_forType_(unicode_(''), ScrapPboardType)
    elif 1:
        raise NotImplementedError(err)





    elif 0 and scrap_type == SCRAP_BMP:
        # Don't use this code... we put the data in as a string.

        #if type(thing) != type(pygame.Surface((1,1))):
        #    thing = pygame.image.fromstring(thing, len(thing) * 4, "RGBA")


        # This is pretty silly, we shouldn't have to do this...
        fh = tempfile.NamedTemporaryFile(suffix='.png')
        pygame.image.save(thing, fh.name)
        path = fh.name
        if not isinstance(path, unicode):
            path = unicode(path, sys.getfilesystemencoding())
        img = NSImage.alloc().initByReferencingFile_(path)
        tiff = img.TIFFRepresentation()
        fh.close()
        board.declareTypes_owner_([NSTIFFPboardType, ScrapPboardType], None)
        board.setData_forType_(tiff, NSTIFFPboardType)
        board.setString_forType_(unicode_(''), ScrapPboardType)
    elif scrap_type == SCRAP_BMP:
        
        other_type = scrap_type
        board.declareTypes_owner_([other_type], None)
        board.setData_forType_(thing, other_type)

    else:
        other_type = scrap_type
        if 0:
            board.declareTypes_owner_([NSStringPboardType, other_type], None)
            board.setString_forType_(text_thing, NSStringPboardType)
        elif 0:
            board.declareTypes_owner_([other_type], None)
            #board.setString_forType_(thing, other_type)
            board.setData_forType_(thing, other_type)
        else:
            board.declareTypes_owner_([NSStringPboardType, other_type], None)
            board.setString_forType_(thing, NSStringPboardType)
See More Examples - Go to Next Page
Page 1 Selected Page 2 Page 3