sample code to call GUI
[pyvconv.git] / pyvconv.py
blobc6c3b9a7250eb3f445876ee25a6685f6e3df0e19
1 import libxml2
2 import re
4 class Error(Exception):
5 def __init__(self, value):
6 Exception.__init__(self)
7 self.value = value
8 def __str__(self):
9 return repr(self.value)
11 class Variable:
12 def __init__(self, namestr, defaultstr, exprstr):
13 self.namestr = namestr
14 self.defaultstr = defaultstr
15 self.exprstr = exprstr
17 def __str__(self):
18 return self.namestr
20 def has_value(self):
21 return self.defaultstr != None
23 def evaluate(self):
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
33 def get_name(self):
34 return self.namestr
36 def evaluate(self, var_list):
37 instr = self.exprstr
38 for var in 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
44 return instr
46 class RenderCommand:
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
53 def get_name(self):
54 return self.namestr
56 def put_var(self, var):
57 self.var_list[str(var)] = var
59 def get_vars(self):
60 return self.var_list
62 def evaluate(self):
63 instr = self.callstr
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")
80 return instr
82 def __str__(self):
83 return self.evaluate()
85 class RenderCommands:
86 def __init__(self):
87 self.cmdcall_dict = {}
88 self._read_config()
90 def _read_properties(self, propnode, mand_propnames, opt_propnames = []):
91 propdict = {}
93 for p in propnode:
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")
102 for p in propnode:
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))
108 return propdict
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":
118 var_list = {}
119 optset_list = {}
121 if cmdnode.children:
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"])
125 defprop = None
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)
140 else:
141 raise Error("Error parsing XML: only command elements are supported")
143 def __iter__(self):
144 return self.cmdcall_dict.values().__iter__()
146 def main():
147 commands = RenderCommands()
148 for c in commands:
149 c.put_var(Variable("in", "src.avi", None))
150 c.put_var(Variable("out", "dest.avi", None))
151 print c
153 if __name__ == "__main__":
154 main()