about summary refs log tree commit diff stats
path: root/apps/ex11.subx
Commit message (Expand)AuthorAgeFilesLines
* 6182 - start of support for safe handlesKartik Agaram2020-04-031-7/+7
* 6089Kartik Agaram2020-03-061-2/+2
* 6014Kartik Agaram2020-02-171-2/+2
* 5924Kartik Agaram2020-01-271-3/+3
* 5897 - rename comparison instructionsKartik Agaram2020-01-161-4/+4
* 5876 - address -> addrKartik Agaram2020-01-031-2/+2
* 5856Kartik Agaram2020-01-011-0/+355
d */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
/*
 * contact.h
 *
 * Copyright (C) 2012, 2013 James Booth <boothj5@gmail.com>
 *
 * This file is part of Profanity.
 *
 * Profanity is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Profanity is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Profanity.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef CONTACT_H
#define CONTACT_H

#include "resource.h"

typedef struct p_contact_t *PContact;

PContact p_contact_new(const char * const barejid, const char * const name,
    const char * const subscription, const char * const offline_message,
    gboolean pending_out);
PContact p_contact_new_subscription(const char * const barejid,
    const char * const subscription, gboolean pending_out);
void p_contact_add_resource(PContact contact, Resource *resource);
gboolean p_contact_remove_resource(PContact contact, const char * const resource);
void p_contact_free(PContact contact);
const char* p_contact_barejid(PContact contact);
const char* p_contact_name(PContact contact);
const char* p_contact_presence(PContact contact);
const char* p_contact_status(PContact contact);
const char* p_contact_subscription(const PContact contact);
GList * p_contact_get_available_resources(const PContact contact);
GDateTime* p_contact_last_activity(const PContact contact);
gboolean p_contact_pending_out(const PContact contact);
void p_contact_set_presence(const PContact contact, Resource *resource);
void p_contact_set_status(const PContact contact, const char * const status);
void p_contact_set_subscription(const PContact contact, const char * const subscription);
void p_contact_set_pending_out(const PContact contact, gboolean pending_out);
void p_contact_set_last_activity(const PContact contact, GDateTime *last_activity);
gboolean p_contact_is_available(const PContact contact);
gboolean p_contact_has_available_resource(const PContact contact);
Resource * p_contact_get_resource(const PContact contact, const char * const resource);

#endif
1) os.ttyname(2) except: return False return True def squash_flags(flags): """ Remove lowercase flags if the respective uppercase flag exists >>> squash_flags('abc') 'abc' >>> squash_flags('abcC') 'ab' >>> squash_flags('CabcAd') 'bd' """ exclude = ''.join(f.upper() + f.lower() for f in flags if f == f.upper()) return ''.join(f for f in flags if f not in exclude) class Rifle(object): delimiter1 = '=' delimiter2 = ',' def hook_before_executing(self, command, mimetype, flags): pass def hook_after_executing(self, command, mimetype, flags): pass def hook_command_preprocessing(self, command): return command def hook_command_postprocessing(self, command): return command def hook_environment(self, env): return env def hook_logger(self, string): sys.stderr.write(string + "\n") def __init__(self, config_file): self.config_file = config_file self._app_flags = '' self._app_label = None def reload_config(self, config_file=None): """Replace the current configuration with the one in config_file""" if config_file is None: config_file = self.config_file f = open(config_file, 'r') self.rules = [] lineno = 1 for line in f: if line.startswith('#') or line == '\n': continue line = line.strip() try: if self.delimiter1 not in line: raise Exception("Line without delimiter") tests, command = line.split(self.delimiter1, 1) tests = tests.split(self.delimiter2) tests = tuple(tuple(f.strip().split(None, 1)) for f in tests) tests = tuple(tests) command = command.strip() self.rules.append((command, tests)) except Exception as e: self.hook_logger("Syntax error in %s line %d (%s)" % \ (config_file, lineno, str(e))) lineno += 1 f.close() def _eval_condition(self, condition, files, label): # Handle the negation of conditions starting with an exclamation mark, # then pass on the arguments to _eval_condition2(). if not condition: return True if condition[0].startswith('!'): new_condition = tuple([condition[0][1:]]) + tuple(condition[1:]) return not self._eval_condition2(new_condition, files, label) return self._eval_condition2(condition, files, label) def _eval_condition2(self, rule, files, label): # This function evaluates the condition, after _eval_condition() handled # negation of conditions starting with a "!". function = rule[0] argument = rule[1] if len(rule) > 1 else '' if not files: return False if function == 'ext': extension = os.path.basename(files[0]).rsplit('.', 1)[-1] return bool(re.search('^' + argument + '$', extension)) if function == 'name': return bool(re.search(argument, os.path.basename(files[0]))) if function == 'path': return bool(re.search(argument, os.path.abspath(files[0]))) if function == 'mime': return bool(re.search(argument, self._get_mimetype(files[0]))) if function == 'has': return argument in get_executables() if function == 'terminal': return _is_terminal() if function == 'label': self._app_label = argument if label: return argument == label return True if function == 'flag': self._app_flags = argument return True if function == 'X': return 'DISPLAY' in os.environ if function == 'else': return True def _get_mimetype(self, fname): # Spawn "file" to determine the mime-type of the given file. if self._mimetype: return self._mimetype mimetype = spawn("file", "--mime-type", "-Lb", fname) self._mimetype = mimetype return mimetype def _build_command(self, files, action, flags): # Get the flags if isinstance(flags, str): self._app_flags += flags self._app_flags = squash_flags(self._app_flags) flags = self._app_flags _filenames = "' '".join(f.replace("'", "'\\\''") for f in files) command = "set -- '%s'" % _filenames + '\n' # Apply flags command += self._apply_flags(action, flags) return command def _apply_flags(self, action, flags): # FIXME: Flags do not work properly when pipes are in the command. if 'r' in flags: action = 'sudo ' + action if 't' in flags: if 'TERMCMD' not in os.environ: term = os.environ['TERM'] if term.startswith('rxvt-unicode'): term = 'urxvt' if term not in get_executables(): self.hook_logger("Can not determine terminal command. " "Please set $TERMCMD manually.") os.environ['TERMCMD'] = term action = "$TERMCMD -e %s" % action if 'f' in flags: action = "nohup %s >& /dev/null &" % action return action def list_commands(self, files, mimetype=None): """ Returns one 4-tuple for all currently applicable commands The 4-tuple contains (count, command, label, flags). count is the index, counted from 0 upwards, command is the command that will be executed. label and flags are the label and flags specified in the rule. """ self._mimetype = mimetype count = 0 result = [] t = time.time() for cmd, tests in self.rules: self._app_flags = '' self._app_label = None for test in tests: if not self._eval_condition(test, files, None): break else: result.append((count, cmd, self._app_label, self._app_flags)) count += 1 return result def execute(self, files, way=0, label=None, flags=None, mimetype=None): """ Executes the given list of files. The default way to run files is 0. Specifying way=N means rifle should execute the Nth command whose conditions match for the given files. If a label is specified, only rules with this label will be considered. Specifying the mimetype will override the mimetype returned by `file`. By specifying a flag, you extend the flag that is defined in the rule. Uppercase flags negate the respective lowercase flags. For example: if the flag in the rule is "pw" and you specify "Pf", then the "p" flag is negated and the "f" flag is added, resulting in "wf". """ self._mimetype = mimetype command = None count = 0 # Determine command for cmd, tests in self.rules: self._app_flags = '' self._app_label = None for test in tests: if not self._eval_condition(test, files, label): break else: if label and label == self._app_label or \ not label and count == way: cmd = self.hook_command_preprocessing(cmd) command = self._build_command(files, cmd, flags) break else: count += 1 # Execute command if command is None: if count <= 0 or way <= 0: self.hook_logger("No action found.") else: self.hook_logger("Method number %d is undefined." % way) else: if 'PAGER' not in os.environ: os.environ['PAGER'] = 'less' command = self.hook_command_postprocessing(command) self.hook_before_executing(command, self._mimetype, self._app_flags) try: p = Popen(command, env=self.hook_environment(os.environ), shell=True) p.wait() finally: self.hook_after_executing(command, self._mimetype, self._app_flags) def main(): """The main function which is run when you start this program direectly.""" import sys # Find configuration file path if 'XDG_CONFIG_HOME' in os.environ and os.environ['XDG_CONFIG_HOME']: conf_path = os.environ['XDG_CONFIG_HOME'] + '/ranger/rifle.conf' else: conf_path = os.path.expanduser('~/.config/ranger/rifle.conf') if not os.path.isfile(conf_path): conf_path = os.path.normpath(os.path.join(os.path.dirname(__file__), '../defaults/rifle.conf')) # Evaluate arguments from optparse import OptionParser parser = OptionParser(usage="%prog [-fhlpw] [files]") parser.add_option('-f', type="string", default=None, metavar="FLAGS", help="use additional flags: f=fork, r=root, t=terminal. " "Uppercase flag negates respective lowercase flags.") parser.add_option('-l', action="store_true", help="list possible ways to open the files (id:label:flags:command)") parser.add_option('-p', type='string', default='0', metavar="KEYWORD", help="pick a method to open the files. KEYWORD is either the " "number listed by 'rifle -l' or a string that matches a label in " "the configuration file") parser.add_option('-w', type='string', default=None, metavar="PROGRAM", help="open the files with PROGRAM") options, positional = parser.parse_args() if not positional: parser.print_help() raise SystemExit(1) if options.p.isdigit(): way = int(options.p) label = None else: way = 0 label = options.p if options.w is not None and not options.l: p = Popen([options.w] + list(positional)) p.wait() else: # Start up rifle rifle = Rifle(conf_path) rifle.reload_config() #print(rifle.list_commands(sys.argv[1:])) if options.l: for count, cmd, label, flags in rifle.list_commands(positional): print("%d:%s:%s:%s" % (count, label or '', flags, cmd)) else: rifle.execute(positional, way=way, label=label, flags=options.f) if __name__ == '__main__': main()