Example #1
0
    def search(self, event, term, distro, arch):
        distro = distro and distro.lower() or self.distro
        arch = arch and arch.lower() or self.arch
        distro = distro + u'-' + arch
        if distro == u'all-all':
            distro = u'all'

        result = json_webservice(
            u'http://dde.debian.net/dde/q/aptfile/byfile/%s/%s' %
            (distro, quote(term)), {'t': 'json'})
        result = result['r']
        if result:
            if isinstance(result[0], list):
                bypkg = map(lambda x: (x[-1], u'/'.join(x[:-1])), result)
                numpackages = len(bypkg)
                packages = defaultdict(list)
                for p, arch in bypkg:
                    packages[p].append(arch)
                packages = map(lambda i: u'%s [%s]' % (i[0], u', '.join(i[1])),
                               packages.iteritems())
            else:
                numpackages = len(result)
                packages = result
            event.addresponse(u'Found %(num)i packages: %(names)s', {
                'num': numpackages,
                'names': human_join(packages),
            })
        else:
            event.addresponse(u'No packages found')
Example #2
0
    def dusk(self, event):
        """Counting of votes and lynching.

        Next state is night.
        """
        self.state = self.dusk
        vote_counts = defaultdict(int)
        for vote in self.votes.values():
            vote_counts[vote] += 1
        self.votes = {}

        victims = []
        victim_votes = 0
        for player, votes in vote_counts.iteritems():
            if votes > victim_votes:
                victims = [player]
                victim_votes = votes
            elif votes == victim_votes:
                victims.append(player)

        if victims:
            if len(victims) > 1:
                event.addresponse(u'The votes are tied. Picking randomly...')
            victim = choice(victims)
            event.addresponse(u'The ballots are in, '
                u'and %(nick)s the %(role)s has been lynched.', {
                    'nick': victim,
                    'role': self.roles[victim],
            })
            self.death(victim)
        else:
            event.addresponse(u'Nobody voted.')

        if not self.endgame(event):
            self.timed_goto(event, 10, self.night)
Example #3
0
    def setup(self):
        iso3166 = join(self.zoneinfo, 'iso3166.tab')
        if exists(iso3166):
            self.countries = {}
            for line in open(iso3166).readlines():
                if not line.startswith('#'):
                    code, name = line.strip().split('\t')
                    self.countries[code] = name

        zones = join(self.zoneinfo, 'zone.tab')
        if exists(zones):
            self.timezones = defaultdict(list)
            for line in open(zones).readlines():
                if not line.startswith('#'):
                    code, coordinates, zone = line.strip().split('\t', 2)
                    if '\t' in zone:
                        zone, comment = zone.split('\t')
                    self.timezones[code].append(zone)

        self.lowerzones = {}
        for path, directories, filenames in walk(self.zoneinfo):
            if path.replace(self.zoneinfo, '').lstrip('/').split('/')[0] not in ('posix', 'right'):
                for filename in filenames:
                    name = join(path, filename).replace(self.zoneinfo, '').lstrip('/')
                    self.lowerzones[name.lower().replace('etc/', '')] = name
Example #4
0
    def remote_tvrage(self, show):
        info_url = 'http://services.tvrage.com/tools/quickinfo.php?%s'

        info = urlopen(info_url % urlencode({'show': show.encode('utf-8')}))

        info = info.read()
        info = info.decode('utf-8')
        if info.startswith('No Show Results Were Found'):
            return
        info = info[5:].splitlines()
        show_info = [i.split('@', 1) for i in info]
        show_dict = dict(show_info)

        #check if there are actual airdates for Latest and Next Episode. None for Next
        #Episode does not neccesarily mean it is nor airing, just the date is unconfirmed.
        show_dict = defaultdict(lambda: 'None', show_info)

        for field in ('Latest Episode', 'Next Episode'):
            if field in show_dict:
                ep, name, date = show_dict[field].split('^', 2)
                count = date.count('/')
                format_from = {0: '%Y', 1: '%b/%Y', 2: '%b/%d/%Y'}[count]
                format_to = ' '.join(('%d', '%B', '%Y')[-1 - count:])
                date = strftime(format_to, strptime(date, format_from))
                show_dict[field] = u'%s - "%s" - %s' % (ep, name, date)

        if 'Genres' in show_dict:
            show_dict['Genres'] = human_join(show_dict['Genres'].split(' | '))

        return show_dict
