[pyzo] 26/68: Move pyzolib.paths module to pyzo/util

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Wed Sep 28 09:47:09 UTC 2016


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to branch debian/master
in repository pyzo.

commit 4ee3cb5e06793826ff67a5a0a27822cef557df80
Author: Almar Klein <almar.klein at gmail.com>
Date:   Mon Sep 5 11:14:19 2016 +0200

    Move pyzolib.paths module to pyzo/util
---
 pyzo/__init__.py                   |   3 +-
 pyzo/__main__.py                   |  15 +-
 pyzo/core/about.py                 |   3 +-
 pyzo/core/main.py                  |   3 +-
 pyzo/core/menu.py                  |   6 +-
 pyzo/util/interpreters/__init__.py |   2 +-
 pyzo/util/paths.py                 | 284 +++++++++++++++++++++++++++++++++++++
 7 files changed, 304 insertions(+), 12 deletions(-)

diff --git a/pyzo/__init__.py b/pyzo/__init__.py
index 354618c..0d05bfd 100644
--- a/pyzo/__init__.py
+++ b/pyzo/__init__.py
@@ -60,6 +60,7 @@ elif pyzolib.__version__ < '0.2.9':
 
 # Import yoton as an absolute package
 from pyzo import yotonloader
+from pyzo.util import paths
 
 # If there already is an instance of Pyzo, and the user is trying an
 # Pyzo command, we should send the command to the other process and quit.
@@ -78,7 +79,7 @@ else:
         print('Our command server is *not* running')
 
 
-from pyzolib import ssdf, paths
+from pyzolib import ssdf
 from pyzolib.qt import QtCore, QtGui
 
 # Import language/translation tools
diff --git a/pyzo/__main__.py b/pyzo/__main__.py
index 64e2221..fb7782f 100755
--- a/pyzo/__main__.py
+++ b/pyzo/__main__.py
@@ -24,12 +24,21 @@ import sys
 # Import them now, so they are available in the frozen app.
 import shutil
 
+import os, sys
+def application_dir():
+    # Compacted version of pyzo.util.paths.application_dir()
+    if not sys.path or not sys.path[0]:
+        raise RuntimeError('Cannot determine app path because sys.path[0] is empty!')
+    thepath = sys.path[0]
+    if getattr(sys, 'frozen', None):
+        thepath = os.path.dirname(thepath)
+    return os.path.abspath(thepath)
+
 
 if hasattr(sys, 'frozen') and sys.frozen:
     # Enable loading from source
-    from pyzolib import paths
-    sys.path.insert(0, os.path.join(paths.application_dir(), 'source'))
-    sys.path.insert(0, os.path.join(paths.application_dir(), 'source/more'))
+    sys.path.insert(0, os.path.join(application_dir(), 'source'))
+    sys.path.insert(0, os.path.join(application_dir(), 'source/more'))
     # Import
     import pyzo
 
diff --git a/pyzo/core/about.py b/pyzo/core/about.py
index c84ec87..283b910 100644
--- a/pyzo/core/about.py
+++ b/pyzo/core/about.py
@@ -3,11 +3,10 @@ import os
 import sys
 
 import pyzolib
-from pyzolib import paths
 from pyzolib.qt import QtCore, QtGui
 
 import pyzo
-
+from pyzo.util import paths
 
 
 class AboutDialog(QtGui.QDialog):
diff --git a/pyzo/core/main.py b/pyzo/core/main.py
index 12d88c1..705d358 100644
--- a/pyzo/core/main.py
+++ b/pyzo/core/main.py
@@ -15,13 +15,14 @@ function which is also defined here.
 import os, sys, time
 import base64
 from queue import Queue, Empty
-from pyzolib import ssdf, paths
+from pyzolib import ssdf
 
 import pyzo
 from pyzo.core.icons import IconArtist
 from pyzo.core import commandline
 from pyzolib.qt import QtCore, QtGui
 from pyzo.core.splash import SplashWidget
+from pyzo.util import paths
 
 
 class MainWindow(QtGui.QMainWindow):
diff --git a/pyzo/core/menu.py b/pyzo/core/menu.py
index 58cbc0b..15783d8 100644
--- a/pyzo/core/menu.py
+++ b/pyzo/core/menu.py
@@ -15,8 +15,7 @@ shortcuts.
 import os, sys, re, time
 import unicodedata
 import datetime
