Python os.path.expanduser() Examples

The following are 30 code examples of os.path.expanduser(). 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: connection.py    From insightconnect-plugins with MIT License 7 votes vote down vote up
def connect_key(self, params={}):
        home_dir = (path.expanduser('~'))
        key_file = "{}/.ssh".format(home_dir)
        f = params.get('key').get('privateKey')
        fb = f.get('content')
        fb64 = base64.b64decode(fb)
        fb64 = fb64.decode("utf-8")
        if not path.exists(key_file):
            os.makedirs(key_file)
            os.chmod(key_file, 0o700)
        key_file_path = path.join(key_file, "id_rsa")
        with open(key_file_path, 'w+') as f:
            f.write(fb64)
        os.chmod(key_file_path, 0o600)
        self.logger.info("Establishing connection")
        device = {'device_type': params.get('device_type'), 'ip': params.get('host'),
                  'username': params.get('credentials').get('username'), 'use_keys': True, 'key_file': key_file_path,
                  'password': params.get('credentials').get('password'), 'port': params.get('port'),
                  'secret': params.get('secret').get('secretKey'), 'allow_agent': True, 'global_delay_factor': 4}
        self.device_connect = ConnectHandler(**device)
        return self.device_connect 
Example #2
Source File: main.py    From pgcli with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def write_to_file(self, pattern, **_):
        if not pattern:
            self.output_file = None
            message = "File output disabled"
            return [(None, None, None, message, "", True, True)]
        filename = os.path.abspath(os.path.expanduser(pattern))
        if not os.path.isfile(filename):
            try:
                open(filename, "w").close()
            except IOError as e:
                self.output_file = None
                message = str(e) + "\nFile output disabled"
                return [(None, None, None, message, "", False, True)]
        self.output_file = filename
        message = 'Writing to file "%s"' % self.output_file
        return [(None, None, None, message, "", True, True)] 
Example #3
Source File: ec2.py    From ec2-gazua with MIT License 6 votes vote down vote up
def key_file(self):
        if self.key_name is None:
            return None

        key_file = self.config['ssh-path'] + '/' + self.key_name
        key_path = expanduser(key_file)

        if isfile(key_path):
            return key_path

        if key_path.endswith('.pem'):
            raw_path = isfile(key_path.rsplit('.pem', 1)[0])
            return raw_path if isfile(raw_path) else None

        pem_path = key_path + '.pem'
        return pem_path if isfile(pem_path) else None 
Example #4
Source File: templates.py    From GTDWeb with GNU General Public License v2.0 6 votes vote down vote up
def handle_template(self, template, subdir):
        """
        Determines where the app or project templates are.
        Use django.__path__[0] as the default because we don't
        know into which directory Django has been installed.
        """
        if template is None:
            return path.join(django.__path__[0], 'conf', subdir)
        else:
            if template.startswith('file://'):
                template = template[7:]
            expanded_template = path.expanduser(template)
            expanded_template = path.normpath(expanded_template)
            if path.isdir(expanded_template):
                return expanded_template
            if self.is_url(template):
                # downloads the file and returns the path
                absolute_path = self.download(template)
            else:
                absolute_path = path.abspath(expanded_template)
            if path.exists(absolute_path):
                return self.extract(absolute_path)

        raise CommandError("couldn't handle %s template %s." %
                           (self.app_or_project, template)) 
Example #5
Source File: datasets_fetcher.py    From rankeval with Mozilla Public License 2.0 6 votes vote down vote up
def __get_data_home__(data_home=None):
    """
    Return the path of the rankeval data dir.
    This folder is used by some large dataset loaders to avoid
    downloading the data several times.
    By default the data dir is set to a folder named 'rankeval_data'
    in the user home folder.
    Alternatively, it can be set by the 'RANKEVAL_DATA' environment
    variable or programmatically by giving an explicit folder path. The
    '~' symbol is expanded to the user home folder.
    If the folder does not already exist, it is automatically created.
    """
    if data_home is None:
        data_home = environ.get('RANKEVAL_DATA', join('~', 'rankeval_data'))
    data_home = expanduser(data_home)
    if not exists(data_home):
        makedirs(data_home)
    return data_home 
