View Single Post
Old 01-18-2018, 07:09 PM   #267
slowsmile
Witchman
slowsmile ought to be getting tired of karma fortunes by now.slowsmile ought to be getting tired of karma fortunes by now.slowsmile ought to be getting tired of karma fortunes by now.slowsmile ought to be getting tired of karma fortunes by now.slowsmile ought to be getting tired of karma fortunes by now.slowsmile ought to be getting tired of karma fortunes by now.slowsmile ought to be getting tired of karma fortunes by now.slowsmile ought to be getting tired of karma fortunes by now.slowsmile ought to be getting tired of karma fortunes by now.slowsmile ought to be getting tired of karma fortunes by now.slowsmile ought to be getting tired of karma fortunes by now.
 
Posts: 628
Karma: 788808
Join Date: May 2013
Location: Philippines
Device: Android S5
@Becky...Regarding just getting the line number for the meta language identifier, you could perhaps put Kevin's code into a simple function and get the line number something like this:

Code:
def getOPFLineNumber(opf, search_text):
    opf_data = opf.splitlines()     # split the opf string into separate lines of code
    
    # assign a line num to the search text when found
    linenum = ''
    for index, line in enumerate(opf_data):
        if search_text in line:
            linum = index + 1
            linenum = str(linum)
            return(linenum)
Then your plugin.py code could perhaps be adjusted to look like this:

Code:
  dc_language = None
    meta_language = tree.find('.//{http://purl.org/dc/elements/1.1/}language')
    if hasattr(meta_language, 'text'):
        dc_language = meta_language.text

    if dc_language:
        if dc_language != "pl":
            opf_data = bk.get_opf()    # get all the opf data as a string
            linenumber = getOPFLineNumber(opf_data, "<dc:language>")
            message = "Language specified in metadata is other than 'pl' (Polish)"
            bk.add_extended_result("error", "content.opf", linenumber , 0, 'Becky Metadata #002' + ' -- ' + message)
    else:
        message = "Language not specified in metadata"
        bk.add_extended_result("error", "content.opf", 0, 0, 'Becky Metadata #001' + ' -- ' + message)
I've used a similar getOPFLineNumber() function in my IDErrorCheck plugin which works without problems to get OPF line numbers on error.

Also, I know that using bk.get_opf() will certainly work for an Edit plugin but I'm not at all sure whether this bk method will be available for a Validation plugin like your plugin. If this is true then you could probably use xml.etree to get the opf file contents as a string instead of using bk.get_opf().

Last edited by slowsmile; 01-18-2018 at 09:58 PM.
slowsmile is offline   Reply With Quote