| OLD | NEW |
| 1 #!/usr/local/bin/python | 1 #!/usr/local/bin/python |
| 2 # coding: utf-8 | 2 # coding: utf-8 |
| 3 | 3 |
| 4 import sys | 4 import sys |
| 5 import re | 5 import re |
| 6 | 6 |
| 7 # TODO(edisonn): put processed part of file in a new file | 7 # TODO(edisonn): put processed part of file in a new file |
| 8 # put unprocessed part, in a new file, so we see what we miss | 8 # put unprocessed part, in a new file, so we see what we miss |
| 9 # keep blank lines, and generate a version without the blank lines | 9 # keep blank lines, and generate a version without the blank lines |
| 10 | 10 |
| (...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 ret = ret.replace(u'\u2122', '(TM)') | 285 ret = ret.replace(u'\u2122', '(TM)') |
| 286 | 286 |
| 287 | 287 |
| 288 # how enable to emit this a python string | 288 # how enable to emit this a python string |
| 289 ret = ret.replace('\'', '\\\'') | 289 ret = ret.replace('\'', '\\\'') |
| 290 ret = ret.replace('\n', '\\n') | 290 ret = ret.replace('\n', '\\n') |
| 291 | 291 |
| 292 | 292 |
| 293 return ret | 293 return ret |
| 294 | 294 |
| 295 def commitRow(): | 295 def commitRow(fspecPy): |
| 296 global columnValues | 296 global columnValues |
| 297 global emitedDitionaryName | 297 global emitedDitionaryName |
| 298 global table | 298 global table |
| 299 global tableToClassName | 299 global tableToClassName |
| 300 | 300 |
| 301 if columnValues == None: | 301 if columnValues == None: |
| 302 return | 302 return |
| 303 | 303 |
| 304 #print columnValues | 304 #print columnValues |
| 305 | 305 |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 386 #else: | 386 #else: |
| 387 #print table | 387 #print table |
| 388 | 388 |
| 389 #print tableKey | 389 #print tableKey |
| 390 #print('\'' + tableKey + '\': [\'' + emitedDitionaryName + '\', \'' + table[
len(tableKey) + 1:] + '\'],') | 390 #print('\'' + tableKey + '\': [\'' + emitedDitionaryName + '\', \'' + table[
len(tableKey) + 1:] + '\'],') |
| 391 | 391 |
| 392 emitedDitionaryName = tableToClassName[tableKey][0] | 392 emitedDitionaryName = tableToClassName[tableKey][0] |
| 393 comment = fix(tableToClassName[tableKey][1]) | 393 comment = fix(tableToClassName[tableKey][1]) |
| 394 | 394 |
| 395 if len(tableToClassName[tableKey]) >= 3 and tableToClassName[tableKey][2] !=
'': | 395 if len(tableToClassName[tableKey]) >= 3 and tableToClassName[tableKey][2] !=
'': |
| 396 print(' pdfspec.addClass(\'' + emitedDitionaryName + '\', \'' + tableToCl
assName[tableKey][2] + '\', \'' + comment + '\')\\') | 396 fspecPy.write(' pdfspec.addClass(\'' + emitedDitionaryName + '\', \'' + t
ableToClassName[tableKey][2] + '\', \'' + comment + '\')\\\n') |
| 397 else: | 397 else: |
| 398 print(' pdfspec.addClass(\'' + emitedDitionaryName + '\', \'Dictionary\',
\'' + comment + '\')\\') | 398 fspecPy.write(' pdfspec.addClass(\'' + emitedDitionaryName + '\', \'Dicti
onary\', \'' + comment + '\')\\\n') |
| 399 | 399 |
| 400 if len(tableToClassName[tableKey]) >= 4 and columnValues[0] in tableToClassNam
e[tableKey][3]: | 400 if len(tableToClassName[tableKey]) >= 4 and columnValues[0] in tableToClassNam
e[tableKey][3]: |
| 401 required = True | 401 required = True |
| 402 | 402 |
| 403 if required: | 403 if required: |
| 404 print(' .required(\'NULL\')\\') | 404 fspecPy.write(' .required(\'NULL\')\\\n') |
| 405 else: | 405 else: |
| 406 print(' .optional()\\') | 406 fspecPy.write(' .optional()\\\n') |
| 407 | 407 |
| 408 print(' .field(\'' + columnValues[0] + '\')\\') | 408 fspecPy.write(' .field(\'' + columnValues[0] + '\')\\\n') |
| 409 print(' .name(\'' + columnValues[0] + '\')\\') | 409 fspecPy.write(' .name(\'' + columnValues[0] + '\')\\\n') |
| 410 print(' .type(\'' + columnValues[1] + '\')\\') | 410 fspecPy.write(' .type(\'' + columnValues[1] + '\')\\\n') |
| 411 print(' .comment(\'' + columnValues[2] + '\')\\') | 411 fspecPy.write(' .comment(\'' + columnValues[2] + '\')\\\n') |
| 412 | 412 |
| 413 if len(tableToClassName[tableKey]) >= 4 and columnValues[0] in tableToClassNam
e[tableKey][3]: | 413 if len(tableToClassName[tableKey]) >= 4 and columnValues[0] in tableToClassNam
e[tableKey][3]: |
| 414 print(' .must(' + tableToClassName[tableKey][3][columnValues[0]] +
')\\') | 414 fspecPy.write(' .must(' + tableToClassName[tableKey][3][columnValue
s[0]] + ')\\\n') |
| 415 | 415 |
| 416 print(' .done().done()\\') | 416 fspecPy.write(' .done().done()\\\n') |
| 417 | 417 |
| 418 | 418 |
| 419 columnValues = None | 419 columnValues = None |
| 420 | 420 |
| 421 def newRow(first, second, third): | 421 def newRow(first, second, third): |
| 422 global columnValues | 422 global columnValues |
| 423 columnValues = [first.rstrip(), second.rstrip(), third.rstrip()] | 423 columnValues = [first.rstrip(), second.rstrip(), third.rstrip()] |
| 424 | 424 |
| 425 def appendRow(second, third): | 425 def appendRow(second, third): |
| 426 global columnValues | 426 global columnValues |
| 427 if second.rstrip() != '': | 427 if second.rstrip() != '': |
| 428 columnValues[1] = columnValues[1] + ' ' + second.rstrip() | 428 columnValues[1] = columnValues[1] + ' ' + second.rstrip() |
| 429 if third.rstrip() != '': | 429 if third.rstrip() != '': |
| 430 columnValues[2] = columnValues[2] + '\n' + third.rstrip() | 430 columnValues[2] = columnValues[2] + '\n' + third.rstrip() |
| 431 | 431 |
| 432 def rebaseTable(line): | 432 def rebaseTable(fspecPy, line): |
| 433 global knownTypes | 433 global knownTypes |
| 434 global columnWidth | 434 global columnWidth |
| 435 | 435 |
| 436 line2 = line.replace(',', ' , ') | 436 line2 = line.replace(',', ' , ') |
| 437 | 437 |
| 438 words = line2.split() | 438 words = line2.split() |
| 439 | 439 |
| 440 if len(words) < 3: | 440 if len(words) < 3: |
| 441 return False | 441 return False |
| 442 | 442 |
| 443 i = 1 | 443 i = 1 |
| 444 while i < len(words) - 1 and words[i] in knownTypes: | 444 while i < len(words) - 1 and words[i] in knownTypes: |
| 445 i = i + 1 | 445 i = i + 1 |
| 446 | 446 |
| 447 if words[i].startswith('(Optional') or words[i].startswith('(Required'): | 447 if words[i].startswith('(Optional') or words[i].startswith('(Required'): |
| 448 commitRow() | 448 commitRow(fspecPy) |
| 449 | 449 |
| 450 columnWidth[0] = line.find(words[1]) | 450 columnWidth[0] = line.find(words[1]) |
| 451 | 451 |
| 452 if words[i].startswith('(Optional'): | 452 if words[i].startswith('(Optional'): |
| 453 columnWidth[1] = line.find('(Optional') - columnWidth[0] | 453 columnWidth[1] = line.find('(Optional') - columnWidth[0] |
| 454 if words[i].startswith('(Required'): | 454 if words[i].startswith('(Required'): |
| 455 columnWidth[1] = line.find('(Required') - columnWidth[0] | 455 columnWidth[1] = line.find('(Required') - columnWidth[0] |
| 456 return True | 456 return True |
| 457 | 457 |
| 458 return False | 458 return False |
| 459 | 459 |
| 460 | 460 |
| 461 def stopTable(): | 461 def stopTable(fspecPy): |
| 462 global tableHeaderFound | 462 global tableHeaderFound |
| 463 global emitedDitionaryName | 463 global emitedDitionaryName |
| 464 | 464 |
| 465 if not inTable(): | 465 if not inTable(): |
| 466 return | 466 return |
| 467 | 467 |
| 468 commitRow() | 468 commitRow(fspecPy) |
| 469 tableHeaderFound = False | 469 tableHeaderFound = False |
| 470 emitedDitionaryName = '' | 470 emitedDitionaryName = '' |
| 471 print(' .done()') | 471 fspecPy.write(' .done()\n') |
| 472 print | 472 fspecPy.write('\n') |
| 473 | 473 |
| 474 | 474 |
| 475 def killTable(): | 475 def killTable(): |
| 476 return | 476 return |
| 477 | 477 |
| 478 def processLineCore(line): | 478 def processLineCore(fspecPy, line): |
| 479 global lines | 479 global lines |
| 480 global tableLine | 480 global tableLine |
| 481 global tableRow | 481 global tableRow |
| 482 global columnWidth | 482 global columnWidth |
| 483 global columnValues | 483 global columnValues |
| 484 global mustFollowTableHeader | 484 global mustFollowTableHeader |
| 485 | 485 |
| 486 global fnewspec | 486 #global fnewspec |
| 487 | 487 |
| 488 lines = lines + 1 | 488 lines = lines + 1 |
| 489 | 489 |
| 490 line = unicode(line, 'utf8') | 490 line = unicode(line, 'utf8') |
| 491 | 491 |
| 492 striped = line.rstrip() | 492 striped = line.rstrip() |
| 493 | 493 |
| 494 words = line.split() | 494 words = line.split() |
| 495 if len(words) == 0: | 495 if len(words) == 0: |
| 496 stopTable() | 496 stopTable(fspecPy) |
| 497 return False | 497 return False |
| 498 | 498 |
| 499 isTableHeader = re.search('^[\s]*(TABLE [0-9].[0-9][0-9]?)', striped) | 499 isTableHeader = re.search('^[\s]*(TABLE [0-9].[0-9][0-9]?)', striped) |
| 500 if isTableHeader: | 500 if isTableHeader: |
| 501 stopTable() | 501 stopTable(fspecPy) |
| 502 tableDescriptionFound(striped) | 502 tableDescriptionFound(striped) |
| 503 mustFollowTableHeader = True | 503 mustFollowTableHeader = True |
| 504 return False | 504 return False |
| 505 | 505 |
| 506 if mustFollowTableHeader: | 506 if mustFollowTableHeader: |
| 507 mustFollowTableHeader = False | 507 mustFollowTableHeader = False |
| 508 if len(words) != 3: | 508 if len(words) != 3: |
| 509 killTable() | 509 killTable() |
| 510 return False | 510 return False |
| 511 | 511 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 528 third = striped[columnWidth[0] + columnWidth[1] :] | 528 third = striped[columnWidth[0] + columnWidth[1] :] |
| 529 | 529 |
| 530 if tableLine == 1: | 530 if tableLine == 1: |
| 531 if third[0] != '(': | 531 if third[0] != '(': |
| 532 killTable() | 532 killTable() |
| 533 return False | 533 return False |
| 534 | 534 |
| 535 newRow(first, second, third) | 535 newRow(first, second, third) |
| 536 return True | 536 return True |
| 537 | 537 |
| 538 if rebaseTable(striped): | 538 if rebaseTable(fspecPy, striped): |
| 539 first = striped[0 : columnWidth[0]] | 539 first = striped[0 : columnWidth[0]] |
| 540 second = striped[columnWidth[0] : columnWidth[0] + columnWidth[1]] | 540 second = striped[columnWidth[0] : columnWidth[0] + columnWidth[1]] |
| 541 third = striped[columnWidth[0] + columnWidth[1] :] | 541 third = striped[columnWidth[0] + columnWidth[1] :] |
| 542 | 542 |
| 543 first = first.rstrip() | 543 first = first.rstrip() |
| 544 second = second.rstrip() | 544 second = second.rstrip() |
| 545 third = third.rstrip() | 545 third = third.rstrip() |
| 546 | 546 |
| 547 if first == '' and second == '' and third != '': | 547 if first == '' and second == '' and third != '': |
| 548 appendRow(second, third) | 548 appendRow(second, third) |
| 549 return True | 549 return True |
| 550 | 550 |
| 551 if len(first.split()) > 1: | 551 if len(first.split()) > 1: |
| 552 stopTable() | 552 stopTable(fspecPy) |
| 553 return False | 553 return False |
| 554 | 554 |
| 555 if first != '' and first[0] == ' ': | 555 if first != '' and first[0] == ' ': |
| 556 stopTable() | 556 stopTable(fspecPy) |
| 557 return False | 557 return False |
| 558 | 558 |
| 559 if first != '' and second != '' and third == '': | 559 if first != '' and second != '' and third == '': |
| 560 stopTable() | 560 stopTable(fspecPy) |
| 561 return False | 561 return False |
| 562 | 562 |
| 563 if first == '' and second != '' and second[0] != ' ': | 563 if first == '' and second != '' and second[0] != ' ': |
| 564 if acceptType(second): | 564 if acceptType(second): |
| 565 appendRow(second, third) | 565 appendRow(second, third) |
| 566 return True | 566 return True |
| 567 else: | 567 else: |
| 568 stopTable() | 568 stopTable(fspecPy) |
| 569 return False | 569 return False |
| 570 | 570 |
| 571 if first != '' and second != '' and third[0] != '(': | 571 if first != '' and second != '' and third[0] != '(': |
| 572 stopTable() | 572 stopTable() |
| 573 return False | 573 return False |
| 574 | 574 |
| 575 if first == '' and second != '' and second[0] == ' ': | 575 if first == '' and second != '' and second[0] == ' ': |
| 576 stopTable() | 576 stopTable(fspecPy) |
| 577 return False | 577 return False |
| 578 | 578 |
| 579 if first != '' and second != '' and third[0] == '(': | 579 if first != '' and second != '' and third[0] == '(': |
| 580 commitRow() | 580 commitRow(fspecPy) |
| 581 newRow(first, second, third) | 581 newRow(first, second, third) |
| 582 return True | 582 return True |
| 583 | 583 |
| 584 return False | 584 return False |
| 585 return False | 585 return False |
| 586 | 586 |
| 587 def processLine(line): | 587 def processLine(fspecPy, line): |
| 588 global fnewspec | 588 #global fnewspec |
| 589 | 589 |
| 590 inSpec = processLineCore(line) | 590 inSpec = processLineCore(fspecPy, line) |
| 591 | 591 |
| 592 #just return, use the next lines if you wish to rewrite spec | 592 #just return, use the next lines if you wish to rewrite spec |
| 593 #return | 593 return |
| 594 | 594 |
| 595 if inSpec: | 595 if inSpec: |
| 596 #resize colum with types | 596 #resize colum with types |
| 597 line = line[:columnWidth[0] + columnWidth[1]] + (' ' * (60 - columnWidth[1])
) + line[columnWidth[0] + columnWidth[1]:] | 597 line = line[:columnWidth[0] + columnWidth[1]] + (' ' * (60 - columnWidth[1])
) + line[columnWidth[0] + columnWidth[1]:] |
| 598 line = line[:columnWidth[0]] + (' ' * (40 - columnWidth[0])) + line[columnWi
dth[0]:] | 598 line = line[:columnWidth[0]] + (' ' * (40 - columnWidth[0])) + line[columnWi
dth[0]:] |
| 599 | 599 |
| 600 fnewspec.write(line) | 600 #fnewspec.write(line) |
| 601 | 601 |
| 602 | 602 |
| 603 def generateDef(): | 603 def generateDef(): |
| 604 global lines | 604 global lines |
| 605 global fnewspec | 605 #global fnewspec |
| 606 | 606 |
| 607 fnewspec = open('PdfReference-okular-2.txt', 'w') | 607 #fnewspec = open('PdfReference-okular-2.txt', 'w') |
| 608 | 608 |
| 609 print 'import datatypes' | 609 # pdf spec in text format |
| 610 print | 610 fspecText = open(sys.argv[1], 'r') |
| 611 |
| 612 # pdf spec in python directives |
| 613 fspecPy = open(sys.argv[2], 'w') |
| 614 |
| 615 fspecPy.write('import datatypes\n') |
| 616 fspecPy.write('\n') |
| 611 | 617 |
| 612 print 'def buildPdfSpec(pdfspec):' | 618 fspecPy.write('def buildPdfSpec(pdfspec):\n') |
| 613 | 619 |
| 614 for line in sys.stdin: | 620 for line in fspecText: |
| 615 processLine(line) | 621 processLine(fspecPy, line) |
| 616 | 622 |
| 617 # close last table if it was not closed already | 623 # close last table if it was not closed already |
| 618 stopTable() | 624 stopTable(fspecPy) |
| 619 | 625 |
| 620 print | 626 fspecPy.write('\n') |
| 621 | 627 |
| 622 print 'def addDictionaryTypesTo(knowTypes):' | 628 fspecPy.write('def addDictionaryTypesTo(knowTypes):\n') |
| 623 for e in tableToClassName: | 629 for e in tableToClassName: |
| 624 print(' knowTypes[\'' + tableToClassName[e][0] + '\'] = [\'SkPdf' + tableTo
ClassName[e][0] + '*\', \'' + tableToClassName[e][0] + 'FromDictionary\', dataty
pes.CppNull(), \'ret->podofo()->GetDataType() == ePdfDataType_Dictionary\']') | 630 fspecPy.write(' knowTypes[\'' + tableToClassName[e][0] + '\'] = [\'SkPdf' +
tableToClassName[e][0] + '*\', \'SkPdf' + tableToClassName[e][0] + 'FromDiction
ary\', datatypes.CppNull(), \'ret->podofo()->GetDataType() == PoDoFo::ePdfDataTy
pe_Dictionary\', \'A_DICTIONARY\']\n') |
| 625 print | 631 fspecPy.write('\n') |
| 626 print | |
| 627 | |
| 628 | 632 |
| 629 #print lines | 633 #print lines |
| 630 fnewspec.close() | 634 #fnewspec.close() |
| 631 | 635 |
| 632 if '__main__' == __name__: | 636 if '__main__' == __name__: |
| 633 sys.exit(generateDef()) | 637 sys.exit(generateDef()) |
| OLD | NEW |