| Total Complexity | 56 |
| Total Lines | 243 |
| Duplicated Lines | 3.7 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ConfigMainWindow often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # -*- coding: utf-8 -*- |
||
| 44 | class ConfigMainWindow(QtWidgets.QMainWindow): |
||
| 45 | """ This class represents the Manager Window. |
||
| 46 | """ |
||
| 47 | def __init__(self, loadfile=None): |
||
| 48 | """ Create the Manager Window. |
||
| 49 | """ |
||
| 50 | # Get the path to the *.ui file |
||
| 51 | this_dir = os.path.dirname(__file__) |
||
| 52 | ui_file = os.path.join(this_dir, 'ui_config_window.ui') |
||
| 53 | self.log = logging.getLogger(__name__) |
||
| 54 | |||
| 55 | # Load it |
||
| 56 | super().__init__() |
||
| 57 | uic.loadUi(ui_file, self) |
||
| 58 | |||
| 59 | self.mods = dict() |
||
| 60 | self.globalsection = OrderedDict() |
||
| 61 | self.currentFile = '' |
||
| 62 | |||
| 63 | # init |
||
| 64 | self.setupUi() |
||
| 65 | self.show() |
||
| 66 | |||
| 67 | if loadfile is not None: |
||
| 68 | self.loadConfigFile(loadfile) |
||
| 69 | |||
| 70 | def setupUi(self): |
||
| 71 | self.actionNew_configuration.triggered.connect(self.newConfigFile) |
||
| 72 | self.actionSave_configuration.triggered.connect(self.saveConfigFile) |
||
| 73 | self.actionSave_configuration_as.triggered.connect(self.saveConfigFileAs) |
||
| 74 | self.actionOpen_configuration.triggered.connect(self.openConfigFile) |
||
| 75 | self.actionDelete_selected_nodes.triggered.connect(self.graphView.deleteSelectedNodes) |
||
| 76 | self.actionFrame_selected_nodes.triggered.connect(self.graphView.frameSelectedNodes) |
||
| 77 | self.actionFrame_all_nodes.activated.connect(self.graphView.frameAllNodes) |
||
| 78 | |||
| 79 | # add module menu |
||
| 80 | self.findModules() |
||
| 81 | self.mmroot = ModMenu(self.m) |
||
| 82 | for mod in self.mmroot.modules: |
||
| 83 | mod.sigAddModule.connect(self.addModule) |
||
| 84 | self.actionAdd_Module.setMenu(self.mmroot) |
||
| 85 | |||
| 86 | # node change signals |
||
| 87 | self.graphView.nodeAdded.connect(self.nodeAdded) |
||
| 88 | self.graphView.nodeRemoved.connect(self.nodeRemoved) |
||
| 89 | View Code Duplication | self.graphView.nodeNameChanged.connect(self.nodeNameChanged) |
|
| 90 | |||
| 91 | def findModules(self): |
||
| 92 | modules = listmods.find_pyfiles(os.getcwd()) |
||
| 93 | m, i_s, ie, oe = listmods.check_qudi_modules(modules) |
||
| 94 | self.m = m |
||
| 95 | |||
| 96 | if len(oe) > 0 or len(ie) > 0: |
||
| 97 | print('\n========== ERRORS: ===========', file=sys.stderr) |
||
| 98 | for e in oe: |
||
| 99 | print(e[0], file=sys.stderr) |
||
| 100 | print(e[1], file=sys.stderr) |
||
| 101 | |||
| 102 | for e in ie: |
||
| 103 | print(e[0], file=sys.stderr) |
||
| 104 | print(e[1], file=sys.stderr) |
||
| 105 | # print(self.m) |
||
| 106 | |||
| 107 | def addModule(self, module, name=None, pos=(0,0)): |
||
| 108 | if name is None: |
||
| 109 | name = 'new_module' |
||
| 110 | n = 1 |
||
| 111 | if self.graphView.hasNode(name): |
||
| 112 | while self.graphView.hasNode('{}{}'.format(name, n)): |
||
| 113 | n += 1 |
||
| 114 | name = '{}{}'.format(name, n) |
||
| 115 | |||
| 116 | g = self.graphView |
||
| 117 | node = Node(g, name) |
||
| 118 | if module.path.startswith('hardware'): |
||
| 119 | node.setColor(palette.c2) |
||
| 120 | elif module.path.startswith('logic'): |
||
| 121 | node.setColor(palette.c1) |
||
| 122 | elif module.path.startswith('gui'): |
||
| 123 | node.setColor(palette.c4) |
||
| 124 | else: |
||
| 125 | node.setColor(palette.c3) |
||
| 126 | |||
| 127 | for conn in module.conn_in: |
||
| 128 | node.addPort(InputPort(node, g, conn[0], palette.c3, conn[1])) |
||
| 129 | |||
| 130 | for conn in module.conn_out: |
||
| 131 | node.addPort(OutputPort(node, g, conn[0], palette.c3, conn[1])) |
||
| 132 | |||
| 133 | node.setGraphPos(QtCore.QPointF(pos[0], pos[1])) |
||
| 134 | |||
| 135 | self.mods[name] = { |
||
| 136 | 'node': node, |
||
| 137 | 'module': module, |
||
| 138 | } |
||
| 139 | g.addNode(node) |
||
| 140 | |||
| 141 | def openConfigFile(self): |
||
| 142 | defaultconfigpath = '' |
||
| 143 | filename = QtWidgets.QFileDialog.getOpenFileName( |
||
| 144 | self, |
||
| 145 | 'Load Configration', |
||
| 146 | defaultconfigpath , |
||
| 147 | 'Configuration files (*.cfg)') |
||
| 148 | if len(filename) > 0: |
||
| 149 | print('Open:', filename) |
||
| 150 | self.loadConfigFile(filename) |
||
| 151 | |||
| 152 | def loadConfigFile(self, filename): |
||
| 153 | config = core.config.load(filename) |
||
| 154 | self.configToNodes(config) |
||
| 155 | self.currentFile = filename |
||
| 156 | self.graphView.frameAllNodes() |
||
| 157 | |||
| 158 | def newConfigFile(self): |
||
| 159 | self.graphView.reset() |
||
| 160 | self.configFileName = 'New configuration' |
||
| 161 | self.updateWindowTitle(self.configFileName, extra='*') |
||
| 162 | |||
| 163 | def saveConfigFile(self): |
||
| 164 | if os.path.isfile(self.currentFile): |
||
| 165 | config = self.nodesToConfig() |
||
| 166 | core.config.save(self.currentFile, config) |
||
| 167 | else: |
||
| 168 | self.saveConfigFileAs() |
||
| 169 | |||
| 170 | def saveConfigFileAs(self): |
||
| 171 | defaultconfigpath = os.path.dirname(self.currentFile) |
||
| 172 | filename = QtWidgets.QFileDialog.getSaveFileName( |
||
| 173 | self, |
||
| 174 | 'Save Configration As', |
||
| 175 | defaultconfigpath , |
||
| 176 | 'Configuration files (*.cfg)') |
||
| 177 | if len(filename) > 0: |
||
| 178 | print('Save:', filename) |
||
| 179 | config = self.nodesToConfig() |
||
| 180 | core.config.save(filename, config) |
||
| 181 | self.currentFile = filename |
||
| 182 | |||
| 183 | def updateWindowTitle(self, filename, extra=''): |
||
| 184 | self.setWindowTitle('{}{} - Qudi configuration editor'.format(filename, extra)) |
||
| 185 | |||
| 186 | def getModuleInfo(self): |
||
| 187 | modules = listmods.find_pyfiles(os.getcwd()) |
||
| 188 | m, i_s, ie, oe = listmods.check_qudi_modules(modules) |
||
| 189 | |||
| 190 | def configToNodes(self, config): |
||
| 191 | pos = [0, 0] |
||
| 192 | for b,m in config.items(): |
||
| 193 | if b not in ['hardware', 'logic', 'gui']: |
||
| 194 | continue |
||
| 195 | for k,v in m.items(): |
||
| 196 | mc = 'module.Class' |
||
| 197 | #print(b, k, v) |
||
| 198 | if mc in v and self.mmroot.hasModule(b + '.' + v[mc]): |
||
| 199 | mod = self.mmroot.getModule(b + '.' + v[mc]) |
||
| 200 | self.addModule(mod, k, pos) |
||
| 201 | pos[1] += 100 |
||
| 202 | pos[0] += 600 |
||
| 203 | pos[1] = 0 |
||
| 204 | |||
| 205 | for b,m in config.items(): |
||
| 206 | if b not in ['hardware', 'logic', 'gui']: |
||
| 207 | continue |
||
| 208 | for k,v in m.items(): |
||
| 209 | if 'connect' in v: |
||
| 210 | for conn_in, conn_out in v['connect'].items(): |
||
| 211 | cl = conn_out.split('.') |
||
| 212 | src = '.'.join(cl[:-1]) |
||
| 213 | |||
| 214 | if k not in self.mods: |
||
| 215 | self.log.error( |
||
| 216 | 'Target module {} not present while connecting {}.{} to {}' |
||
| 217 | ''.format(k, conn_in, src, cl[-1])) |
||
| 218 | continue |
||
| 219 | if conn_in not in [c[0] for c in self.mods[k]['module'].conn_in]: |
||
| 220 | self.log.error( |
||
| 221 | 'Target connector {} not present while connecting {}.{} to {}.{}' |
||
| 222 | ''.format(conn_in, src, cl[-1], k, conn_in)) |
||
| 223 | continue |
||
| 224 | if src not in self.mods: |
||
| 225 | self.log.error( |
||
| 226 | 'Source module {} not present while connecting {} to {}.{}' |
||
| 227 | ''.format(src, cl[-1], k, conn_in)) |
||
| 228 | continue |
||
| 229 | if cl[-1] not in [c[0] for c in self.mods[src]['module'].conn_out]: |
||
| 230 | self.log.error( |
||
| 231 | 'Source connector {} not present while connecting {}.{} to {}.{}' |
||
| 232 | ''.format(conn_in, src, cl[-1], k, conn_in)) |
||
| 233 | continue |
||
| 234 | |||
| 235 | try: |
||
| 236 | self.graphView.connectPorts(self.mods[src]['node'], cl[-1], self.mods[k]['node'], conn_in) |
||
| 237 | except: |
||
| 238 | self.log.error( |
||
| 239 | 'pyflowgraph failed while connecting {}.{} to {}.{}' |
||
| 240 | ''.format(src, cl[-1], k, conn_in)) |
||
| 241 | |||
| 242 | self.globalsection = config['global'] |
||
| 243 | |||
| 244 | def nodesToConfig(self): |
||
| 245 | """ Convert nodes into OrderedDict for saving. |
||
| 246 | """ |
||
| 247 | config = OrderedDict() |
||
| 248 | config['global'] = OrderedDict() |
||
| 249 | config['hardware'] = OrderedDict() |
||
| 250 | config['logic'] = OrderedDict() |
||
| 251 | config['gui'] = OrderedDict() |
||
| 252 | |||
| 253 | for key,value in self.globalsection.items(): |
||
| 254 | config['global'][key] = value |
||
| 255 | |||
| 256 | for mname,mod in self.mods.items(): |
||
| 257 | entry = OrderedDict() |
||
| 258 | path = mod['module'].path.split('.') |
||
| 259 | |||
| 260 | if len(path) > 1 and path[0] in ('hardware', 'logic', 'gui'): |
||
| 261 | config[path[0]][mname] = entry |
||
| 262 | entry['module.Class'] = '.'.join(path[1:]) |
||
| 263 | |||
| 264 | portin = (mod['node'].getPort(x[0]) for x in mod['module'].conn_in) |
||
| 265 | conndict = OrderedDict() |
||
| 266 | for port in portin: |
||
| 267 | conns = port.inCircle().getConnections() |
||
| 268 | if len(conns) == 1: |
||
| 269 | c = tuple(conns)[0] |
||
| 270 | src = c.getSrcPort() |
||
| 271 | node = src.getNode() |
||
| 272 | conndict[port.getName()] = '{}.{}'.format(node.getName(), src.getName()) |
||
| 273 | if len(conndict) > 0: |
||
| 274 | entry['connect'] = conndict |
||
| 275 | print(entry) |
||
| 276 | |||
| 277 | return config |
||
| 278 | |||
| 279 | def nodeAdded(self): |
||
| 280 | pass |
||
| 281 | |||
| 282 | def nodeRemoved(self): |
||
| 283 | pass |
||
| 284 | |||
| 285 | def nodeNameChanged(self): |
||
| 286 | pass |
||
| 287 | |||
| 297 |