Example #5
0
    def remote_tvrage(self, show):
        info_url = 'http://services.tvrage.com/tools/quickinfo.php?%s'

        info = urlopen(info_url % urlencode({'show': show.encode('utf-8')}))

        info = info.read()
        info = info.decode('utf-8')
        if info.startswith('No Show Results Were Found'):
            return
        info = info[5:].splitlines()
        show_info = [i.split('@', 1) for i in info]
        show_dict = dict(show_info)

        #check if there are actual airdates for Latest and Next Episode. None for Next
        #Episode does not neccesarily mean it is nor airing, just the date is unconfirmed.
        show_dict = defaultdict(lambda: 'None', show_info)

        for field in ('Latest Episode', 'Next Episode'):
            if field in show_dict:
                ep, name, date = show_dict[field].split('^', 2)
                count = date.count('/')
                format_from = {
                    0: '%Y',
                    1: '%b/%Y',
                    2: '%b/%d/%Y'
                }[count]
                format_to = ' '.join(('%d', '%B', '%Y')[-1 - count:])
                date = strftime(format_to, strptime(date, format_from))
                show_dict[field] = u'%s - "%s" - %s' % (ep, name, date)

        if 'Genres' in show_dict:
            show_dict['Genres'] = human_join(show_dict['Genres'].split(' | '))

        return show_dict
Example #6
0
    def setup(self):
        iso3166 = join(self.zoneinfo, 'iso3166.tab')
        if exists(iso3166):
            self.countries = {}
            for line in open(iso3166).readlines():
                if not line.startswith('#'):
                    code, name = line.strip().split('\t')
                    self.countries[code] = name

        zones = join(self.zoneinfo, 'zone.tab')
        if exists(zones):
            self.timezones = defaultdict(list)
            for line in open(zones).readlines():
                if not line.startswith('#'):
                    code, coordinates, zone = line.strip().split('\t', 2)
                    if '\t' in zone:
                        zone, comment = zone.split('\t')
                    self.timezones[code].append(zone)

        self.lowerzones = {}
        for path, directories, filenames in walk(self.zoneinfo):
            if path.replace(self.zoneinfo, '').lstrip('/').split('/')[0] not in ('posix', 'right'):
                for filename in filenames:
                    name = join(path, filename).replace(self.zoneinfo, '').lstrip('/')
                    self.lowerzones[name.lower().replace('etc/', '')] = name
Example #7
0
    def _load_services(self):
        if self.protocols:
            return

        self.protocols = defaultdict(list)
        self.ports = defaultdict(list)
        f = open(self.filename)
        for line in f.readlines():
            parts = line.split()
            if parts and not parts[0].startswith("#") and parts[0] != "unknown":
                number, transport = parts[1].split("/")
                port = "%s (%s)" % (number, transport.upper())
                self.protocols[parts[0].lower()].append(port)
                self.ports[parts[1]].append(parts[0])
                if not self.nmap:
                    for proto in parts[2:]:
                        if proto.startswith("#"):
                            break
                        self.protocols[proto.lower()].append(port)
Example #8
0
    def _load_services(self):
        if self.protocols:
            return

        self.protocols = defaultdict(list)
        self.ports = defaultdict(list)
        f = open(self.filename)
        for line in f.readlines():
            parts = line.split()
            if parts and not parts[0].startswith('#') and parts[0] != 'unknown':
                number, transport = parts[1].split('/')
                port = '%s (%s)' % (number, transport.upper())
                self.protocols[parts[0].lower()].append(port)
                self.ports[parts[1]].append(parts[0])
                if not self.nmap:
                    for proto in parts[2:]:
                        if proto.startswith('#'):
                            break
                        self.protocols[proto.lower()].append(port)
Example #9
0
    def __init__(self, char):
        self.char = char
        url = 'http://www.unicode.org/cgi-bin/GetUnihanData.pl?'
        params = {'codepoint': self.char.encode('utf8'), 'useuft8': 'true'}
        soup = get_html_parse_tree(url + urlencode(params),
                                   treetype='html5lib-beautifulsoup')

        tables = soup('table', border="1")

        self.data = defaultdict(unicode, ((html_flatten(td).strip()
                                           for td in row('td'))
                                          for table in tables
                                          for row in table('tr')[1:]))
Example #10
0
    def __init__(self, char):
        self.char = char
        url = 'http://www.unicode.org/cgi-bin/GetUnihanData.pl?'
        params = {'codepoint': self.char.encode('utf8'),
                  'useuft8': 'true'}
        soup = get_html_parse_tree(url + urlencode(params),
                                            treetype='html5lib-beautifulsoup')

        tables = soup('table', border="1")

        self.data = defaultdict(unicode,
                                ((html_flatten(td).strip() for td in row('td'))
                                 for table in tables for row in table('tr')[1:]))
