4 class Error(Exception):
5 def __init__(self
, value
):
6 Exception.__init
__(self
)
9 return repr(self
.value
)
12 def __init__(self
, namestr
, defaultstr
, exprstr
):
13 self
.namestr
= namestr
14 self
.defaultstr
= defaultstr
15 self
.exprstr
= exprstr
21 return self
.defaultstr
!= None
24 if self
.defaultstr
== None:
25 raise Error("Error: Trying to eval variable " + self
.namestr
+ " with no value")
26 return self
.defaultstr
28 class OptionalSetting
:
29 def __init__(self
, namestr
, exprstr
):
30 self
.namestr
= namestr
31 self
.exprstr
= exprstr
36 def evaluate(self
, var_list
):
39 if var_list
[var
].has_value():
40 p
= re
.compile( "\$\{" + var
+ "\}")
41 instr
= p
.sub( var_list
[var
].evaluate(), instr
)
42 if instr
.find("$") != -1:
43 instr
= "" # if not all variables were found, return empty option
47 def __init__(self
, namestr
, callstr
, var_list
, optset_list
):
48 self
.namestr
= namestr
49 self
.callstr
= callstr
50 self
.var_list
= var_list
51 self
.optset_list
= optset_list
56 def put_var(self
, var
):
57 self
.var_list
[str(var
)] = var
65 p
= re
.compile("\$\[(\w+)\]")
66 for o
in p
.findall(instr
):
67 if o
not in self
.optset_list
:
68 raise Error("Error: Optional variable not defined")
69 instr
= p
.sub(self
.optset_list
[o
].evaluate(self
.var_list
), instr
)
71 p
= re
.compile("\$\{(\w+)\}")
72 for o
in p
.findall(instr
):
73 if o
not in self
.var_list
:
74 raise Error("Error: Variable \"" + o
+ "\" has not been assigned")
75 val
= self
.var_list
[o
].evaluate()
76 instr
= instr
.replace("${" + o
+ "}", val
)
78 if instr
.find("$") != -1:
79 raise Error("Error: all variables were not expanded")
83 return self
.evaluate()
87 self
.cmdcall_dict
= {}
90 def _read_properties(self
, propnode
, mand_propnames
, opt_propnames
= []):
94 if p
.name
in mand_propnames
:
95 propdict
[p
.name
] = str(p
.content
)
96 elif p
.name
!= "text" and p
.name
not in opt_propnames
:
97 raise Error("Error parsing XML: only name and call are accepted properties in the element command, found " + str(p
.name
))
99 if len(mand_propnames
) != len(propdict
.keys()):
100 raise Error("Error parsing XML: must supply both name and call in element command")
103 if p
.name
in opt_propnames
:
104 propdict
[p
.name
] = str(p
.content
)
105 elif p
.name
!= "text" and p
.name
not in mand_propnames
:
106 raise Error("Error parsing XML: only name and call are accepted properties in the element command, found " + str(p
.name
))
110 def _read_config(self
):
111 doc
= libxml2
.parseFile("rendercommands.xml")
112 cmd_xmllist
= doc
.xpathEval( '//command')
114 # read all commands from XML description
115 for cmdnode
in cmd_xmllist
:
116 if cmdnode
.type == "element" and cmdnode
.name
== "command":
122 for varsetnode
in cmdnode
.children
:
123 if varsetnode
.type == "element" and varsetnode
.name
== "variable":
124 props
= self
._read
_properties
(varsetnode
.properties
, ["name", "expr"], ["default"])
126 if "default" in props
:
127 defprop
= props
["default"]
128 var_list
[props
["name"]] = Variable(props
["name"], defprop
, props
["expr"])
130 elif varsetnode
.type == "element" and varsetnode
.name
== "optionalsetting":
131 props
= self
._read
_properties
(varsetnode
.properties
, ["name", "expr"])
132 optset_list
[props
["name"]] = OptionalSetting(props
["name"], props
["expr"])
134 elif varsetnode
.name
!= "text":
135 raise Error("Error parsing XML: only variable and optionalsetting elements allowed in command")
137 props
= self
._read
_properties
(cmdnode
.properties
, ["name", "call"])
138 self
.cmdcall_dict
[props
["name"]] = RenderCommand(props
["name"], props
["call"], var_list
, optset_list
)
141 raise Error("Error parsing XML: only command elements are supported")
144 return self
.cmdcall_dict
.values().__iter
__()
147 commands
= RenderCommands()
149 c
.put_var(Variable("in", "src.avi", None))
150 c
.put_var(Variable("out", "dest.avi", None))
153 if __name__
== "__main__":