This repository has been archived by the owner on Jan 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFolderHelper.py
84 lines (74 loc) · 2.62 KB
/
FolderHelper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# -*- coding:utf-8 -*-
# pyview
# Folders helper
"""
Helper functions for file/folder handling
"""
import sys, os
class FolderHelper():
def __init__(self, commandLineArgs):
"""
Initializes the folder helper and sets the command line arguments
class as created by the CommandLine helper class
"""
self.cmdLineOptions = commandLineArgs
self.lastOpenedFile = None
return
# Gets the user's home directory
def getUserHomeDir(self):
"""Gets the user's home directory"""
# TODO: Make this Windows safe
return str(os.environ.get('HOME'))
# Gets the user's "pictures" folder
def getUserPicturesDir(self):
"""Gets the user's Pictures folder"""
# http://win32com.goermezer.de/content/view/191/188/
# http://www.blueskyonmars.com/2005/08/05/finding-a-users-my-documents-folder-on-windows/
# TODO: Extend this for Windows, MacOS ...
user_home_dir = self.getUserHomeDir()
config_home = user_home_dir + "/.config"
if os.environ.has_key("XDG_CONFIG_HOME"):
config_home = os.environ["XDG_CONFIG_HOME"]
userDirectories_path = str(config_home + "/user-dirs.dirs")
if os.path.exists(userDirectories_path) and os.path.isfile(userDirectories_path):
userDirectories_file = open(userDirectories_path, "r")
lines = userDirectories_file.readlines()
userDirectories_file.close()
for line in lines:
if line.startswith("XDG_PICTURES_DIR"):
pictures_dir = line.split("=", 1)[1].strip()
if pictures_dir.startswith("\""):
pictures_dir = pictures_dir[1:-1]
pictures_dir = pictures_dir.replace("$HOME", user_home_dir)
pictures_dir = pictures_dir.replace("~/", user_home_dir + "/")
return str(pictures_dir)
return None
# Sets the last opened file
def setLastOpenedFile(self, filePath):
"""Sets the path of the last opened file"""
filePath = str(filePath)
if os.path.isfile(filePath):
self.lastOpenedFile = filePath
return
# Gets the application's startup directory
def getStartDir(self):
"""Gets the directory from which pyview was started"""
startDir = str(self.cmdLineOptions.initialDirectory)
if os.path.isdir(startDir):
startDir = os.path.normpath(startDir)
else:
startDir = os.path.dirname(startDir)
return str(startDir)
# Gets the start directory for the file|open dialog
def getFileDialogInitialDirectory(self):
"""Gets the initial directory for a file|open dialog"""
startDir = None
if self.lastOpenedFile is not None:
startDir = os.path.dirname(str(self.lastOpenedFile))
if not startDir:
startDir = self.getStartDir()
if not startDir:
startDir = self.getUserPicturesDir()
if not startDir:
startDir = self.getUserHomeDir()
return startDir