Example #11
0
    def currency(self, event, place):
        if not self.currencies:
            self._load_currencies()

        results = defaultdict(list)
        for code, (c_places, name) in self.currencies.iteritems():
            for c_place in c_places:
                if re.search(place, c_place, re.I):
                    results[c_place].append(u'%s (%s)' % (name, code))
                    break

        if results:
            event.addresponse(
                human_join(u'%s uses %s' % (place, human_join(currencies))
                           for place, currencies in results.iteritems()))
        else:
            event.addresponse(u'No currencies found')
Example #12
0
    def currency(self, event, place):
        if not self.currencies:
            self._load_currencies()

        results = defaultdict(list)
        for code, (c_places, name) in self.currencies.iteritems():
            for c_place in c_places:
                if re.search(place, c_place, re.I):
                    results[c_place].append(u'%s (%s)' % (name, code))
                    break

        if results:
            event.addresponse(human_join(
                u'%s uses %s' % (place, human_join(currencies))
                for place, currencies in results.iteritems()
            ))
        else:
            event.addresponse(u'No currencies found')
Example #13
0
    def __init__(self, lines):
        cmds = defaultdict(list)
        for line in lines:
            line = line.lstrip()[:-1]
            if not line:
                break

            line_m = re.match(r"%([A-Z]) (A\d+)(?: (.*))?$", line)
            if line_m:
                cmd, self.catalog_num, info = line_m.groups()
                cmds[cmd].append(info)
            else:
                cmds[cmd][-1] += line

        # %V, %W and %X give signed values if the sequence is signed.
        # Otherwise, only %S, %T and %U are given.
        self.values = "".join(cmds["V"] + cmds["W"] + cmds["X"]) or "".join(cmds["S"] + cmds["T"] + cmds["U"])

        self.name = "".join(cmds["N"])
Example #14
0
    def __init__(self, lines):
        cmds = defaultdict(list)
        for line in lines:
            line = line.lstrip()[:-1]
            if not line:
                break

            line_m = re.match(r'%([A-Z]) (A\d+)(?: (.*))?$', line)
            if line_m:
                cmd, self.catalog_num, info = line_m.groups()
                cmds[cmd].append(info)
            else:
                cmds[cmd][-1] += line

        # %V, %W and %X give signed values if the sequence is signed.
        # Otherwise, only %S, %T and %U are given.
        self.values = (''.join(cmds['V'] + cmds['W'] + cmds['X'])
                       or ''.join(cmds['S'] + cmds['T'] + cmds['U']))

        self.name = ''.join(cmds['N'])
Example #15
0
def _match_sub_selectors(regex):
    selector_patterns = {
        'alpha': r'[a-zA-Z]+',
        'any': r'.+',
        'chunk': r'\S+',
        'digits': r'\d+',
        'number': r'\d*\.?\d+',
        'url': url_regex(),
        'word': r'\w+',
    }

    regex = regex.replace(' ', r'(?:\s+)')

    name_count = defaultdict(int)

    def selector_to_re(match):
        name = match.group(1)
        pattern = match.group(2)

        if name is None:
            return '(%s)' % selector_patterns[pattern]

        # Prevent conflicts when reusing a name
        name_count[name] += 1
        name = '%s__%d_' % (name, name_count[name])

        return '(?P<%s>%s)' % (name, selector_patterns[pattern])

    regex = re.sub(r'{(?:(\w+):)?(%s)}' % '|'.join(selector_patterns.keys()),
                   selector_to_re, regex)

    if not regex.startswith('^'):
        regex = '^' + regex
    if not regex.endswith('$'):
        regex = regex + '$'

    return regex
Example #16
0
    def dusk(self, event):
        """Counting of votes and lynching.

        Next state is night.
        """
        self.state = self.dusk
        vote_counts = defaultdict(int)
        for vote in self.votes.values():
            vote_counts[vote] += 1
        self.votes = {}

        victims = []
        victim_votes = 0
        for player, votes in vote_counts.iteritems():
            if votes > victim_votes:
                victims = [player]
                victim_votes = votes
            elif votes == victim_votes:
                victims.append(player)

        if victims:
            if len(victims) > 1:
                event.addresponse(u'The votes are tied. Picking randomly...')
            victim = choice(victims)
            event.addresponse(
                u'The ballots are in, '
                u'and %(nick)s the %(role)s has been lynched.', {
                    'nick': victim,
                    'role': self.roles[victim],
                })
            self.death(victim)
        else:
            event.addresponse(u'Nobody voted.')

        if not self.endgame(event):
            self.timed_goto(event, 10, self.night)
