def check(self, data): from pydsl.Interaction.Protocol import protocol_split filename = None if protocol_split(data)["protocol"] == "file": filename = protocol_split(data)["path"] else: import tempfile tmpfile = tempfile.NamedTemporaryFile(delete=False) tmpfile.write(data) filename = tmpfile.name import subprocess proc = subprocess.Popen(["file", "-b", filename], stdout=subprocess.PIPE) lines = proc.stdout.readlines() proc.stdout.close() line = lines[0].decode() return self.regexp.match(line) != None
PARSER.add_argument("-e", "--expression", action="store", dest="expression", help="input expression") ARGS = PARSER.parse_args() import sys if not ARGS.inputfile and not ARGS.expression: PARSER.error("options -i, -u or -e are required") if ARGS.inputfile and ARGS.expression: PARSER.error("options -i and -e can't be together") DEBUGLEVEL = 39 if ARGS.debuglevel: DEBUGLEVEL = ARGS.debuglevel logging.basicConfig(level = DEBUGLEVEL) inputstr = "" from pydsl.Guess import guess, guess_filename if (ARGS.inputfile): from pydsl.Interaction.Protocol import protocol_split pdict = protocol_split(ARGS.inputfile) if pdict["protocol"] == "file": try: with open(pdict["path"], "rb") as f: inputstr = f.read() except IOError: inputstr = "" result = guess(inputstr) result = result.union(guess_filename(ARGS.inputfile)) elif pdict["protocol"] == "http": import urllib.request f = urllib.request.urlopen(ARGS.inputfile); inputstr = f.read() f.close() result = guess(inputstr) else: