def add_product_item(product): title = utf8ify(product['name']) from babel.numbers import format_currency cheapest_price = utf8ify( format_currency(float(product['lowestPrice']['amount']), product['lowestPrice']['currency'], locale=get_locale())) subtitle = ' - '.join( filter(None, (cheapest_price, utf8ify( product['categoryName']), get_stars(product)))) largetext = '{} – {}'.format(title, cheapest_price) wf.add_item(title=title, subtitle=subtitle, arg=product['url'], valid=True, largetext=largetext)
def add_suggestion(suggestion): suggestion = utf8ify(suggestion) wf.add_item(title=suggestion, subtitle='Search DuckDuckGo for "{}"'.format(suggestion), arg=suggestion, valid=True, autocomplete=suggestion, largetext=suggestion)
def add_suggestion_item(suggestion): if 'categories' in suggestion: for category in suggestion['categories']: if category['name'] == 'all': title = utf8ify(suggestion['name']) else: title = '{} → {}'.format(utf8ify(category['name']), utf8ify(suggestion['name'])) wf.add_item(title=title, subtitle=suggestion['type'].capitalize(), arg=category['url'], valid=True, largetext=title) else: title = utf8ify(suggestion['name']) wf.add_item(title=title, subtitle=suggestion['type'].capitalize(), arg=suggestion['url'], valid=True, largetext=title)
def test_utf8ify(): """UTF-8 encoding.""" data = [ # input, expected output ("Köln", "Köln"), ("UTF-8", "UTF-8"), (10, "10"), ([1, 2, 3], "[1, 2, 3]"), ] for s, x in data: r = utf8ify(s) assert x == r assert isinstance(x, str)
def test_utf8ify(): """UTF-8 encoding.""" data = [ # input, expected output (u'Köln', 'Köln'), ('UTF-8', 'UTF-8'), (10, '10'), ([1, 2, 3], '[1, 2, 3]'), ] for s, x in data: r = utf8ify(s) assert x == r assert isinstance(x, str)
def find_git_repos(dirpath, excludes, depth, uid, gids, name_for_parent=1): """Return list of directories containing a `.git` file or directory. Results matching globbing patterns in `excludes` will be ignored. `depth` is how many directories deep to search (2 is the minimum in most situations). `name_for_parent` is which level of the directory hierarchy to name the repo after relative to `.git` (1=immediate parent, 2=grandparent) """ def _group(args, primary, operator=None): """Pair each arg with primary, then join pairs with operator.""" out = ['('] for i, arg in enumerate(args): if operator and i > 0 and i < len(args) - 1: out.append(operator) out += [primary, arg] return out + [')'] start = time() cmd = ['find', '-L', dirpath, '-maxdepth', str(depth)] # excludes converted to `find` arguments if excludes: cmd += _group(excludes, '-name', '-o') + ['-prune', '-o'] # ignore unreadable directories # https://unix.stackexchange.com/a/257058 cmd.append('(') # ignore user-owned that we can't open cmd += ['-uid', str(uid), '(', '-perm', '-u=rx', '-o', '-prune', ')'] # ignore group-owned that we can't open cmd += ['-o'] + _group([str(n) for n in gids], '-gid') cmd += ['(', '-perm', '-g=rx', '-o', '-prune', ')'] # ignore other-owned that we can't open cmd += ['-o', '(', '-perm', '-o=rx', '-o', '-prune', ')'] # close "unreadable" group cmd.append(')') cmd += ['-name', '.git', '-print'] cmd = [utf8ify(s) for s in cmd] try: output = subprocess.check_output(cmd) except Exception as err: log.exception('failed: %r', err) raise err output = [ os.path.dirname(s.strip()) for s in decode(output).split('\n') if s.strip() ] results = [] for filepath in output: ignore = False for pattern in excludes: if fnmatch(filepath, pattern): ignore = True break if ignore: continue # Work out name for repo if name_for_parent < 2: # ignore 0, it's pointless name = os.path.basename(filepath) else: components = filepath.rstrip('/').split('/') if name_for_parent >= len(components): log.warning( '%s : `name_for_parent` is %d, but ' 'only %d levels in file tree', filepath, name_for_parent, len(components)) name = os.path.basename(filepath) else: name = components[-(name_for_parent)] results.append(Repo(name, filepath)) log.debug('%d repo(s) found in `%s` in %0.2fs', len(results), dirpath, time() - start) for r in results: log.debug(' %r', r) return results