コード例 #1
0
def test_fsescape():
    test_set = (
        (u'abc', 'abc'),
        ('abc', 'abc'),
        (u'ä', '~228~'),
        ('ä', '~195~~164~'),
        (u'~', '~~'),
        ('~', '~~'),
    )
    for s_in, s_out in test_set:
        assert utils.fsescape(s_in) == s_out
        assert isinstance(utils.fsescape(s_in), str)
コード例 #2
0
ファイル: test_utils.py プロジェクト: aarddict/mwlib
def test_fsescape():
    test_set = (
        (u'abc', 'abc'),
        ('abc', 'abc'),
        (u'ä', '~228~'),
        ('ä', '~195~~164~'),
        (u'~', '~~'),
        ('~', '~~'),
    )
    for s_in, s_out in test_set:
        assert utils.fsescape(s_in) == s_out
        assert type(utils.fsescape(s_in)) is str
コード例 #3
0
 def _getCachedImagePath(self, baseurl, name, size=None, makedirs=False):
     """Construct a filename for an image with the given parameters inside
     the cache directory. The file doesn't need to exist. If makedirs is True
     create all directories up to filename.
     """
     
     urlpart = self._getCacheDirForBaseURL(baseurl)
     if size is not None:
         sizepart = '%dpx' % size
     else:
         sizepart = 'orig'
     
     if name.lower().endswith('.svg'):
         if size is None:
             log.warn('Cannot get SVG image when no size is given')
             return None
         name += '.png'
     if name.lower().endswith('.gif'):
         name += '.png'
     name = (name[0].upper() + name[1:]).replace(' ', '_').replace("'", "-")
     
     d = os.path.join(urlpart, sizepart)
     if makedirs and not os.path.isdir(d):
         os.makedirs(d)
     return os.path.join(d, utils.fsescape(name))
コード例 #4
0
 def getDiskPath(self, name, size=None):
     """Return filename for image with given name and size
     
     @param name: image name (without namespace, i.e. without 'Image:')
     @type name: unicode
     
     @param size: if given, the image is converted to the given maximum width
     @type size: int or NoneType
     
     @returns: filename of image or None if image could not be found
     @rtype: basestring
     """
     
     assert isinstance(name, unicode), 'name must be of type unicode'
     
     url = self.getURL(name, size=size)
     if url is None:
         return None
     
     data = fetch_url(url, ignore_errors=True)
     if not data:
         return None
     
     ext = url.rsplit('.')[-1]
     if size is not None:
         ext = '%dpx.%s' % (size, ext)
     else:
         ext = '.%s' % ext
     filename = os.path.join(self.tmpdir, utils.fsescape(name + ext))
     f = open(filename, 'wb')
     f.write(data)
     f.close()
     return filename
コード例 #5
0
ファイル: nuwiki.py プロジェクト: ingob/mwlib
    def normalize_and_get_image_path(self, name):
        assert isinstance(name, basestring)
        name = unicode(name)
        ns, partial, fqname = self.nshandler.splitname(name, defaultns=6)
        if ns != 6:
            return

        if "/" in fqname:
            return None
        
        
        
        
        p = self._pathjoin("images", utils.fsescape(fqname))
        if not self._exists(p):
            fqname = 'File:' + partial # Fallback to default language english
            p = self._pathjoin("images", utils.fsescape(fqname))
            if not self._exists(p):
                return None

        if 1:
            from hashlib import md5
            
            hd = md5(fqname.encode("utf-8")).hexdigest()
            ext = os.path.splitext(p)[-1]
            ext = ext.replace(' ', '')
            # mediawiki gives us png's for these extensions. let's change them here.
            if ext.lower() in (".gif", ".svg", '.tif', '.tiff'):
                # print "change xt:", ext
                ext = ".png"
            hd += ext
                
            safe_path = self._pathjoin("images", "safe", hd)
            if not os.path.exists(safe_path):
                try:
                    os.symlink(os.path.join("..", utils.fsescape(fqname)), safe_path)
                except OSError, exc:
                    if exc.errno != 17: # File exists
                        raise
            return safe_path
コード例 #6
0
    def normalize_and_get_image_path(self, name):
        assert isinstance(name, basestring)
        name = unicode(name)
        ns, partial, fqname = self.nshandler.splitname(name, defaultns=6)
        if ns != 6:
            return

        if "/" in fqname:
            return None
        
        
        
        
        p = self._pathjoin("images", utils.fsescape(fqname))
        if not self._exists(p):
            fqname = 'File:' + partial # Fallback to default language english
            p = self._pathjoin("images", utils.fsescape(fqname))
            if not self._exists(p):
                return None

        if 1:
            from hashlib import md5
            
            hd = md5(fqname.encode("utf-8")).hexdigest()
            ext = os.path.splitext(p)[-1]
            ext = ext.replace(' ', '')
            # mediawiki gives us png's for these extensions. let's change them here.
            if ext.lower() in (".gif", ".svg", '.tif', '.tiff'):
                # print "change xt:", ext
                ext = ".png"
            hd += ext
                
            safe_path = self._pathjoin("images", "safe", hd)
            if not os.path.exists(safe_path):
                try:
                    os.symlink(os.path.join("..", utils.fsescape(fqname)), safe_path)
                except OSError, exc:
                    if exc.errno != 17: # File exists
                        raise
            return safe_path
コード例 #7
0
ファイル: fetch.py プロジェクト: ingob/mwlib
 def get_imagepath(self, title):
     p = os.path.join(self.path, "images", "%s" % (utils.fsescape(title),))
     self.imgcount+=1
     return p
コード例 #8
0
ファイル: fetch.py プロジェクト: vprusa/mwlib
 def get_imagepath(self, title):
     p = os.path.join(self.path, "images", "%s" % (utils.fsescape(title),))
     self.imgcount += 1
     return p