-
-from pyzolib import paths
+import webbrowser
 
 from pyzolib.qt import QtCore, QtGui
 
@@ -24,11 +23,10 @@ import pyzo
 from pyzo.core.compactTabWidget import CompactTabWidget
 from pyzo.core.pyzoLogging import print
 from pyzo.core.assistant import PyzoAssistant
-import webbrowser
+from pyzo.util import paths
 from pyzo import translate
 
 
-
 def buildMenus(menuBar):
     """
     Build all the menus
diff --git a/pyzo/util/interpreters/__init__.py b/pyzo/util/interpreters/__init__.py
index 3a5b77c..95110e2 100644
--- a/pyzo/util/interpreters/__init__.py
+++ b/pyzo/util/interpreters/__init__.py
@@ -9,11 +9,11 @@ registry, and conda's environment list.
 
 import sys
 import os
-from pyzolib import paths
 from pyzolib import ssdf
 
 from .pythoninterpreter import EXE_DIR, PythonInterpreter, versionStringToTuple
 from .inwinreg import get_interpreters_in_reg
+from .. import paths
 
 
 def get_interpreters(minimumVersion=None):
diff --git a/pyzo/util/paths.py b/pyzo/util/paths.py
new file mode 100644
index 0000000..c7f3e57
--- /dev/null
+++ b/pyzo/util/paths.py
@@ -0,0 +1,284 @@
+# -*- coding: utf-8 -*-
+# Copyright (c) 2016, Almar Klein, Rob Reilink
+#
+# This file is distributed under the terms of the (new) BSD License.
+
+""" Module paths
+
+Get paths to useful directories in a cross platform manner. The functions
+in this module are designed to be stand-alone, so that they can easily
+be copied and used in code that does not want pyzo as a dependency.
+
+This code was first part of pyzolib, and later moved to pyzo.
+
+"""
+
+# Notes:
+# * site.getusersitepackages() returns a dir in roaming userspace on Windows
+#   so better avoid that.
+# * site.getuserbase() returns appdata_dir('Python', True)
+# * See docstring: that's why the functions tend to not re-use each-other
+
+import sys
+
+ISWIN = sys.platform.startswith('win')
+ISMAC = sys.platform.startswith('darwin')
+ISLINUX = sys.platform.startswith('linux')
+
+PY2 = sys.version_info[0] == 2
+PY3 = sys.version_info[0] == 3
+
+
+
+# From pyzolib/paths.py (https://bitbucket.org/pyzo/pyzolib/src/tip/paths.py)
+import sys
+def is_frozen():
+    """ is_frozen()
+    Return whether this app is a frozen application (using e.g. cx_freeze).
+    """
+    return bool( getattr(sys, 'frozen', None) )
+
+
+
+# From pyzolib/paths.py (https://bitbucket.org/pyzo/pyzolib/src/tip/paths.py)
+import os, sys, tempfile
+def temp_dir(appname=None, nospaces=False):
+    """ temp_dir(appname=None, nospaces=False)
+    Get path to a temporary directory with write access. 
+    If appname is given, a subdir is appended (and created if necessary). 
+    If nospaces, will ensure that the path has no spaces.
+    """
+    
+    # Do it the Python way
+    path = tempfile.gettempdir()
+    
+    # Try harder if we have to
+    if nospaces and ' ' in path:
+        if sys.platform.startswith('win'):
+            for path in ['c:\\TEMP', 'c:\\TMP']:
+                if os.path.isdir(path):
+                    break
+            if not os.path.isdir(path):
+                os.mkdir(path) 
+        else:
+            for path in ['/tmp', '/var/tmp']: # http://www.tuxfiles.org/linuxhelp/linuxdir.html
+                if os.path.isdir(path):
+                    break
+            else:
+                raise RuntimeError('Could not locate temporary directory.')
+    
+    # Get path specific for this app
+    if appname:
+        path = os.path.join(path, appname)
+        if not os.path.isdir(path):
+            os.mkdir(path)
+    
+    # Done
+    return path
+
+
+
+# From pyzolib/paths.py (https://bitbucket.org/pyzo/pyzolib/src/tip/paths.py)
+import os
+def user_dir():
+    """ user_dir()
+    Get the path to the user directory. (e.g. "/home/jack", "c:/Users/jack")
+    """
+    return os.path.expanduser('~')
+
+
+
+# From pyzolib/paths.py (https://bitbucket.org/pyzo/pyzolib/src/tip/paths.py)
+import os, sys
+def appdata_dir(appname=None, roaming=False, macAsLinux=False):
+    """ appdata_dir(appname=None, roaming=False,  macAsLinux=False)
+    Get the path to the application directory, where applications are allowed
+    to write user specific files (e.g. configurations). For non-user specific
+    data, consider using common_appdata_dir().
+    If appname is given, a subdir is appended (and created if necessary). 
+    If roaming is True, will prefer a roaming directory (Windows Vista/7).
+    If macAsLinux is True, will return the Linux-like location on Mac.
+    """
+    
+    # Define default user directory
+    userDir = os.path.expanduser('~')
+    
+    # Get system app data dir
+    path = None
+    if sys.platform.startswith('win'):
+        path1, path2 = os.getenv('LOCALAPPDATA'), os.getenv('APPDATA')
+        path = (path2 or path1) if roaming else (path1 or path2)
+    elif sys.platform.startswith('darwin') and not macAsLinux:
+        path = os.path.join(userDir, 'Library', 'Application Support')
+    # On Linux and as fallback
+    if not (path and os.path.isdir(path)):
+        path = userDir
+    
+    # Maybe we should store things local to the executable (in case of a 
+    # portable distro or a frozen application that wants to be portable)
+    prefix = sys.prefix
+    if getattr(sys, 'frozen', None): # See application_dir() function
+        prefix = os.path.abspath(os.path.dirname(sys.path[0]))
+    for reldir in ('settings', '../settings'):
+        localpath = os.path.abspath(os.path.join(prefix, reldir))
+        if os.path.isdir(localpath):
+            try:
+                open(os.path.join(localpath, 'test.write'), 'wb').close()
+                os.remove(os.path.join(localpath, 'test.write'))
+            except IOError:
+                pass # We cannot write in this directory
+            else:
+                path = localpath
+                break
+    
+    # Get path specific for this app
+    if appname:
+        if path == userDir:
+            appname = '.' + appname.lstrip('.') # Make it a hidden directory
+        path = os.path.join(path, appname)
+        if not os.path.isdir(path):
+            os.mkdir(path)
+    
+    # Done
+    return path
+
+
+
+# From pyzolib/paths.py (https://bitbucket.org/pyzo/pyzolib/src/tip/paths.py)
+import os, sys
+def common_appdata_dir(appname=None):
+    """ common_appdata_dir(appname=None)
+    Get the path to the common application directory. Applications are
+    allowed to write files here. For user specific data, consider using 
+    appdata_dir().
+    If appname is given, a subdir is appended (and created if necessary). 
+    """
+    
+    # Try to get path 
+    path = None
+    if sys.platform.startswith('win'):
+        path = os.getenv('ALLUSERSPROFILE', os.getenv('PROGRAMDATA'))
+    elif sys.platform.startswith('darwin'):
+        path = '/Library/Application Support'
+    else:
+        # Not sure what to use. Apps are only allowed to write to the home
+        # dir and tmp dir, right?
+        pass
+    
+    # If no success, use appdata_dir() instead
+    if not (path and os.path.isdir(path)):
+        path = appdata_dir()
+    
+    # Get path specific for this app
+    if appname:
+        path = os.path.join(path, appname)
+        if not os.path.isdir(path):
+            os.mkdir(path)
+    
+    # Done
+    return path
+
+
+
+#  Other approaches that we considered, but which did not work for links,
+#  or are less reliable for other reasons are:
+#      * sys.executable: does not work for links
+#      * sys.prefix: dito
+#      * sys.exec_prefix: dito
+#      * os.__file__: does not work when frozen
+#      * __file__: only accessable from main module namespace, does not work when frozen 
+
+# todo: get this included in Python sys or os module!
+
+# From pyzolib/paths.py (https://bitbucket.org/pyzo/pyzolib/src/tip/paths.py)
+import os, sys
+def application_dir():
+    """ application_dir()
+    Get the directory in which the current application is located. 
+    The "application" can be a Python script or a frozen application. 
+    This function raises a RuntimeError if in interpreter mode.
+    """
+    # Test if the current process can be considered an "application"
+    if not sys.path or not sys.path[0]:
+        raise RuntimeError('Cannot determine app path because sys.path[0] is empty!')
+    # Get the path. If frozen, sys.path[0] is the name of the executable,
+    # otherwise it is the path to the directory that contains the script.
+    thepath = sys.path[0]
+    if getattr(sys, 'frozen', None):
+        thepath = os.path.dirname(thepath)
+    # Return absolute version, or symlinks may not work
+    return os.path.abspath(thepath)
+
+
+
+## Pyzo specific
+#
+# A Pyzo distribution maintains a file in the appdata dir that lists
+# the directory where it is intalled. Pyzo can in principle be installed 
+# multiple times. In that case the file contains multiple entries.
+# This file is checked each time the pyzo executable is run. Therefore
+# a user can move the Pyzo directory and simply run the Pyzo executable 
+# to update the registration.
+
+def pyzo_dirs(newdir=None, makelast=False):
+    """ pyzo_dirs(newdir=None,  makelast=False)
+    Compatibility function. Like pyzo_dirs2, but returns a list of
+    directories and does not allow setting the version.
+    """
+    return [p[0] for p in pyzo_dirs2(newdir, makelast=makelast)]
+
+
+# From pyzolib/paths.py (https://bitbucket.org/pyzo/pyzolib/src/tip/paths.py)
+import os, sys
+def pyzo_dirs2(path=None, version='0', **kwargs):
+    """ pyzo_dirs2(dir=None, version='0', makelast=False)
+    Get the locations of installed Pyzo directories. Returns a list of
+    tuples: (dirname, version). In future versions more information may
+    be added to the file, so please take larger tuples into account.
+    If path is a dir containing a python exe, it is added it to the
+    list. If the keyword arg 'makelast' is given and True, will ensure
+    that the given path is the last in the list (i.e. the default).
+    """
+    defaultPyzo = '', '0'  # To fill in values for shorter items
+    newPyzo = (str(path), str(version)) if path else None
+    # Get application dir
+    userDir = os.path.expanduser('~')
+    path = None
+    if sys.platform.startswith('win'):
+        path = os.getenv('LOCALAPPDATA', os.getenv('APPDATA'))
+    elif sys.platform.startswith('darwin'):
+        path = os.path.join(userDir, 'Library', 'Application Support')
+    # Get application dir for Pyzo
+    if path and os.path.isdir(path):
+        path = os.path.join(path, 'pyzo')
+    else:
+        path = os.path.join(userDir, '.pyzo')  # On Linux and as fallback
+    if not os.path.isdir(path):
+        os.mkdir(path)
+    # Open file and parse
+    fname = os.path.join(path, 'pyzodirs')
+    pyzos, npyzos = [], 0
+    if os.path.isfile(fname):
+        lines = open(fname, 'rb').read().decode('utf-8').split('\n')
+        pyzos = [tuple(d.split(':::')) for d in [d.strip() for d in lines] if d]
+        npyzos = len(pyzos)
+    # Add dir if necessary
+    if newPyzo and os.path.isdir(newPyzo[0]):
+        if kwargs.get('makelast', False) or newPyzo not in pyzos:
+            npyzos = 0  # force save
+            pyzos = [p for p in pyzos if p[0] != newPyzo[0]]  # rm based on dir
+            pyzos.append(newPyzo)
+    # Check validity of all pyzos, write back if necessary, and return
+    pythonname = 'python' + '.exe' * sys.platform.startswith('win')
+    pyzos = [p for p in pyzos if os.path.isfile(os.path.join(p[0], pythonname))]
+    if len(pyzos) != npyzos:
+        lines = [':::'.join(p) for p in pyzos]
+        open(fname, 'wb').write( ('\n'.join(lines)).encode('utf-8') )
+    return [p+defaultPyzo[len(p):] for p in pyzos]
+
+
+
+
+## Windows specific
+
+# Maybe for directory of programs, pictures etc.

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/pyzo.git



More information about the debian-science-commits mailing list