Example #6
Source File: main.py    From pgcli with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def parse_service_info(service):
    service = service or os.getenv("PGSERVICE")
    service_file = os.getenv("PGSERVICEFILE")
    if not service_file:
        # try ~/.pg_service.conf (if that exists)
        if platform.system() == "Windows":
            service_file = os.getenv("PGSYSCONFDIR") + "\\pg_service.conf"
        elif os.getenv("PGSYSCONFDIR"):
            service_file = os.path.join(os.getenv("PGSYSCONFDIR"), ".pg_service.conf")
        else:
            service_file = expanduser("~/.pg_service.conf")
    if not service:
        # nothing to do
        return None, service_file
    service_file_config = ConfigObj(service_file)
    if service not in service_file_config:
        return None, service_file
    service_conf = service_file_config.get(service)
    return service_conf, service_file 
Example #7
Source File: data_store.py    From pyaff4 with Apache License 2.0 6 votes vote down vote up
def loadMetadata(self, zip):
        # Load the turtle metadata.
        aff4cache = os.path.join(expanduser("~"), ".aff4")
        if not os.path.exists(aff4cache):
            try:
                os.makedirs(aff4cache)
            except OSError as exc:  # Guard against race condition
                if exc.errno != errno.EEXIST:
                    raise
        cached_turtle = os.path.join(aff4cache, "%s.hdt" % str(zip.urn)[7:])
        if not os.path.exists(cached_turtle):
            self.createHDTviaLib(zip, cached_turtle)

        if os.path.exists(cached_turtle):
            # assume we have a HDT cache of turtle at this point
            self.hdt = HDTDocument(cached_turtle)


    # this implementation currently not tested
    # and it is super ugly. We are materializing all triples just to
    # list all the subjects.
    # TODO: Implement subject iterator in pyHDT 
Example #8
Source File: main.py    From pgcli with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def execute_from_file(self, pattern, **_):
        if not pattern:
            message = "\\i: missing required argument"
            return [(None, None, None, message, "", False, True)]
        try:
            with open(os.path.expanduser(pattern), encoding="utf-8") as f:
                query = f.read()
        except IOError as e:
            return [(None, None, None, str(e), "", False, True)]

        if self.destructive_warning and confirm_destructive_query(query) is False:
            message = "Wise choice. Command execution stopped."
            return [(None, None, None, message)]

        on_error_resume = self.on_error == "RESUME"
        return self.pgexecute.run(
            query, self.pgspecial, on_error_resume=on_error_resume
        ) 
Example #9
Source File: path.py    From vedaseg with Apache License 2.0 6 votes vote down vote up
def find_vcs_root(path, markers=('.git', )):
    """Finds the root directory (including itself) of specified markers.

    Args:
        path (str): Path of directory or file.
        markers (list[str], optional): List of file or directory names.

    Returns:
        The directory contained one of the markers or None if not found.
    """
    if osp.isfile(path):
        path = osp.dirname(path)

    prev, cur = None, osp.abspath(osp.expanduser(path))
    while cur != prev:
        if any(osp.exists(osp.join(cur, marker)) for marker in markers):
            return cur
        prev, cur = cur, osp.split(cur)[0]
    return None 