Example #17
0
def _match_sub_selectors(regex):
    selector_patterns = {
        'alpha'   : r'[a-zA-Z]+',
        'any'     : r'.+',
        'chunk'   : r'\S+',
        'digits'  : r'\d+',
        'number'  : r'\d*\.?\d+',
        'url'     : url_regex(),
        'word'    : r'\w+',
    }

    regex = regex.replace(' ', r'(?:\s+)')

    name_count = defaultdict(int)
    def selector_to_re(match):
        name    = match.group(1)
        pattern = match.group(2)

        if name is None:
            return '(%s)' % selector_patterns[pattern]

        # Prevent conflicts when reusing a name
        name_count[name] += 1
        name = '%s__%d_' % (name, name_count[name])

        return '(?P<%s>%s)' % (name, selector_patterns[pattern])

    regex = re.sub(r'{(?:(\w+):)?(%s)}' % '|'.join(selector_patterns.keys()),
                   selector_to_re, regex)

    if not regex.startswith('^'):
        regex = '^' + regex
    if not regex.endswith('$'):
        regex = regex + '$'

    return regex
Example #18
0
        return self._dict.__repr__()

sources = InsensitiveDict()
config = {}
dispatcher = None
processors = []
categories = {}
reloader = None
databases = {}
auth = None
service = None
options = {
        'base': '.',
}
rpc = {}
channels = defaultdict(lambda: defaultdict(MultiSet))

def twisted_log(eventDict):
    log = logging.getLogger('twisted')
    if 'failure' in eventDict:
        log.error(eventDict.get('why') or 'Unhandled exception' + '\n' + str(eventDict['failure'].getTraceback()))
    elif 'warning' in eventDict:
        log.warning(eventDict['warning'])
    else:
        log.debug(' '.join([str(m) for m in eventDict['message']]))

def setup(opts, service=None):
    service = service
    for key, value in opts.items():
        options[key] = value
    options['base'] = dirname(options['config'])
Example #19
0

sources = InsensitiveDict()
config = {}
dispatcher = None
processors = []
categories = {}
reloader = None
databases = {}
auth = None
service = None
options = {
    'base': '.',
}
rpc = {}
channels = defaultdict(lambda: defaultdict(MultiSet))


def twisted_log(eventDict):
    log = logging.getLogger('twisted')
    if 'failure' in eventDict:
        log.error(
            eventDict.get('why') or 'Unhandled exception' + '\n' +
            str(eventDict['failure'].getTraceback()))
    elif 'warning' in eventDict:
        log.warning(eventDict['warning'])
    else:
        log.debug(' '.join([str(m) for m in eventDict['message']]))


def setup(opts, service=None):
Example #20
0
    formatted = ' and '.join(parts)
    return formatted.replace(' and ', ', ', len(parts) - 2)


def decode_htmlentities(text):
    replace = lambda match: unichr(int(match.group(1)))
    text = re.sub("&#(\d+);", replace, text)

    replace = lambda match: match.group(1) in name2codepoint and unichr(
        name2codepoint[match.group(1)]) or match.group(0)
    text = re.sub("&(\w+);", replace, text)
    return text


downloads_in_progress = defaultdict(Lock)


def cacheable_download(url, cachefile, headers={}, timeout=60):
    """Download url to cachefile if it's modified since cachefile.
    Specify cachefile in the form pluginname/cachefile.
    Returns complete path to downloaded file."""

    downloads_in_progress[cachefile].acquire()
    try:
        f = _cacheable_download(url, cachefile, headers, timeout)
    finally:
        downloads_in_progress[cachefile].release()

    return f
Example #21
0
            parts.append('%s %s%s' % (value, unit, value != 1 and 's' or ''))
            if units and len(parts) >= units:
                break

    formatted =  ' and '.join(parts)
    return formatted.replace(' and ', ', ', len(parts)-2)

def decode_htmlentities(text):
    replace = lambda match: unichr(int(match.group(1)))
    text = re.sub("&#(\d+);", replace, text)

    replace = lambda match: match.group(1) in name2codepoint and unichr(name2codepoint[match.group(1)]) or match.group(0)
    text = re.sub("&(\w+);", replace, text)
    return text

downloads_in_progress = defaultdict(Lock)
def cacheable_download(url, cachefile, headers={}, timeout=60):
    """Download url to cachefile if it's modified since cachefile.
    Specify cachefile in the form pluginname/cachefile.
    Returns complete path to downloaded file."""

    downloads_in_progress[cachefile].acquire()
    try:
        f = _cacheable_download(url, cachefile, headers, timeout)
    finally:
        downloads_in_progress[cachefile].release()

    return f

def _cacheable_download(url, cachefile, headers={}, timeout=60):
    # We do allow absolute paths, for people who know what they are doing,