diff --git a/pym/euscan/__init__.py b/pym/euscan/__init__.py index bb83dc8..01255f4 100644 --- a/pym/euscan/__init__.py +++ b/pym/euscan/__init__.py @@ -3,17 +3,8 @@ # Copyright 2011 Corentin Chary # Distributed under the terms of the GNU General Public License v2 -from io import StringIO -from collections import defaultdict -import json - -from gentoolkit import pprinter as pp -from portage.output import EOutput - - __version__ = "git" - CONFIG = { 'nocolor': False, 'quiet': False, @@ -75,93 +66,5 @@ ROBOTS_TXT_BLACKLIST_DOMAINS = [ '(.*)nodejs.org(.*)', ] - -class EOutputFile(EOutput): - """ - Override of EOutput, allows to specify an output file for writes - """ - def __init__(self, out_file=None, *args, **kwargs): - super(EOutputFile, self).__init__(*args, **kwargs) - self.out_file = out_file - - def _write(self, f, msg): - if self.out_file is None: - super(EOutputFile, self)._write(f, msg) - else: - super(EOutputFile, self)._write(self.out_file, msg) - - -class EuscanOutput(object): - """ - Class that handles output for euscan - """ - def __init__(self, config): - self.config = config - self.queries = defaultdict(dict) - self.current_query = None - - def set_query(self, query): - self.current_query = query - if query is not None: - if not query in self.queries: - self.queries[query] = { - "messages": defaultdict(StringIO), - "result": [], - "metadata": {}, - } - - def get_formatted_output(self): - data = {} - - for query in self.queries: - data[query] = { - "result": self.queries[query]["result"], - "metadata": self.queries[query]["metadata"], - "messages": {} - } - for key in self.queries[query]["messages"]: - if key not in ("ebegin", "eend"): - _msg = self.queries[query]["messages"][key].getvalue() - val = [x for x in _msg.split("\n") if x] - data[query]["messages"][key] = val - - if self.config["format"].lower() == "json": - return json.dumps(data, indent=self.config["indent"]) - else: - raise TypeError("Invalid output format") - - def result(self, cp, version, url, handler, confidence): - from euscan.helpers import get_version_type - - if self.config['format']: - _curr = self.queries[self.current_query] - _curr["result"].append( - {"version": version, "urls": [url], "handler": handler, - "confidence": confidence, "type": get_version_type(version)} - ) - else: - if not self.config['quiet']: - print "Upstream Version:", pp.number("%s" % version), - print pp.path(" %s" % url) - else: - print pp.cpv("%s-%s" % (cp, version)) + ":", pp.path(url) - - def metadata(self, key, value, show=True): - if self.config["format"]: - self.queries[self.current_query]["metadata"][key] = value - elif show: - print "%s: %s" % (key.capitalize(), value) - - def __getattr__(self, key): - if self.config["format"]: - out_file = self.queries[self.current_query]["messages"][key] - - _output = EOutputFile(out_file=out_file, - quiet=self.config['quiet']) - ret = getattr(_output, key) - else: - ret = getattr(EOutputFile(quiet=self.config['quiet']), key) - return ret - - -output = EuscanOutput(CONFIG) +from out import EuscanOutput +output = out.EuscanOutput(CONFIG) diff --git a/pym/euscan/out.py b/pym/euscan/out.py new file mode 100644 index 0000000..850772e --- /dev/null +++ b/pym/euscan/out.py @@ -0,0 +1,89 @@ +from io import StringIO +from collections import defaultdict +import json + +from gentoolkit import pprinter as pp +from portage.output import EOutput + +class EOutputMem(EOutput): + """ + Override of EOutput, allows to specify an output file for writes + """ + def __init__(self, *args, **kwargs): + super(EOutputMem, self).__init__(*args, **kwargs) + self.out = StringIO() + + def getvalue(self): + return self.out.getvalue() + + def _write(self, f, msg): + super(EOutputMem, self)._write(self.out, msg) + +class EuscanOutput(object): + """ + Class that handles output for euscan + """ + def __init__(self, config): + self.config = config + self.queries = defaultdict(dict) + self.current_query = None + + def set_query(self, query): + self.current_query = query + if query is None: + return + + if query in self.queries: + return + + if self.config["format"]: + output = EOutputMem() + else: + output = EOutput() + + self.queries[query] = { + "output": output, + "result": [], + "metadata": {}, + } + + def get_formatted_output(self): + data = {} + + for query in self.queries: + data[query] = { + "result": self.queries[query]["result"], + "metadata": self.queries[query]["metadata"], + "messages": self.queries[query]["output"].getvalue(), + } + + if self.config["format"].lower() == "json": + return json.dumps(data, indent=self.config["indent"]) + else: + raise TypeError("Invalid output format") + + def result(self, cp, version, url, handler, confidence): + from euscan.helpers import get_version_type + + if self.config['format']: + _curr = self.queries[self.current_query] + _curr["result"].append( + {"version": version, "urls": [url], "handler": handler, + "confidence": confidence, "type": get_version_type(version)} + ) + else: + if not self.config['quiet']: + print "Upstream Version:", pp.number("%s" % version), + print pp.path(" %s" % url) + else: + print pp.cpv("%s-%s" % (cp, version)) + ":", pp.path(url) + + def metadata(self, key, value, show=True): + if self.config["format"]: + self.queries[self.current_query]["metadata"][key] = value + elif show: + print "%s: %s" % (key.capitalize(), value) + + def __getattr__(self, key): + output = self.queries[self.current_query]["output"] + return getattr(output, key)