def parse_list(string): list = TemplateParamList() for i, w in enumerate( split_quoted_strings(string, unescape=False) ): if i % 2: if w != ',': raise TemplateSyntaxError(self, string) elif w.startswith('"') or w.startswith("'"): list.append(TemplateLiteral(unescape_quoted_string(w))) else: list.append(TemplateParam(w)) return list
def parse_list(string): list = TemplateParamList() for i, w in enumerate(split_quoted_strings(string, unescape=False)): if i % 2: if w != ',': raise TemplateSyntaxError(self, string) elif w.startswith('"') or w.startswith("'"): list.append(TemplateLiteral(unescape_quoted_string(w))) else: list.append(TemplateParam(w)) return list
def parse_exec(self, args=None): if not (isinstance(args, tuple) and len(args) == 3): raise AssertionError('Custom commands needs 3 arguments') # assert statement could be optimized away notebook, page, pageview = args cmd = split_quoted_strings(self['Desktop Entry']['X-Zim-ExecTool']) if '%f' in cmd: self._tmpfile = TmpFile('tmp-page-source.txt') self._tmpfile.writelines(page.dump('wiki')) cmd[cmd.index('%f')] = self._tmpfile.path if '%d' in cmd: dir = notebook.get_attachments_dir(page) if dir: cmd[cmd.index('%d')] = dir.path else: cmd[cmd.index('%d')] = '' if '%s' in cmd: if hasattr(page, 'source') and isinstance(page.source, File): cmd[cmd.index('%s')] = page.source.path else: cmd[cmd.index('%s')] = '' if '%p' in cmd: cmd[cmd.index('%p')] = page.name if '%n' in cmd: cmd[cmd.index('%n')] = File(notebook.uri).path if '%D' in cmd: dir = notebook.document_root if dir: cmd[cmd.index('%D')] = dir.path else: cmd[cmd.index('%D')] = '' if '%t' in cmd: text = pageview.get_selection() or pageview.get_word() cmd[cmd.index('%t')] = text or '' # FIXME - need to substitute this in arguments + url encoding if '%T' in cmd: text = pageview.get_selection(format='wiki') or pageview.get_word( format='wiki') cmd[cmd.index('%T')] = text or '' # FIXME - need to substitute this in arguments + url encoding return tuple(cmd)
def update(self, E=(), **F): self['Desktop Entry'].update(E, **F) # Set sane default for X-Zim-ShowInContextMenus if not (E and 'X-Zim-ShowInContextMenu' in E) \ and not 'X-Zim-ShowInContextMenu' in F: cmd = split_quoted_strings(self['Desktop Entry']['X-Zim-ExecTool']) if any(c in cmd for c in ['%f', '%d', '%s']): context = 'Page' elif '%t' in cmd: context = 'Text' else: context = None self['Desktop Entry']['X-Zim-ShowInContextMenu'] = context
def update(self, E=None, **F): self['Desktop Entry'].update(E, **F) # Set sane default for X-Zim-ShowInContextMenus if not (E and 'X-Zim-ShowInContextMenu' in E) \ and not 'X-Zim-ShowInContextMenu' in F: cmd = split_quoted_strings(self['Desktop Entry']['X-Zim-ExecTool']) if any(c in cmd for c in ['%f', '%d', '%s']): context = 'Page' elif '%t' in cmd: context = 'Text' else: context = None self['Desktop Entry']['X-Zim-ShowInContextMenu'] = context
def __init__(self, cmd, tryexeccmd=None): '''Constructor @param cmd: the command for the external application, either a string for the command, or a tuple or list with the command and arguments @param tryexeccmd: command to check in L{tryexec()} as string. If C{None} will default to C{cmd} or the first item of C{cmd}. ''' if isinstance(cmd, str): cmd = split_quoted_strings(cmd) else: assert isinstance(cmd, (tuple, list)) assert tryexeccmd is None or isinstance(tryexeccmd, str) self.cmd = tuple(cmd) self.tryexeccmd = tryexeccmd
def __init__(self, cmd, tryexeccmd=None): '''Constructor @param cmd: the command for the external application, either a string for the command, or a tuple or list with the command and arguments @param tryexeccmd: command to check in L{tryexec()} as string. If C{None} will default to C{cmd} or the first item of C{cmd}. ''' if isinstance(cmd, basestring): cmd = split_quoted_strings(cmd) else: assert isinstance(cmd, (tuple, list)) assert tryexeccmd is None or isinstance(tryexeccmd, basestring) self.cmd = tuple(cmd) self.tryexeccmd = tryexeccmd
def parse_exec(self, args=None): if not (isinstance(args, tuple) and len(args) == 3): raise AssertionError, 'Custom commands needs 3 arguments' # assert statement could be optimized away notebook, page, pageview = args cmd = split_quoted_strings(self['Desktop Entry']['X-Zim-ExecTool']) if '%f' in cmd: self._tmpfile = TmpFile('tmp-page-source.txt') self._tmpfile.writelines(page.dump('wiki')) cmd[cmd.index('%f')] = self._tmpfile.path if '%d' in cmd: dir = notebook.get_attachments_dir(page) if dir: cmd[cmd.index('%d')] = dir.path else: cmd[cmd.index('%d')] = '' if '%s' in cmd: if hasattr(page, 'source') and isinstance(page.source, File): cmd[cmd.index('%s')] = page.source.path else: cmd[cmd.index('%s')] = '' if '%n' in cmd: cmd[cmd.index('%n')] = File(notebook.uri).path if '%D' in cmd: dir = notebook.document_root if dir: cmd[cmd.index('%D')] = dir.path else: cmd[cmd.index('%D')] = '' if '%t' in cmd: text = pageview.get_selection() or pageview.get_word() cmd[cmd.index('%t')] = text or '' # FIXME - need to substitute this in arguments + url encoding if '%T' in cmd: text = pageview.get_selection(format='wiki') or pageview.get_word(format='wiki') cmd[cmd.index('%T')] = text or '' # FIXME - need to substitute this in arguments + url encoding return tuple(cmd)
def __init__(self, cmd, tryexeccmd=None, encoding=None): '''Constructor @param cmd: the command for the external application, either a string for the command, or a tuple or list with the command and arguments @param tryexeccmd: command to check in L{tryexec()} as string. If C{None} will default to C{cmd} or the first item of C{cmd}. @param encoding: the encoding to use for commandline args if known, else falls back to system default ''' if isinstance(cmd, basestring): cmd = split_quoted_strings(cmd) else: assert isinstance(cmd, (tuple, list)) assert tryexeccmd is None or isinstance(tryexeccmd, basestring) self.cmd = tuple(cmd) self.tryexeccmd = tryexeccmd self.encoding = encoding or zim.fs.ENCODING if self.encoding == 'mbcs': self.encoding = 'utf-8'
def _parse_query(self, string): words = split_quoted_strings(string, unescape=False) tokens = [] while words: w = words.pop(0) if w.lower() in operators: tokens.append(operators[w.lower()]) elif keyword_re.match(w): keyword = keyword_re[1].lower() term = keyword_re[2] or words.pop(0) term = unescape_quoted_string(term) tokens.append((keyword, term)) else: w = unescape_quoted_string(w) tokens.append(("content", w)) # default keyword # ~ print tokens root = AndGroup() while tokens: token = tokens.pop(0) if token in (OPERATOR_AND, OPERATOR_OR): pass # AND is the default, OR shoulds not appear here, ignore silently elif tokens and tokens[0] == OPERATOR_OR: # collect terms joined by OR assert isinstance(token, tuple) group = [token] while len(tokens) >= 2 and tokens[0] == OPERATOR_OR and isinstance(tokens[1], tuple): tokens.pop(0) group.append(tokens.pop(0)) root.append(OrGroup(group)) else: # simple term in AND group assert isinstance(token, tuple) root.append(token) # ~ print root return root
def _parse_query(self, string): # First do a raw tokenizer words = split_quoted_strings(string, unescape=False, strict=False) tokens = [] while words: if operators_re.match(words[0]): w = operators_re[0] words[0] = words[0][len(w):] else: w = words.pop(0) if w.lower() in operators: tokens.append(operators[w.lower()]) elif keyword_re.match(w): keyword = keyword_re[1].lower() string = keyword_re[2] or words.pop(0) string = unescape_quoted_string(string) if keyword == 'links': keyword = 'linksfrom' tokens.append(QueryTerm(keyword, string)) else: w = unescape_quoted_string(w) if tag_re.match(w): tokens.append(QueryTerm('tag', w[1:])) else: tokens.append(QueryTerm('contentorname', w)) # default keyword #~ print tokens # Then parse NOT operator out tokens, mytokens = [], tokens while mytokens: token = mytokens.pop(0) if token == OPERATOR_NOT: if mytokens and isinstance(mytokens[0], QueryTerm): token = mytokens.pop(0) token.inverse = True tokens.append(token) else: pass # ignore else: tokens.append(token) #~ print tokens # Finally group in AND and OR groups root = QueryGroup(OPERATOR_AND) while tokens: token = tokens.pop(0) if isinstance(token, QueryTerm): if tokens and tokens[0] == OPERATOR_OR: # collect terms joined by OR assert isinstance(token, QueryTerm) group = QueryGroup(OPERATOR_OR) group.append(token) while len(tokens) >= 2 and tokens[0] == OPERATOR_OR \ and isinstance(tokens[1], QueryTerm): tokens.pop(0) group.append(tokens.pop(0)) root.append(group) else: # simple term in AND group root.append(token) else: assert token in (OPERATOR_AND, OPERATOR_OR) pass # AND is the default, OR should not appear here, ignore silently #~ print root return root
def parse_exec(self, args=None): '''Returns a list of command and arguments that can be used to open this application. Args can be either File objects or urls. ''' assert args is None or isinstance(args, (list, tuple)) def uris(args): uris = [] for arg in args: if isinstance(arg, (File, Dir)): uris.append(arg.uri) else: uris.append(unicode(arg)) return uris cmd = split_quoted_strings(self['Desktop Entry']['Exec']) if args is None or len(args) == 0: if '%f' in cmd: cmd.remove('%f') elif '%F' in cmd: cmd.remove('%F') elif '%u' in cmd: cmd.remove('%u') elif '%U' in cmd: cmd.remove('%U') elif '%f' in cmd: assert len(args) == 1, 'application takes one file name' i = cmd.index('%f') cmd[i] = unicode(args[0]) elif '%F' in cmd: i = cmd.index('%F') for arg in reversed(map(unicode, args)): cmd.insert(i, unicode(arg)) cmd.remove('%F') elif '%u' in cmd: assert len(args) == 1, 'application takes one url' i = cmd.index('%u') cmd[i] = uris(args)[0] elif '%U' in cmd: i = cmd.index('%U') for arg in reversed(uris(args)): cmd.insert(i, unicode(arg)) cmd.remove('%U') else: cmd.extend(map(unicode, args)) if '%i' in cmd: if 'Icon' in self['Desktop Entry']: i = cmd.index('%i') cmd[i] = self['Desktop Entry']['Icon'] cmd.insert(i, '--icon') else: cmd.remove('%i') if '%c' in cmd: i = cmd.index('%c') cmd[i] = self.name if '%k' in cmd: i = cmd.index('%k') if hasattr(self, 'file'): cmd[i] = self.file.path else: cmd[i] = '' return tuple(cmd)
def parse_exec(self, args=None): '''Parse the 'Exec' string and interpolate the arguments according to the keys of the desktop spec. @param args: list of either URLs or L{File} objects @returns: the full command to execute as a tuple ''' assert args is None or isinstance(args, (list, tuple)) def uris(args): uris = [] for arg in args: if isinstance(arg, (File, Dir)): uris.append(arg.uri) else: uris.append(unicode(arg)) return uris cmd = split_quoted_strings(self['Desktop Entry']['Exec']) if args is None or len(args) == 0: if '%f' in cmd: cmd.remove('%f') elif '%F' in cmd: cmd.remove('%F') elif '%u' in cmd: cmd.remove('%u') elif '%U' in cmd: cmd.remove('%U') elif '%f' in cmd: assert len(args) == 1, 'application takes one file name' i = cmd.index('%f') cmd[i] = unicode(args[0]) elif '%F' in cmd: i = cmd.index('%F') for arg in reversed(map(unicode, args)): cmd.insert(i, unicode(arg)) cmd.remove('%F') elif '%u' in cmd: assert len(args) == 1, 'application takes one url' i = cmd.index('%u') cmd[i] = uris(args)[0] elif '%U' in cmd: i = cmd.index('%U') for arg in reversed(uris(args)): cmd.insert(i, unicode(arg)) cmd.remove('%U') else: cmd.extend(map(unicode, args)) if '%i' in cmd: if 'Icon' in self['Desktop Entry'] \ and self['Desktop Entry']['Icon']: i = cmd.index('%i') cmd[i] = self['Desktop Entry']['Icon'] cmd.insert(i, '--icon') else: cmd.remove('%i') if '%c' in cmd: i = cmd.index('%c') cmd[i] = self.name if '%k' in cmd: i = cmd.index('%k') if hasattr(self, 'file'): cmd[i] = self.file.path else: cmd[i] = '' return tuple(cmd)
def cmd(self): return split_quoted_strings(self['Desktop Entry']['Exec'])
def parse_exec(self, args=None): '''Parse the 'Exec' string and interpolate the arguments according to the keys of the desktop spec. @param args: list of either URLs or L{File} objects @returns: the full command to execute as a tuple ''' assert args is None or isinstance(args, (list, tuple)) def uris(args): uris = [] for arg in args: if isinstance(arg, (File, Dir)): uris.append(arg.uri) else: uris.append(unicode(arg)) return uris cmd = split_quoted_strings(self['Desktop Entry']['Exec']) if args is None or len(args) == 0: if '%f' in cmd: cmd.remove('%f') elif '%F' in cmd: cmd.remove('%F') elif '%u' in cmd: cmd.remove('%u') elif '%U' in cmd: cmd.remove('%U') elif '%f' in cmd: assert len(args) == 1, 'application takes one file name' i = cmd.index('%f') cmd[i] = unicode(args[0]) elif '%F' in cmd: i = cmd.index('%F') for arg in reversed(map(unicode, args)): cmd.insert(i, unicode(arg)) cmd.remove('%F') elif '%u' in cmd: assert len(args) == 1, 'application takes one url' i = cmd.index('%u') cmd[i] = uris(args)[0] elif '%U' in cmd: i = cmd.index('%U') for arg in reversed(uris(args)): cmd.insert(i, unicode(arg)) cmd.remove('%U') else: cmd.extend(map(unicode, args)) if '%i' in cmd: if 'Icon' in self['Desktop Entry']: i = cmd.index('%i') cmd[i] = self['Desktop Entry']['Icon'] cmd.insert(i, '--icon') else: cmd.remove('%i') if '%c' in cmd: i = cmd.index('%c') cmd[i] = self.name if '%k' in cmd: i = cmd.index('%k') if hasattr(self, 'file'): cmd[i] = self.file.path else: cmd[i] = '' return tuple(cmd)