def which(program, infile=None): """ takes a program name and returns the full path to the executable or None modified after: http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python :param program: name of the desired external program :return: full path of the executable file """ try: from PySide.QtCore import QSettings settings = QSettings() for key in settings.allKeys(): if 'binPath' in key: os.environ['PATH'] += ':{0}'.format(settings.value(key)) if infile is None: # use default parameter-file name bpath = os.path.join(os.path.expanduser('~'), '.pylot', 'pylot.in') else: bpath = os.path.join(os.path.expanduser('~'), '.pylot', infile) if os.path.exists(bpath): nllocpath = ":" + PylotParameter(bpath).get('nllocbin') os.environ['PATH'] += nllocpath except ImportError as e: print(e.message) def is_exe(fpath): return os.path.exists(fpath) and os.access(fpath, os.X_OK) def ext_candidates(fpath): yield fpath for ext in os.environ.get("PATHEXT", "").split(os.pathsep): yield fpath + ext fpath, fname = os.path.split(program) if fpath: if is_exe(program): return program else: for path in os.environ["PATH"].split(os.pathsep): exe_file = os.path.join(path, program) for candidate in ext_candidates(exe_file): if is_exe(candidate): return candidate return None
class Config(): """Classe chargée de la lecture/écriture/sauvegarde de la configuration""" _instance = None _path = "private/config" def __init__(self): assert self._instance == None Config._instance = self QCoreApplication.setOrganizationName("nicolas.carrier") QCoreApplication.setApplicationVersion(version) QCoreApplication.setApplicationName("gem") self.__settings = QSettings() @staticmethod def getInstance(): if Config._instance == None: Config._instance = Config() return Config._instance def keys(self): return self.__settings.allKeys() def __getitem__(self, key): """Méthode magique sous-jacent à l'opérateur [] en lecture""" if key in self.keys(): return self.__settings.value(key) else: return "" def __setitem__(self, key, value): """Méthode magique sous-jacent à l'opérateur [] en écriture""" self.__settings.setValue(key, value) def __delitem__(self, key): """Méthode magique sous-jacent à l'opérateur del""" self.__settings.remove(key)
def initialize(args=None, **kwargs): """ Initialize the profile system. You may use the following arguments to configure the profile system: ============= ============ ============ Argument Default Description ============= ============ ============ portable ``False`` If True, the profile system will attempt to live entirely in the root path. profile ``default`` The name of the profile to load. package If this is set, and ``pkg_resources`` is available, then attempt to find resources in this package or requirement. profile_path If this is set, load the profile from this path. root_path The application root directory. If not set, this will be calculated with ``os.path.abspath(os.path.dirname(sys.argv[0]))``. cache_path If this is set, this path will be used to cache data rather than a profile-specific path. ============= ============ ============ In addition, you can provide a list of command line arguments to have siding load them automatically. Example:: siding.profile.initialize(sys.argv[1:]) The following command line arguments are supported: =================== ============ Argument Description =================== ============ ``--portable`` If set, the profile system will attempt to live entirely in the ``root_path``. ``--profile`` The name of the profile to load. ``--profile-path`` If this is set, load the profile from this path. ``--root-path`` The application root directory. ``--cache-path`` If this is set, this path will be used to cache data rather than a profile-specific path. ``--package`` If this is set, and ``pkg_resources`` is available, then attempt to find resources in this package or requirement. =================== ============ """ global name global portable global package global profile_path global root_path global cache_path global settings # Set the defaults now. portable = kwargs.get('portable', False) name = kwargs.get('profile', 'default') # And load the paths if we've got them. package = kwargs.get('package', package) root_path = kwargs.get('root_path', root_path) profile_path = kwargs.get('profile_path', profile_path) cache_path = kwargs.get('cache_path', cache_path) # Now, parse the options we've got. if args: if args is True: args = sys.argv[1:] parser = argparse.ArgumentParser(add_help=False) parser.add_argument('--portable', action='store_true', default=None) parser.add_argument('--profile') parser.add_argument('--profile-path') parser.add_argument('--root-path') parser.add_argument('--cache-path') parser.add_argument('--package') options = parser.parse_known_args(args)[0] # Let's set stuff up then. if options.portable is not None: portable = options.portable if options.profile: name = options.profile if options.package: package = package if options.profile_path: profile_path = options.profile_path if not os.path.exists(profile_path): os.makedirs(profile_path) if options.root_path: root_path = options.root_path if not os.path.exists(root_path): parser.error("The specified root path doesn't exist.") if options.cache_path: cache_path = options.cache_path if not os.path.exists(cache_path): os.makedirs(cache_path) # Now, open the settings file with QSettings and we're done. ensure_paths() file = os.path.join(profile_path, u'settings.ini') settings = QSettings(file, QSettings.IniFormat) log.info(u'Using profile: %s (%s)' % (name, profile_path)) log.debug(u'settings.ini contains %d keys across %d groups.' % ( len(settings.allKeys()), len(settings.childGroups())))
def initialize(args=None, **kwargs): """ Initialize the profile system. You may use the following arguments to configure the profile system: ============= ============ ============ Argument Default Description ============= ============ ============ portable ``False`` If True, the profile system will attempt to live entirely in the root path. profile ``default`` The name of the profile to load. package If this is set, and ``pkg_resources`` is available, then attempt to find resources in this package or requirement. profile_path If this is set, load the profile from this path. root_path The application root directory. If not set, this will be calculated with ``os.path.abspath(os.path.dirname(sys.argv[0]))``. cache_path If this is set, this path will be used to cache data rather than a profile-specific path. ============= ============ ============ In addition, you can provide a list of command line arguments to have siding load them automatically. Example:: siding.profile.initialize(sys.argv[1:]) The following command line arguments are supported: =================== ============ Argument Description =================== ============ ``--portable`` If set, the profile system will attempt to live entirely in the ``root_path``. ``--profile`` The name of the profile to load. ``--profile-path`` If this is set, load the profile from this path. ``--root-path`` The application root directory. ``--cache-path`` If this is set, this path will be used to cache data rather than a profile-specific path. ``--package`` If this is set, and ``pkg_resources`` is available, then attempt to find resources in this package or requirement. =================== ============ """ global name global portable global package global profile_path global root_path global cache_path global settings # Set the defaults now. portable = kwargs.get('portable', False) name = kwargs.get('profile', 'default') # And load the paths if we've got them. package = kwargs.get('package', package) root_path = kwargs.get('root_path', root_path) profile_path = kwargs.get('profile_path', profile_path) cache_path = kwargs.get('cache_path', cache_path) # Now, parse the options we've got. if args: if args is True: args = sys.argv[1:] parser = argparse.ArgumentParser(add_help=False) parser.add_argument('--portable', action='store_true', default=None) parser.add_argument('--profile') parser.add_argument('--profile-path') parser.add_argument('--root-path') parser.add_argument('--cache-path') parser.add_argument('--package') options = parser.parse_known_args(args)[0] # Let's set stuff up then. if options.portable is not None: portable = options.portable if options.profile: name = options.profile if options.package: package = package if options.profile_path: profile_path = options.profile_path if not os.path.exists(profile_path): os.makedirs(profile_path) if options.root_path: root_path = options.root_path if not os.path.exists(root_path): parser.error("The specified root path doesn't exist.") if options.cache_path: cache_path = options.cache_path if not os.path.exists(cache_path): os.makedirs(cache_path) # Now, open the settings file with QSettings and we're done. ensure_paths() file = os.path.join(profile_path, u'settings.ini') settings = QSettings(file, QSettings.IniFormat) log.info(u'Using profile: %s (%s)' % (name, profile_path)) log.debug(u'settings.ini contains %d keys across %d groups.' % (len(settings.allKeys()), len(settings.childGroups())))
def initialize(args=None, **kwargs): """ Initialize the profile system. You may use the following arguments to configure the profile system: ============= ============ ============ Argument Default Description ============= ============ ============ portable ``False`` If True, the profile system will create a profile path within the root folder, allowing the application to work as a portable app. profile ``default`` The name of the profile to load. sources ``[]`` A list of additional sources for the path system to use. These will be fed into :func:`siding.path.add_source`, along with any sources from the command line. profile_path If this is set, load the profile from this path rather than building a path. root_path The application root directory. This is always the last source to be used by the path system. ============= ============ ============ .. warning:: ``root_path`` will *probably* not work as expected after your application is frozen into an executable, so be sure to test that it's working properly before distributing your application if you're using ``root_path``. In addition, you can provide a list of command line arguments to have siding load them automatically. Example:: siding.profile.initialize(sys.argv[1:]) The following command line arguments are supported: =================== ============ Argument Description =================== ============ ``--portable`` If True, the profile system will create a profile path within the root folder, allowing the application to work as a portable app. ``--profile`` The name of the profile to load. ``--profile-path`` The path to load the profile from. ``--root-path`` The application root directory. ``--source`` An additional source for the path system. This can be used multiple times. =================== ============ """ global name global portable global profile_path global root_path global settings # Set the defaults now. portable = kwargs.get('portable', False) name = kwargs.get('profile', 'default') # And load the paths if we've got them. root_path = kwargs.get('root_path', root_path) profile_path = kwargs.get('profile_path', profile_path) # Get the source list. sources = kwargs.get('sources', []) # Now, parse the options we've got. if args: if args is True: args = sys.argv[1:] parser = argparse.ArgumentParser(add_help=False) parser.add_argument('--portable', action='store_true', default=None) parser.add_argument('--profile') parser.add_argument('--profile-path') parser.add_argument('--root-path') parser.add_argument('--source', action='append') options = parser.parse_known_args(args)[0] # Let's set stuff up then. if options.portable is not None: portable = options.portable if options.profile: name = options.profile if options.profile_path: profile_path = options.profile_path if not os.path.exists(profile_path): os.makedirs(profile_path) if options.root_path: root_path = options.root_path if not os.path.exists(root_path): parser.error("The specified root path doesn't exist.") if options.source: for source in options.source: if not source in sources: if not os.path.exists(source): parser.error("The source %r doesn't exist." % source) sources.append(source) # Now, do the path stuff. for source in sources: path.add_source(source) # Do we already have our paths? if profile_path or root_path: path.add_source(profile_path) # Make sure. ensure_paths() # Now, open the settings file with QSettings and we're done. file = os.path.join(profile_path, 'settings.ini') settings = QSettings(file, QSettings.IniFormat) log.info(u'Using profile: %s (%s)' % (name, profile_path)) log.debug(u'settings.ini contains %d keys across %d groups.' % ( len(settings.allKeys()), len(settings.childGroups())))