cssutils complains when the import is not relative to the Python
script being run?
Is that correct?
Is it a bug?
Is it not specified?
regards
--
Dave Pawson
XSLT XSL-FO FAQ.
Docbook FAQ.
http://www.dpawson.co.uk
So I have a CSS file:
contains @import url('t.generic.css');
I'd expect that to import from the same directory as the containing file?
Nevertheless you might have to tell
> cssutils where the importing sheet sits. it all depends how hrefs are
> given in the @import rules. If they are relative it should work easy
> but for path absolute paths (e.g. "/imported.css") this is a different
> thing. Please see
>
> http://cssutils.googlecode.com/svn/trunk/docs/html/docs/parse.html#cssparser
>
> the parameter "href" is for this.
<quote>If href is given it is set as sheet.href. Either way sheet.href
is used to resolve e.g. stylesheet imports via @import rules. </
Sorry Chris, I don't understand how that relates to my case?
You might also be interested in the
> custom fetcher described on the same page.
<quote>The fetcher is called when an @import rule is found and the
referenced stylesheet is about to be retrieved.</
OK, that I understand.
I may have to use that, although I think my usecase matches your description,
so why it is looking for a CSS file relative to the location of the
python Script I don't know.
Error
WARNING [Errno 2] No such file or directory:
'/d/shared/cakes/php/etc/t.generic.css'
WARNING CSSImportRule: While processing imported style sheet
href=u't.generic.css': IOError('Cannot read Stylesheet.',)
Running on Linux.
Tried
if rule.type == rule.IMPORT_RULE:
imp=cssutils.css.CSSImportRule(href="/d/shared/cakes/php/css")
print "import ", imp.href
Since I'm processing a number of stylesheets, I don't know the url of
the imported stylesheet.
>
>>
>> Nevertheless you might have to tell
>>
>> > cssutils where the importing sheet sits. it all depends how hrefs are
>> > given in the @import rules. If they are relative it should work easy
>> > but for path absolute paths (e.g. "/imported.css") this is a different
>> > thing. Please see
>>
>> >http://cssutils.googlecode.com/svn/trunk/docs/html/docs/parse.html#cs...
>>
>> > the parameter "href" is for this.
>>
>> <quote>If href is given it is set as sheet.href. Either way sheet.href
>> is used to resolve e.g. stylesheet imports via @import rules. </
>>
>> Sorry Chris, I don't understand how that relates to my case?
>
> If you do not give any href as parameter to parse in your case this
> should just work. If you would e.g. parse a sheet on the web you might
> have to overwrite the "base" href of the importing sheet.
OK, I added
parser=cssutils.CSSParser()
sheet = parser.parseFile(item,'utf-8', href="/d/shared/cakes/php/css"")
Still shows the same error
> check the hrefs as above and also what OS are you working on? And are
> you using parseFile when working locally or maybe parseString? You
> should be using parseFile I guess
Yes, as above, parsefile.
On Linux, fedora 10.
>> Running on Linux.
>>
>> Tried
>>
>> if rule.type == rule.IMPORT_RULE:
>> imp=cssutils.css.CSSImportRule(href="/d/shared/cakes/php/css")
>> print "import ", imp.href
>>
>> Since I'm processing a number of stylesheets, I don't know the url of
>> the imported stylesheet.
>
> Any way to use a file URL for href? Something like file:...? I guess
> this may be a problem with urllib2 which is used to read the imported
> sheets and a file ref on OS level might not work.
file::// is a valid url.
You might also want
> to try a relative path something like "shared/cakes/php/css/file.css".
> I also think you need the full file path and not just the folder.
The imported file is in the same directory as the importing file,
thats what is puzzling. A relative file should work.
It's the 'base' that seems to be set to the current working directory.
>
> I do not have access to a Unix system currently so cannot check
> really...
Can you think of an easy test that we could run on windows and Linux
to add to your test harnesses?
WARNING [Errno 2] No such file or directory:
'/d/shared/cakes/php/etc/t.generic.css'
WARNING CSSImportRule: While processing imported style sheet
href=u't.generic.css': IOError('Cannot read Stylesheet.',)
WARNING [Errno 2] No such file or directory:
'/d/shared/cakes/php/etc/t.generic.css'
WARNING CSSImportRule: While processing imported style sheet
href=u't.generic.css': IOError('Cannot read Stylesheet.',)
import file:///d/shared/cakes/php/css/t.generic.css
This after adding
if rule.type == rule.IMPORT_RULE:
imp=cssutils.css.CSSImportRule(href="file:///d/shared/cakes/php/css/t.generic.css")
So a full url using file:// doesn't work either?
Note that the import (in the CSS stylesheet) is still showing relative
to .../etc
where the Python is.
I.e the cssutils.css.CSSImportRule isn't affecting the actual import statement?
OK. Possible differences, but same setup.
run from . with all stylesheets in ./sheets
python script in .
parser=cssutils.CSSParser() #return a DOM Level 2 CSS StyleSheet object
sheet = parser.parseFile(item,'utf-8',
href="sheets/import.css")
def checkrules(sheet):
nrules = sheet.cssRules.length
for item in range(0 , nrules):
rule = sheet.cssRules[item] #class cssutils.css.CSSRule
if rule.type == rule.IMPORT_RULE:
imp=cssutils.css.CSSImportRule(href="file:///files/css/cssutilsTest/sheets/import.css")
print "import ", imp.href
Shows :
[dpawson@marge cssutilsTest]$ python csscheck.py
WARNING ValueError, unknown url type: sheets/imported.css
WARNING CSSImportRule: While processing imported style sheet
href=u'imported.css': IOError('Cannot read Stylesheet.',)
WARNING ValueError, unknown url type: sheets/imported.css
WARNING CSSImportRule: While processing imported style sheet
href=u'imported.css': IOError('Cannot read Stylesheet.',)
******************* Checking sheets/import.css **********************
WARNING [Errno 2] No such file or directory:
'/files/css/cssutilsTest/imported.css'
WARNING CSSImportRule: While processing imported style sheet
href=u'imported.css': IOError('Cannot read Stylesheet.',)
WARNING [Errno 2] No such file or directory:
'/files/css/cssutilsTest/imported.css'
WARNING CSSImportRule: While processing imported style sheet
href=u'imported.css': IOError('Cannot read Stylesheet.',)
import file:///files/css/cssutilsTest/sheets/import.css
Warnings, but still seems wrong somehow.