Example #10
Source File: ocsp_snowflake.py    From snowflake-connector-python with Apache License 2.0 6 votes vote down vote up
def reset_cache_dir():
        # Cache directory
        OCSPCache.CACHE_DIR = os.getenv('SF_OCSP_RESPONSE_CACHE_DIR')
        if OCSPCache.CACHE_DIR is None:
            cache_root_dir = expanduser("~") or tempfile.gettempdir()
            if platform.system() == 'Windows':
                OCSPCache.CACHE_DIR = path.join(cache_root_dir, 'AppData', 'Local', 'Snowflake',
                                                'Caches')
            elif platform.system() == 'Darwin':
                OCSPCache.CACHE_DIR = path.join(cache_root_dir, 'Library', 'Caches', 'Snowflake')
            else:
                OCSPCache.CACHE_DIR = path.join(cache_root_dir, '.cache', 'snowflake')
        logger.debug("cache directory: %s", OCSPCache.CACHE_DIR)

        if not path.exists(OCSPCache.CACHE_DIR):
            try:
                os.makedirs(OCSPCache.CACHE_DIR, mode=0o700)
            except Exception as ex:
                logger.debug('cannot create a cache directory: [%s], err=[%s]',
                             OCSPCache.CACHE_DIR, ex)
                OCSPCache.CACHE_DIR = None 
Example #11
Source File: dataset.py    From pytorch_geometric with MIT License 6 votes vote down vote up
def __init__(self, root=None, transform=None, pre_transform=None,
                 pre_filter=None):
        super(Dataset, self).__init__()

        if isinstance(root, str):
            root = osp.expanduser(osp.normpath(root))

        self.root = root
        self.transform = transform
        self.pre_transform = pre_transform
        self.pre_filter = pre_filter
        self.__indices__ = None

        if 'download' in self.__class__.__dict__.keys():
            self._download()

        if 'process' in self.__class__.__dict__.keys():
            self._process() 
Example #12
Source File: base.py    From pulse2percept with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def get_data_dir(data_dir=None):
    """Return the path of the pulse2percept data directory

    This directory is used to store the datasets retrieved by the data fetch
    utility functions to avoid downloading the data several times.

    By default, this is set to a directory called 'pulse2percept_data' in the
    user home directory.
    Alternatively, it can be set by a ``PULSE2PERCEPT_DATA`` environment
    variable or set programmatically by specifying a path.

    If the directory does not already exist, it is automatically created.

    Parameters
    ----------
    data_dir : str | None
        The path to the pulse2percept data directory.
    """
    if data_dir is None:
        data_dir = environ.get('PULSE2PERCEPT_DATA',
                               join('~', 'pulse2percept_data'))
    data_dir = expanduser(data_dir)
    if not exists(data_dir):
        makedirs(data_dir)
    return data_dir 
Example #13
Source File: protect.py    From rm-protection with MIT License 6 votes vote down vote up
def protect(protect_args=None):
    global c
    flags = ''
    option_end = False
    if not protect_args:
        protect_args = argv[1:]
    for arg in protect_args:
        if arg == '--':
            option_end = True
        elif (arg.startswith("-") and not option_end):
            flags = flags + arg[arg.rfind('-') + 1:]
        elif arg in c.invalid:
            pprint('"." and ".." may not be protected')
        else:
            path = abspath(expv(expu(arg)))
            evalpath = dirname(path) + "/." + basename(path) + c.suffix
            if not exists(path):
                pprint("Warning: " + path + " does not exist")
            with open(evalpath, "w") as f:
                question = input("Question for " + path + ": ")
                answer = input("Answer: ")
                f.write(question + "\n" + answer + "\n" + flags.upper()) 
Example #14
Source File: util.py    From PE-HFT-Python with GNU General Public License v3.0 6 votes vote down vote up
def util_setCredentials(username, password, apiKey, host='quant07.portfolioeffect.com', port=443):
    # write credentials to file
    global CLIENT_CONNECTION
    CLIENT_CONNECTION = {'username': username, 'password': password, 'apiKey': apiKey, 'host': host, 'port': port  }
    APPNAME = "ice9"
    if sys.platform == 'darwin':
        from AppKit import NSSearchPathForDirectoriesInDomains
        # http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html#//apple_ref/c/func/NSSearchPathForDirectoriesInDomains
        # NSApplicationSupportDirectory = 14
        # NSUserDomainMask = 1
        # True for expanding the tilde into a fully qualified path
        appdata = path.join(NSSearchPathForDirectoriesInDomains(14, 1, True)[0], APPNAME,'login.npy')
    elif sys.platform == 'win32':
        appdata = path.join(environ['APPDATA'], APPNAME,'login.npy')
    else:
        appdata = path.expanduser(path.join("~", "." + APPNAME,'login.npy'))
    np.save(appdata, CLIENT_CONNECTION) 
