Exemplo n.º 1
0
 def find_css_uris(uri):
     """Find all the CSS dependencies (@import directives)."""
     uris = [uri]
     stream = Uri.UrlOpen(uri)
     for line in Uri.UrlOpen(uri).readlines():
         match = re.match(r"\s*@import\s+url\s*\((.*)\)", line)
         if match:
             next_uri = Uri.BaseJoin(uri, eval(match.group(1)))
             uris.extend(find_css_uris(next_uri))
     return uris
Exemplo n.º 2
0
    def copy_uri(self, uri, filename):
        """
        Copies the contents of the resource given by 'uri' to 'filename'.
        """
        source = Uri.UrlOpen(uri)
        try:
            source_mtime = source.headers.getdate_tz('last-modified')
            source_mtime = rfc822.mktime_tz(source_mtime)
            try:
                target_mtime = os.path.getmtime(filename)
            except OSError:
                target_mtime = -1
            if not (self.force or source_mtime > target_mtime):
                self.announce("not copying %s (output up-to-date)" % uri, 1)
                return filename, False

            self.announce("copying %s -> %s" % (uri, filename), 2)
            if not self.dry_run:
                f = open(filename, 'wb')
                try:
                    f.write(source.read())
                finally:
                    f.close()
        finally:
            source.close()
        return filename, True
Exemplo n.º 3
0
    def get_modification_time(self, uri, xslt=False, _mtimes=None):
        if _mtimes is None:
            _mtimes = {}

        def gather_mtimes(fullurl):
            if fullurl not in _mtimes:
                _mtimes[fullurl] = -1
                self.get_modification_time(fullurl, xslt, _mtimes)
            return

        try:
            source = Uri.UrlOpen(uri)
        except EnvironmentError:
            mtime = None
        else:
            mtime = source.headers.getdate_tz('last-modified')
            mtime = rfc822.mktime_tz(mtime)
            ProcessIncludes(source, gather_mtimes, xslt)
        _mtimes[uri] = mtime
        return max(_mtimes.values())
Exemplo n.º 4
0
        # we also avoid its potentially time-consuming socket.gethostbyname()
        # calls, which aren't even warranted and are part of urllib's dubious
        # interpretation of RFC 1738.
        if scheme == 'file':
            path = Uri.UriToOsPath(uri, attemptAbsolute=False)
            try:
                stream = file(path, 'rb')
            except IOError, e:
                raise UriException(UriException.RESOURCE_ERROR,
                                   loc='%s (%s)' % (uri, path),
                                   uri=uri, msg=str(e))
        else:
            # urllib2.urlopen, wrapped by us, will suffice for http, ftp,
            # data and gopher
            try:
                stream = Uri.UrlOpen(uri)
            except IOError, e:
                raise UriException(UriException.RESOURCE_ERROR,
                                   uri=uri, loc=uri, msg=str(e))
        return stream

    def resolve(self, uri, base_uri=None):
        here = os.path.abspath('.')
        if uri.startswith('local:'):
            uri = uri[6:]
            resource = os.path.join(self.templates, uri)
            if os.path.exists(resource):
                return file(resource, 'rb')
            raise UriException(UriException.RESOURCE_ERROR,
                               uri=uri, loc=uri,
                               msg="The file did not exist in '%s'" % self.templates)
Exemplo n.º 5
0
        # Attempt to disable all external entity resolution
        for feature in (feature_validation, feature_external_ges,
                        feature_external_pes):
            try:
                parser.setFeature(feature, False)
            except SAXNotRecognizedException:
                pass
    except SAXException, e:
        raise DistutilsModuleError(e.getMessage())

    handler = InclusionFilter()
    parser.setContentHandler(handler)

    if isinstance(source, (str, unicode)):
        try:
            stream = Uri.UrlOpen(source)
        except OSError:
            # Assume part of an XInclude w/fallback.
            return
        source = InputSource(source)
        source.setByteStream(stream)
    elif hasattr(source, 'read'):
        stream = source
        source = InputSource(getattr(stream, 'name', None))
        source.setByteStream(stream)
    parser.parse(source)
    return


INDEX_TEMPLATE = """<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD Simplified DocBook XML V1.1//EN"