def load(self): import moduleparse src = open(self.resourcepath).readlines() module = moduleparse.Module(self.name, src) # find keydefs keydefs = {} names = [] values = [] start = end = idx = -1 for line in src: idx = idx + 1 line = line.strip() if line == 'keyDefs = {': start = idx elif start != -1 and line: if line[-1] == '}': end = idx break elif line[0] != '#': colon = line.find(':') if colon == -1: raise Exception(_('Invalid KeyDef item: %s') % line) name = line[:colon].rstrip()[1:-1] val = line[colon + 1:].lstrip() keydefs[name] = moduleparse.CodeBlock( val, idx + 1, idx + 1) names.append(name) values.append(val) return (names, values, keydefs, [''] * len(keydefs), ['## keydef'] * len(keydefs), module.break_lines)
def load(self): # All preferences are local import moduleparse module = moduleparse.Module(self.name, open(self.resourcepath).readlines()) values = [] comments = [] options = [] # keep only names defined in the property list for name in module.global_order[:]: if name[0] == '_' or self.properties != ('*',) and \ name not in self.properties: module.global_order.remove(name) del module.globals[name] else: # XXX Should handle multiline assign code = '\n'.join(module.source[\ module.globals[name].start-1 : \ module.globals[name].end]) # Extract value s = code.find('=') if s != -1: values.append(code[s + 1:].strip()) else: values.append('') # Read possible comment/help or options comment = [] option = '' idx = module.globals[name].start - 2 while idx >= 0: line = module.source[idx].strip() if len(line) > 11 and line[:11] == '## options:': option = line[11:].strip() idx = idx - 1 elif len(line) > 8 and line[:8] == '## type:': option = '##' + line[8:].strip() idx = idx - 1 elif line and line[0] == '#': comment.append(line[1:].lstrip()) idx = idx - 1 else: break comment.reverse() comments.append(_('\n'.join(comment))) options.append(option) breaks = {} if self.showBreakLines: for k, v in module.break_lines.items(): breaks[k] = _(v) return (module.global_order, values, module.globals, comments, options, breaks)
def getModule(self): if self._module is None: import moduleparse wx.BeginBusyCursor() try: self._module = moduleparse.Module(self.transport.whole_name, self.data.split('\012')) finally: wx.EndBusyCursor() return self._module
def getModule(self): if self._module is None: wx.BeginBusyCursor() try: import moduleparse self._module = moduleparse.Module(self.moduleName, self.getDataAsLines()) finally: wx.EndBusyCursor() return self._module
def updateRcFile(rcFile, propName, propSrcValue): from Explorers.PrefsExplorer import UsedModuleSrcBsdPrefColNode import moduleparse prefsRcFile = os.path.join(Preferences.rcPath, rcFile) m = moduleparse.Module(rcFile, open(prefsRcFile).readlines()) prefsRcNode = UsedModuleSrcBsdPrefColNode('', ('*', ), prefsRcFile, -1, None, Preferences, True) newProp = (propName, propSrcValue, m.globals[propName]) prefsRcNode.save(propName, newProp)
def getFunctions(self, module): from Explorers import Explorer extPath = self.getExtPath(module) src = Explorer.openEx(extPath).load() sep = src.count('\r\n') < src.count('\n') and '\n' or '\r\n' srclines = src.split(sep) import moduleparse module = moduleparse.Module('test', srclines) return module.functions.keys()
def registerPreference(pluginName, prefName, defPrefValSrc, docs=[], info=''): """ Define a plug-in preference. Added to prefs.plug-ins.rc.py in needed """ def addBlankLine(module, lineNo): module.addLine('', lineNo) return lineNo + 1 Preferences.exportedPluginProps.append(prefName) # quick exit when name already exists if hasattr(Preferences, prefName): return pluginPrefs = os.path.join(Preferences.rcPath, 'prefs.plug-ins.rc.py') lines = [l.rstrip() for l in open(pluginPrefs).readlines()] import moduleparse m = moduleparse.Module(pluginName, lines) if not m.globals.has_key(prefName): breakLineNames = m.break_lines.values() if pluginName not in breakLineNames: lineNo = addBlankLine(m, len(lines)) lineNo = addBlankLine(m, lineNo) m.addLine( '#-%s%s' % (pluginName, '-' * (80 - 2 - len(pluginName))), lineNo) lineNo = addBlankLine(m, lineNo + 1) else: for l, n in m.break_lines.items(): if pluginName == n: lineNo = l + 1 break else: lineNo = len(lines) if docs: for doc in docs: m.addLine('# %s' % doc, lineNo) lineNo += 1 if info: m.addLine('## %s' % info, lineNo) lineNo += 1 try: value = eval(defPrefValSrc, Preferences.__dict__) except Exception, err: raise PluginError(( _('Could not create default value from "%s" for %s. (%s:%s)') % (defPrefValSrc, prefName, err.__class__, err))) m.addLine('%s = %s' % (prefName, defPrefValSrc), lineNo) lineNo = addBlankLine(m, lineNo + 1) setattr(Preferences, prefName, value) open(pluginPrefs, 'wb').write(os.linesep.join(m.source))
def parsePrefsFile(prefsPath, comments, breaks): module = moduleparse.Module(os.path.basename(prefsPath), open(prefsPath).readlines()) for name in module.global_order[:]: # Read possible comment/help or options comment = [] option = '' idx = module.globals[name].start-2 while idx >= 0: line = module.source[idx].strip() if len(line) > 11 and line[:11] == '## options:': option = line[11:].strip() idx = idx - 1 elif len(line) > 8 and line[:8] == '## type:': option = '##'+line[8:].strip() idx = idx - 1 elif line and line[0] == '#': comment.append(line[1:].lstrip()) idx = idx - 1 else: break comment.reverse() comments.append( (name, '\n'.join(comment)) ) breaks.update(module.break_lines)