Example #15
Source File: pipeline.py    From daudin with MIT License 6 votes vote down vote up
def __init__(self, outfp=sys.stdout, errfp=sys.stderr, debug=False,
                 printTracebacks=False, loadInitFile=True, shell=None,
                 usePtys=True):
        self.outfp = outfp
        self.errfp = errfp
        self.debug = debug
        self.printTracebacks = printTracebacks
        self.shell = shell or ['/bin/sh', '-c']
        self.usePtys = usePtys
        self.stdin = None
        self.lastStdin = None
        self.stdout = None
        self.pendingText = ''
        self.initFile = join(expanduser('~'), '.daudin.py')
        self.lastResultIsList = False
        self.local = self._getLocal()
        if loadInitFile:
            self.loadInitFile()
        self.inPipeline = False 
Example #16
Source File: cred.py    From magpy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def sc():
    """
    Show credentials
    """

    sysuser = getuser()
    home = expanduser('~'+sysuser)
    # previously used expanduser('~') which does not work for root
    credentials = os.path.join(home,'.magpycred')
    print("Credentials: Overview of existing credentials:")
    try:
        dictslist = loadobj(credentials)
    except:
        print("Credentials: Could not load file")
        return

    for d in dictslist:
        print(d)
    return dictslist 
Example #17
Source File: settings.py    From screeps-stats with MIT License 6 votes vote down vote up
def getSettings():
    if not getSettings.settings:
        cwd = os.getcwd()
        path = cwd + '/.settings.yaml'

        if not os.path.isfile(path):
            path = cwd + '/.screeps_settings.yaml'

        if not os.path.isfile(path):
            path = expanduser('~') + '/.screeps_settings.yaml'

        if not os.path.isfile(path):
            path = '/vagrant/.screeps_settings.yaml'


        if not os.path.isfile(path):
            print 'no settings file found'
            sys.exit(-1)
            return False

        with open(path, 'r') as f:
            getSettings.settings = yaml.load(f)

    return getSettings.settings 
Example #18
Source File: arduinoprotocol.py    From magpy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def extendarduinolist(self, idnum):
        from os.path import expanduser
        home = expanduser("~")

        martasdir = [path for path, dirs, files in os.walk(home) if path.endswith('MARTAS')][0]
        arduinosensorfile = os.path.join(martasdir,'arduinolist.csv')
        log.msg('Checking Arduinofile: %s' % arduinosensorfile)
        arduinolist = []
        sensorelement = []
        try:
            arduinolist = self.loadarduinolist(arduinosensorfile)
            sensorelement = [elem[0] for elem in arduinolist]
            print("Liste", sensorelement)
        except:
            log.msg('Arduino: No Sensor list so far -or- Error while getting sensor list')
            pass
        if not self.sensordict[idnum] in sensorelement:
            arduinolist.append([self.sensordict[idnum], self.keydict[idnum]])
            self.savearduinolist(arduinosensorfile,arduinolist) 
Example #19
Source File: data_store.py    From pyaff4 with Apache License 2.0 5 votes vote down vote up
def invalidateCachedMetadata(self, zip):
        aff4cache = os.path.join(expanduser("~"), ".aff4")
        cached_turtle = os.path.join(aff4cache, "%s.hdt" % str(zip.urn)[7:])
        cached_turtle_index = cached_turtle + ".index.v1-1"
        for f in [cached_turtle, cached_turtle_index]:
            if os.path.exists(f):
                LOGGER.debug("Invalidating HDT index %s" % f)
                os.unlink(f) 
