From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) by finch.gentoo.org (Postfix) with ESMTP id 8CBD11381F3 for ; Mon, 12 Nov 2012 07:46:33 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 25281E0713; Mon, 12 Nov 2012 07:46:06 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 3C23721C003 for ; Mon, 12 Nov 2012 07:46:05 +0000 (UTC) Received: from hornbill.gentoo.org (hornbill.gentoo.org [94.100.119.163]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 04C0B33D986 for ; Mon, 12 Nov 2012 07:46:04 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by hornbill.gentoo.org (Postfix) with ESMTP id ADC2CE5448 for ; Mon, 12 Nov 2012 07:46:01 +0000 (UTC) From: "Brian Dolbec" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Brian Dolbec" Message-ID: <1352704988.2497e6df850f23e7ae17f01c436a3ee1a2883c31.dol-sen@gentoo> Subject: [gentoo-commits] proj/mirrorselect:master commit in: mirrorselect/ X-VCS-Repository: proj/mirrorselect X-VCS-Files: mirrorselect/main.py mirrorselect/output.py X-VCS-Directories: mirrorselect/ X-VCS-Committer: dol-sen X-VCS-Committer-Name: Brian Dolbec X-VCS-Revision: 2497e6df850f23e7ae17f01c436a3ee1a2883c31 X-VCS-Branch: master Date: Mon, 12 Nov 2012 07:46:01 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Archives-Salt: bd1710a0-e3fb-498e-9963-6d418d9c0a1a X-Archives-Hash: ef278687a163a473cf7cddb1e9191ba1 commit: 2497e6df850f23e7ae17f01c436a3ee1a2883c31 Author: Brian Dolbec gentoo org> AuthorDate: Mon Nov 12 02:01:23 2012 +0000 Commit: Brian Dolbec gmail com> CommitDate: Mon Nov 12 07:23:08 2012 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/mirrorselect.git;a=commit;h=2497e6df move output classes to thier own file. --- mirrorselect/main.py | 110 +++----------------------------------- mirrorselect/output.py | 138 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+), 103 deletions(-) diff --git a/mirrorselect/main.py b/mirrorselect/main.py index 1d01e6d..0f624b7 100755 --- a/mirrorselect/main.py +++ b/mirrorselect/main.py @@ -37,108 +37,13 @@ import time import urllib import urlparse import hashlib -from optparse import IndentedHelpFormatter, OptionParser -from mirrorselect.mirrorparser3 import MirrorParser3, MIRRORS_3_XML, MIRRORS_RSYNC_DATA -import codecs - -class Output(object): - """Handles text output. Only prints messages with level <= verbosity. - Therefore, verbosity=2 is everything (debug), and verbosity=0 is urgent - messages only (quiet).""" - - def __init__(self, verbosity=1, out=sys.stderr): - esc_seq = "\x1b[" - codes = {} - - codes["reset"] = esc_seq + "39;49;00m" - codes["bold"] = esc_seq + "01m" - codes["blue"] = esc_seq + "34;01m" - codes["green"] = esc_seq + "32;01m" - codes["yellow"] = esc_seq + "33;01m" - codes["red"] = esc_seq + "31;01m" - - self.codes = codes - del codes - - self.verbosity = verbosity - self.file = out - - def red(self, text): - return self.codes["red"]+text+self.codes["reset"] - - def green(self, text): - return self.codes["green"]+text+self.codes["reset"] - - def white(self, text): - return self.codes["bold"]+text+self.codes["reset"] - - def blue(self, text): - return self.codes["blue"]+text+self.codes["reset"] - - def yellow(self, text): - return self.codes["yellow"]+text+self.codes["reset"] +from optparse import OptionParser +from mirrorselect.mirrorparser3 import (MirrorParser3, MIRRORS_3_XML, + MIRRORS_RSYNC_DATA) +from mirrorselect.output import output, ColoredFormatter - def print_info(self, message, level=1): - """Prints an info message with a green star, like einfo.""" - if level <= self.verbosity: - self.file.write('\r' + self.green('* ') + message) - self.file.flush() - - def print_warn(self, message, level=1): - """Prints a warning.""" - if level <= self.verbosity: - self.file.write(self.yellow('Warning: ') + message) - self.file.flush() - - def print_err(self, message, level=0): - """prints an error message with a big red ERROR.""" - if level <= self.verbosity: - self.file.write(self.red('\nERROR: ') + message + '\n') - self.file.flush() - sys.exit(1) - - def write(self, message, level=1): - """A wrapper arounf stderr.write, to enforce verbosity settings.""" - if level <= self.verbosity: - self.file.write(message) - self.file.flush() - - -class ColoredFormatter(IndentedHelpFormatter): - - """HelpFormatter with colorful output. - - Extends format_option. - Overrides format_heading. - """ +import codecs - def format_heading(self, heading): - """Return a colorful heading.""" - return "%*s%s:\n" % (self.current_indent, "", output.white(heading)) - - def format_option(self, option): - """Return colorful formatted help for an option.""" - option = IndentedHelpFormatter.format_option(self, option) - # long options with args - option = re.sub( - r"--([a-zA-Z]*)=([a-zA-Z]*)", - lambda m: "-%s %s" % (output.green(m.group(1)), - output.blue(m.group(2))), - option) - # short options with args - option = re.sub( - r"-([a-zA-Z]) ?([0-9A-Z]+)", - lambda m: " -" + output.green(m.group(1)) + ' ' + output.blue(m.group(2)), - option) - # options without args - option = re.sub( - r"-([a-zA-Z\d]+)", lambda m: "-" + output.green(m.group(1)), - option) - return option - - def format_description(self, description): - """Do not wrap.""" - return description + '\n' class Extractor(object): @@ -696,7 +601,7 @@ def get_filesystem_mirrors(out, path, sync=False): try: f = open(path,'r') - except IOError, e: + except IOError: return fsmirrors """ Search for 'var' in make.conf and extract value """ @@ -727,7 +632,7 @@ def get_filesystem_mirrors(out, path, sync=False): break elif key is None: break - except Exception, e: + except Exception: fsmirrors = [] return fsmirrors @@ -858,7 +763,6 @@ def parse_args(argv, config_path): return options -output = Output() #the only FUCKING global. Damnit. def main(argv): """Lets Rock!""" # start with the new location diff --git a/mirrorselect/output.py b/mirrorselect/output.py new file mode 100644 index 0000000..b00226a --- /dev/null +++ b/mirrorselect/output.py @@ -0,0 +1,138 @@ +#!/usr/bin/env python +#-*- coding:utf-8 -*- + +"""Mirrorselect 2.x + Tool for selecting Gentoo source and rsync mirrors. + +Copyright 2005-2012 Gentoo Foundation + + Copyright (C) 2005 Colin Kingsley + Copyright (C) 2008 Zac Medico + Copyright (C) 2009 Sebastian Pipping + Copyright (C) 2009 Christian Ruppert + +Distributed under the terms of the GNU General Public License v2 + This program 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, version 2 of the License. + + This program 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 this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. + +""" + +from __future__ import print_function + +import sys +import re + +from optparse import IndentedHelpFormatter + + +class Output(object): + """Handles text output. Only prints messages with level <= verbosity. + Therefore, verbosity=2 is everything (debug), and verbosity=0 is urgent + messages only (quiet).""" + + def __init__(self, verbosity=1, out=sys.stderr): + esc_seq = "\x1b[" + codes = {} + + codes["reset"] = esc_seq + "39;49;00m" + codes["bold"] = esc_seq + "01m" + codes["blue"] = esc_seq + "34;01m" + codes["green"] = esc_seq + "32;01m" + codes["yellow"] = esc_seq + "33;01m" + codes["red"] = esc_seq + "31;01m" + + self.codes = codes + del codes + + self.verbosity = verbosity + self.file = out + + def red(self, text): + return self.codes["red"]+text+self.codes["reset"] + + def green(self, text): + return self.codes["green"]+text+self.codes["reset"] + + def white(self, text): + return self.codes["bold"]+text+self.codes["reset"] + + def blue(self, text): + return self.codes["blue"]+text+self.codes["reset"] + + def yellow(self, text): + return self.codes["yellow"]+text+self.codes["reset"] + + def print_info(self, message, level=1): + """Prints an info message with a green star, like einfo.""" + if level <= self.verbosity: + self.file.write('\r' + self.green('* ') + message) + self.file.flush() + + def print_warn(self, message, level=1): + """Prints a warning.""" + if level <= self.verbosity: + self.file.write(self.yellow('Warning: ') + message) + self.file.flush() + + def print_err(self, message, level=0): + """prints an error message with a big red ERROR.""" + if level <= self.verbosity: + self.file.write(self.red('\nERROR: ') + message + '\n') + self.file.flush() + sys.exit(1) + + def write(self, message, level=1): + """A wrapper arounf stderr.write, to enforce verbosity settings.""" + if level <= self.verbosity: + self.file.write(message) + self.file.flush() + + +class ColoredFormatter(IndentedHelpFormatter): + + """HelpFormatter with colorful output. + + Extends format_option. + Overrides format_heading. + """ + + def format_heading(self, heading): + """Return a colorful heading.""" + return "%*s%s:\n" % (self.current_indent, "", output.white(heading)) + + def format_option(self, option): + """Return colorful formatted help for an option.""" + option = IndentedHelpFormatter.format_option(self, option) + # long options with args + option = re.sub( + r"--([a-zA-Z]*)=([a-zA-Z]*)", + lambda m: "-%s %s" % (output.green(m.group(1)), + output.blue(m.group(2))), + option) + # short options with args + option = re.sub( + r"-([a-zA-Z]) ?([0-9A-Z]+)", + lambda m: " -" + output.green(m.group(1)) + ' ' + output.blue(m.group(2)), + option) + # options without args + option = re.sub( + r"-([a-zA-Z\d]+)", lambda m: "-" + output.green(m.group(1)), + option) + return option + + def format_description(self, description): + """Do not wrap.""" + return description + '\n' + + +output = Output() #the only FUCKING global. Damnit.