|
18 | 18 | """
|
19 | 19 |
|
20 | 20 |
|
| 21 | + |
21 | 22 | __author__ = 'Victor Olaya'
|
22 | 23 | __date__ = 'August 2012'
|
23 | 24 | __copyright__ = '(C) 2012, Victor Olaya'
|
|
31 | 32 | import copy
|
32 | 33 | import time
|
33 | 34 | import json
|
| 35 | +import codecs |
| 36 | +import traceback |
34 | 37 | from PyQt4 import QtCore, QtGui
|
35 | 38 | from qgis.core import *
|
36 | 39 | from processing.core.GeoAlgorithm import GeoAlgorithm
|
|
41 | 44 | from processing.modeler.ModelerUtils import ModelerUtils
|
42 | 45 | from processing.core.parameters import *
|
43 | 46 | from processing.tools import dataobjects
|
| 47 | +from processing.core.parameters import getParameterFromString |
| 48 | + |
44 | 49 |
|
45 | 50 |
|
46 | 51 | class ModelerParameter():
|
@@ -524,3 +529,115 @@ def fromJsonFile(filename):
|
524 | 529 | alg.descriptionFile = filename
|
525 | 530 | return alg
|
526 | 531 |
|
| 532 | + |
| 533 | + ############LEGACY METHOD TO SUPPORT OLD FORMAT########### |
| 534 | + |
| 535 | + LINE_BREAK_STRING = '%%%' |
| 536 | + |
| 537 | + @staticmethod |
| 538 | + def fromFile(filename): |
| 539 | + try: |
| 540 | + alg = ModelerAlgorithm.fromJsonFile(filename) |
| 541 | + return alg |
| 542 | + except WrongModelException, e: |
| 543 | + alg = ModelerAlgorithm.fromOldFormatFile(filename) |
| 544 | + return alg |
| 545 | + |
| 546 | + |
| 547 | + @staticmethod |
| 548 | + def fromOldFormatFile(filename): |
| 549 | + hardcodedValues = {} |
| 550 | + modelParameters = [] |
| 551 | + modelAlgs = [] |
| 552 | + model = ModelerAlgorithm() |
| 553 | + model.descriptionFile = filename |
| 554 | + lines = codecs.open(filename, 'r', encoding='utf-8') |
| 555 | + line = lines.readline().strip('\n').strip('\r') |
| 556 | + try: |
| 557 | + while line != '': |
| 558 | + if line.startswith('PARAMETER:'): |
| 559 | + paramLine = line[len('PARAMETER:'):] |
| 560 | + param = getParameterFromString(paramLine) |
| 561 | + if param: |
| 562 | + pass |
| 563 | + else: |
| 564 | + raise WrongModelException('Error in parameter line: ' |
| 565 | + + line) |
| 566 | + line = lines.readline().strip('\n') |
| 567 | + tokens = line.split(',') |
| 568 | + model.addParameter(ModelerParameter(param, QtCore.QPointF( |
| 569 | + float(tokens[0]), float(tokens[1])))) |
| 570 | + modelParameters.append(param.name) |
| 571 | + elif line.startswith('VALUE:'): |
| 572 | + valueLine = line[len('VALUE:'):] |
| 573 | + tokens = valueLine.split('===') |
| 574 | + name = tokens[0] |
| 575 | + value = tokens[1].replace(ModelerAlgorithm.LINE_BREAK_STRING, '\n') |
| 576 | + hardcodedValues[name] = value |
| 577 | + elif line.startswith('NAME:'): |
| 578 | + model.name = line[len('NAME:'):] |
| 579 | + elif line.startswith('GROUP:'): |
| 580 | + model.group = line[len('GROUP:'):] |
| 581 | + elif line.startswith('ALGORITHM:'): |
| 582 | + algLine = line[len('ALGORITHM:'):] |
| 583 | + alg = ModelerUtils.getAlgorithm(algLine) |
| 584 | + if alg is not None: |
| 585 | + modelAlg = Algorithm(alg.commandLineName()) |
| 586 | + modelAlg.description = alg.name |
| 587 | + posline = lines.readline().strip('\n').strip('\r') |
| 588 | + tokens = posline.split(',') |
| 589 | + modelAlg.pos = QtCore.QPointF(float(tokens[0]), float(tokens[1])) |
| 590 | + dependenceline = lines.readline().strip('\n').strip('\r') #unused |
| 591 | + for param in alg.parameters: |
| 592 | + if not param.hidden: |
| 593 | + line = lines.readline().strip('\n').strip('\r') |
| 594 | + if line == str(None): |
| 595 | + modelAlg.params[param.name] = None |
| 596 | + else: |
| 597 | + tokens = line.split('|') |
| 598 | + algIdx = int(tokens[0]) |
| 599 | + if algIdx == -1: |
| 600 | + if tokens[1] in modelParameters: |
| 601 | + modelAlg.params[param.name] = ValueFromInput(tokens[1]) |
| 602 | + else: |
| 603 | + modelAlg.params[param.name] = hardcodedValues[tokens[1]] |
| 604 | + else: |
| 605 | + modelAlg.params[param.name] = ValueFromOutput(algIdx, tokens[1]) |
| 606 | + |
| 607 | + for out in alg.outputs: |
| 608 | + if not out.hidden: |
| 609 | + line = lines.readline().strip('\n').strip('\r') |
| 610 | + if str(None) != line: |
| 611 | + if '|' in line: |
| 612 | + tokens = line.split('|') |
| 613 | + name = tokens[0] |
| 614 | + tokens = tokens[1].split(',') |
| 615 | + pos = QtCore.QPointF( |
| 616 | + float(tokens[0]), float(tokens[1])) |
| 617 | + else: |
| 618 | + name = line |
| 619 | + pos = None |
| 620 | + modelerOutput = ModelerOutput(name) |
| 621 | + modelerOutput.pos = pos |
| 622 | + modelAlg.outputs[out.name] = modelerOutput |
| 623 | + |
| 624 | + model.addAlgorithm(modelAlg) |
| 625 | + modelAlgs.append(modelAlg.name) |
| 626 | + else: |
| 627 | + raise WrongModelException('Error in algorithm name: ' |
| 628 | + + algLine) |
| 629 | + line = lines.readline().strip('\n').strip('\r') |
| 630 | + for modelAlg in model.algs.values(): |
| 631 | + for name, value in modelAlg.params.iteritems(): |
| 632 | + if isinstance(value, ValueFromOutput): |
| 633 | + value.alg = modelAlgs[value.alg] |
| 634 | + return model |
| 635 | + except Exception, e: |
| 636 | + if isinstance(e, WrongModelException): |
| 637 | + raise e |
| 638 | + else: |
| 639 | + raise WrongModelException('Error in model definition line:' |
| 640 | + + line.strip() + ' : ' + traceback.format_exc()) |
| 641 | + |
| 642 | + |
| 643 | + |
0 commit comments