Example #20
Source File: pathlib.py    From python-netsurv with MIT License 5 votes vote down vote up
def resolve_from_str(input, root):
    assert not isinstance(input, Path), "would break on py2"
    root = Path(root)
    input = expanduser(input)
    input = expandvars(input)
    if isabs(input):
        return Path(input)
    else:
        return root.joinpath(input) 
Example #21
Source File: consts.py    From foremast with Apache License 2.0 5 votes vote down vote up
def find_config():
    """Look for **foremast.cfg** in config_locations or ``./config.py``.

    Raises:
        SystemExit: No configuration file found.

    Returns:
        dict: Found dynamic or static configuration.

    """
    config_locations = [
        '/etc/foremast/foremast.cfg',
        expanduser('~/.foremast/foremast.cfg'),
        './.foremast/foremast.cfg',
    ]
    configurations = ConfigParser()

    cfg_file = configurations.read(config_locations)
    dynamic_config_file = getenv('FOREMAST_CONFIG_FILE', DEFAULT_DYNAMIC_CONFIG_FILE)

    if cfg_file:
        LOG.info('Loading static configuration file.')
    elif exists(dynamic_config_file):
        LOG.info('Loading dynamic configuration file.')
        configurations = load_dynamic_config(config_file=dynamic_config_file)
    else:
        config_locations.append(dynamic_config_file)
        LOG.warning('No configuration found in the following locations:\n%s', '\n'.join(config_locations))
        LOG.warning('Using defaults...')

    return dict(configurations) 
Example #22
Source File: util.py    From apio with GNU General Public License v2.0 5 votes vote down vote up
def get_package_dir(pkg_name):
    home_dir = _get_projconf_option_dir('pkg_dir', '')
    if not home_dir:
        home_dir = _get_projconf_option_dir('home_dir', '~/.apio')
    home_dir = re.sub(r'\~', expanduser('~').replace('\\', '/'), home_dir)

    paths = home_dir.split(os.pathsep)
    for path in paths:
        package_dir = safe_join(path, 'packages', pkg_name)
        if isdir(package_dir):
            return package_dir

    return '' 
Example #23
Source File: util.py    From apio with GNU General Public License v2.0 5 votes vote down vote up
def get_home_dir():
    home_dir = _get_projconf_option_dir('home_dir', '~/.apio')
    home_dir = re.sub(r'\~', expanduser('~').replace('\\', '/'), home_dir)

    paths = home_dir.split(os.pathsep)
    path = _check_writable(paths)
    if not path:
        path = _create_path(paths)
        if not path:
            click.secho('Error: no usable home directory ' + path, fg='red')
            exit(1)
    return path 
Example #24
Source File: cred.py    From magpy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def dc(name):
    """
    Drop credentials for 'name'
    """
    sysuser = getuser()
    home = expanduser('~'+sysuser)
    # previously used expanduser('~') which does not work for root
    credentials = os.path.join(home,'.magpycred')
    try:
        dictslist = loadobj(credentials)
    except:
        print("Credentials: Could not load credentials file - aborting ...")
        return

    foundinput = False
    for d in dictslist:
        if d[0] == name:
            foundinput = True
    if foundinput:
        newdlist = [d for d in dictslist if not d[0] == name]
        saveobj(newdlist, credentials)
        print("Credentials: Removed entry for ", name)
        entries = [n[0] for n in newdlist]
        print("Credentials: Now containing entries for", entries)
    else:
        print("Credentials: Input for %s not found - aborting" % (name))
        return 
