1 # -*- coding: utf-8 -*-
4 Dboxswitch dropbox profile switcher
6 license: Modified BSD License
8 Copyright (c) 2012, <stack@inventati.org>
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions are met:
13 * Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright
16 notice, this list of conditions and the following disclaimer in the
17 documentation and/or other materials provided with the distribution.
18 * Neither the name of the <organization> nor the
19 names of its contributors may be used to endorse or promote products
20 derived from this software without specific prior written permission.
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
26 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 from apperror import AppError
41 from settings import appconf
47 #create profile directory if not exists
49 os.makedirs(self.getProfileFolder())
51 if e.errno != errno.EEXIST:
54 #compile regular expression for validating profile names
55 self.reg = re.compile("[a-zA-Z0-9_-]+")
57 #patch symlink on windows
58 if platform.system() is 'Windows':
59 os.symlink = winsymlink
61 def getProfilesList(self):
62 """ Generate and returns the profiles
63 it assumes that self.pdir is defined """
64 #this is generated every time to handle the case of the user renaming the directories by hand
65 return [os.path.join(self.pdir, f) for f in os.listdir(self.pdir)]
67 def getProfileFolder(self):
68 """ Generates, in a os dependant way, the local folder where all profiles are stored """
70 #directory path is cached
72 except AttributeError:
73 pl = platform.system()
76 from xdg.BaseDirectory import xdf_data_home
77 self.pdir = os.path.join(xdg_data_home, appconf.appname)
79 self.pdir = os.path.join(os.path.expanduser('~'),".local/share",appconf.appname)
81 self.pdir = os.path.join(os.getenv("APPDATA"), appconf.appname)
83 self.pdir = os.path.join(os.path.expanduser('~'),"."+appconf.appname)
85 raise AppError('Operative system NOT supported.')
89 def addProfile(self, profileName):
90 """ Create a profile """
92 print("Creating a new profile")
93 if self.isValidProfileName(profileName):
94 os.makedirs(os.path.join(self.getProfileFolder(), profileName))
96 raise AppError('Profile Name not valid.\nAllowed only ascii characters.')
97 print("Profile "+profileName+" created.")
99 def delProfile(self, profileName):
100 """ Delete a profile """
102 print("Deleting profile")
103 if self.isValidProfileName(profileName):
105 #recursively delete the profile directory
106 shutil.rmtree(os.path.join(self.pdir, profileName))
108 raise AppError('Profile Name does not exists')
110 raise AppError('Profile Name not valid')
111 print("Profile "+profileName+" created.")
113 def isValidProfileName(self, pname):
114 if self.reg.match(pname) is not None:
119 def activateProfile(self, ppath):
120 if ppath in self.getProfilesList():
123 with open(ppath) as pdir:
124 os.unlink(self.getDropboxDirectory())
125 os.symlink(ppath, self.getDropboxDirectory())
127 raise AppError('Error on activating Profile: '+ppath)
130 raise AppError("Trying to acrivate non existant profile")
132 def getDropboxDirectory(self):
133 pl = platform.system()
135 return os.path.join(os.path.expanduser('~'),".dropbox")
136 elif pl == 'Windows':
137 raise NotImplementedError, "Not implemented yet."
139 raise NotImplementedError, "Not implemented yet."
143 def winsymlink(source, link_name):
144 '''symlink(source, link_name)
145 Creates a symbolic link pointing to source named link_name.
146 Used to patch the nonexistant version on windows for python 2.6'''
150 csl = ctypes.windll.kernel32.CreateSymbolicLinkW
151 csl.argtypes = (ctypes.c_wchar_p, ctypes.c_wchar_p, ctypes.c_uint32)
152 csl.restype = ctypes.c_ubyte
155 if source is not None and os.path.isdir(source):
157 if __CSL(link_name, source, flags) == 0:
158 raise ctypes.WinError()