Back to index

system-config-printer  1.3.9+20120706
smburi.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 
00003 ## system-config-printer
00004 
00005 ## Copyright (C) 2006, 2007, 2008, 2009, 2011, 2012 Red Hat, Inc.
00006 ## Copyright (C) 2006, 2007 Florian Festi <ffesti@redhat.com>
00007 ## Copyright (C) 2006, 2007, 2008, 2009 Tim Waugh <twaugh@redhat.com>
00008 
00009 ## This program is free software; you can redistribute it and/or modify
00010 ## it under the terms of the GNU General Public License as published by
00011 ## the Free Software Foundation; either version 2 of the License, or
00012 ## (at your option) any later version.
00013 
00014 ## This program is distributed in the hope that it will be useful,
00015 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 ## GNU General Public License for more details.
00018 
00019 ## You should have received a copy of the GNU General Public License
00020 ## along with this program; if not, write to the Free Software
00021 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00022 
00023 import urllib
00024 
00025 def urlquote (x):
00026     q = urllib.quote (x)
00027     for c in ["/", "@", ":"]:
00028         q = q.replace (c, "%%%02X" % ord (c))
00029 
00030     return q
00031 
00032 class SMBURI:
00033     def __init__ (self,
00034                   uri=None,
00035                   group='', host='', share='', user='', password=''):
00036         if uri:
00037             if group or host or share or user or password:
00038                 raise RuntimeError
00039 
00040             uri = uri.encode ('utf-8')
00041             if uri.startswith ("smb://"):
00042                 uri = uri[6:]
00043 
00044             self.uri = uri
00045         else:
00046             self.uri = self._construct (group, host, share,
00047                                         user=user, password=password)
00048 
00049     def _construct (self, group, host, share, user='', password=''):
00050         uri_password = ''
00051         if password:
00052             uri_password = ':' + urlquote (password)
00053         if user:
00054             uri_password += '@'
00055         uri = "%s%s%s" % (urlquote (user),
00056                           uri_password,
00057                           urlquote (group))
00058         if len (group) > 0:
00059             uri += '/'
00060         uri += urlquote (host)
00061         if len (share) > 0:
00062             uri += "/" + urlquote (share)
00063         return uri
00064 
00065     def get_uri (self):
00066         return self.uri
00067 
00068     def sanitize_uri (self):
00069         group, host, share, user, password = self.separate ()
00070         return self._construct (group, host, share)
00071 
00072     def separate (self):
00073         uri = self.get_uri ()
00074         user = ''
00075         password = ''
00076         auth = uri.find ('@')
00077         if auth != -1:
00078             u = uri[:auth].find(':')
00079             if u != -1:
00080                 user = uri[:u]
00081                 password = uri[u + 1:auth]
00082             else:
00083                 user = uri[:auth]
00084             uri = uri[auth + 1:]
00085         sep = uri.count ('/')
00086         group = ''
00087         if sep == 2:
00088             g = uri.find('/')
00089             group = uri[:g]
00090             uri = uri[g + 1:]
00091         if sep < 1:
00092             host = ''
00093         else:
00094             h = uri.find('/')
00095             host = uri[:h]
00096             uri = uri[h + 1:]
00097             p = host.find(':')
00098             if p != -1:
00099                 host = host[:p]
00100         share = uri
00101         return (urllib.unquote (group), urllib.unquote (host),
00102                 urllib.unquote (share),
00103                 urllib.unquote (user), urllib.unquote (password))