Example #25
Source File: test_ec2.py    From ec2-gazua with MIT License 5 votes vote down vote up
def test_ec2_instance_key_file_follow_key_name():
    instance = EC2Instance(
        {
            'key-file': {'default': 'hodolman.pem'},
            'ssh-path': '~/.ssh'
        },
        {
            'KeyName': 'super-secret.pem'
        }
    )

    ssh_path = expanduser('~') + '/.ssh'
    assert instance.key_file == ssh_path + '/hodolman.pem' 
Example #26
Source File: configure.py    From CoinSwapCS with GNU General Public License v3.0 5 votes vote down vote up
def lookup_appdata_folder():
    from os import path, environ
    if sys.platform == 'darwin':
        if "HOME" in environ:
            data_folder = path.join(os.environ["HOME"],
                                   "Library/Application support/",
                                   global_singleton.APPNAME) + '/'
        else:
            print("Could not find home folder")
            os.exit()

    elif 'win32' in sys.platform or 'win64' in sys.platform:
        data_folder = path.join(environ['APPDATA'], global_singleton.APPNAME) + '\\'
    else:
        data_folder = path.expanduser(path.join("~",
                                    "." + global_singleton.APPNAME + "/"))
    return data_folder 
Example #27
Source File: cred.py    From magpy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def lc(dictionary,value,path=None,debug=False):
    """
    Load credentials
    """

    if not path:
        sysuser = getuser()
        home = expanduser('~'+sysuser)
        # previously used expanduser('~') which does not work for root
        credentials = os.path.join(home,'.magpycred')
    else:
        credentials = path
    if debug:
        print("Accessing credential file:", credentials)

    try:
        dictslist = loadobj(credentials)
    except:
        print("Credentials: Could not load file")
        return

    for d in dictslist:
        if d[0] == dictionary:
            if 'passwd' in value:
                data = base64.b64decode(d[1][value])
                try:  # Python2/3 compatibility
                    data = data.decode()
                except AttributeError:
                    pass
                return data
            return d[1][value]

    print("Credentials: value/dict not found")
    return 
Example #28
Source File: client.py    From pyinfra with MIT License 5 votes vote down vote up
def get_ssh_config():
    user_config_file = path.expanduser('~/.ssh/config')
    if path.exists(user_config_file):
        with open(user_config_file) as f:
            ssh_config = SSHConfig()
            ssh_config.parse(f)
            return ssh_config 
Example #29
Source File: test_helper.py    From vidgear with Apache License 2.0 5 votes vote down vote up
def test_validate_webdata():
    """
    Testing validation function of WebGear API
    """
    validate_webdata(
        os.path.join(expanduser("~"), ".vidgear"),
        files=["im_not_a_file1", "im_not_a_file2", "im_not_a_file3"],
        logging=True,
    ) 
Example #30
Source File: ssh.py    From pyinfra with MIT License 5 votes vote down vote up
def _get_private_key(state, key_filename, key_password):
    if key_filename in state.private_keys:
        return state.private_keys[key_filename]

    ssh_key_filenames = [
        # Global from executed directory
        path.expanduser(key_filename),
    ]

    # Relative to the deploy
    if state.deploy_dir:
        ssh_key_filenames.append(
            path.join(state.deploy_dir, key_filename),
        )

    for filename in ssh_key_filenames:
        if not path.isfile(filename):
            continue

        try:
            key = _load_private_key_file(filename, key_filename, key_password)
            break
        except SSHException:
            pass

    # No break, so no key found
    else:
        raise IOError('No such private key file: {0}'.format(key_filename))

    # Load any certificate, names from OpenSSH:
    # https://github.com/openssh/openssh-portable/blob/049297de975b92adcc2db77e3fb7046c0e3c695d/ssh-keygen.c#L2453  # noqa
    for certificate_filename in (
        '{0}-cert.pub'.format(key_filename),
        '{0}.pub'.format(key_filename),
    ):
        if path.isfile(certificate_filename):
            key.load_certificate(certificate_filename)

    state.private_keys[key_filename] = key
    return key