def process_text(self, text): "Process the text building scanners" # 1. Start with the first line of text scanner_text = [text[0]] assert scanner_text[0].startswith(SCANNER_HEAD) # 2. Loop for the rest of the text for line in text[1:]: # 3. If this the start of a new scanner? if line.startswith(SCANNER_HEAD): # 4. Build a scanner and add it to the list scnr = scanner.Scanner(text=scanner_text, part2=self.part2) self.scnrs.append(scnr) # 5. Start a new scanner text scanner_text = [line] # 6. Not new scanner, add line to existing text else: scanner_text.append(line) # 7. One last scanner scnr = scanner.Scanner(text=scanner_text, part2=self.part2) self.scnrs.append(scnr)
def __init__(self, context): super().__init__() self.context = context self.logger = logging.getLogger(utils.logger_str(__class__) \ + " " + context) # self.logger.setLevel(logging.INFO) self.config = config.Config.instance() self.copies = int(self.config.get(self.context, "copies", 2)) self.path = config.path_for(self.config.get(self.context, "source")) self.scanner = scanner.Scanner(self.context, self.path) lazy_write = utils.str_to_duration( self.config.get(context, "LAZY WRITE", 5)) # TODO: support expiration self.rescan = utils.str_to_duration( self.config.get(self.context, "rescan")) self.clients = persistent_dict.PersistentDict( f"/tmp/cb.s{context}.json.bz2", lazy_write=lazy_write, cls=lock.Lock, expiry=self.rescan) self.drains = elapsed.ExpiringDict(300) # NOT persistent! self.locks = locker.Locker(5) # TODO: timers should relate to a configurable cycle time self.bailout = False self.stats = {'claims': 0, 'drops': 0} self.handling = False
def compile(self, program_file, debug_mode): scan = scanner.Scanner() tokenstream = scan.read_program(program_file) if debug_mode == True: # prints language tokens print("Language tokens : " + str(tokenstream)) if tokenstream != False: token_head = scan.create_tokens(tokenstream) syntax_tree = SyntaxTree(token.MAGICCODE) parser = Parser(token_head) parser.statement(syntax_tree.insertSubtree("STATEMENT")) if debug_mode == True: # prints syntax tree generated by parser syntax_tree.printSyntaxTree(0) vm_code = Semantic(syntax_tree).generate() vm = magic_machine(vm_code, debug_mode) vm.run() print("Result : " + str(vm.get_symbol_table()))
def test_empty_init(self): "Test the default Scanner creation" # 1. Create default Scanner object myobj = scanner.Scanner() # 2. Make sure it has the default values self.assertEqual(myobj.part2, False) self.assertEqual(myobj.text, None) self.assertEqual(myobj.number, 0) self.assertEqual(myobj.bcns, set()) self.assertEqual(myobj.orientations, []) self.assertEqual(myobj.coordinates, None) self.assertEqual(myobj.adjusted, set()) # 3. Check methods self.assertEqual(myobj.coord_diff((0, 2, 0), (-5, 0, 0)), (5, 2, 0)) self.assertEqual(myobj.coord_diff((3, 3, 0), (-2, 1, 0)), (5, 2, 0)) self.assertEqual(myobj.coord_diff((4, 1, 0), (-1, -1, 0)), (5, 2, 0)) self.assertEqual(myobj.coord_diff((-618, -824, -621), (686, 422, 578)), (-1304, -1246, -1199)) self.assertEqual(myobj.coord_diff((-537, -823, -458), (605, 423, 415)), (-1142, -1246, -873)) self.assertEqual(myobj.coord_diff((-447, -329, 318), (515, 917, -361)), (-962, -1246, 679))
def run(self, source): scanner2 = scanner.Scanner(source) tokens = scanner2.scanTokens() for token in tokens: print(token)
def main(): while True: print "\nPython Calculator: " line = list() while True: user_input = raw_input("Enter Expression: ") user_input = user_input.replace(' ', '') if user_input is not None and len(user_input.strip()) == 0: break user_input += " " line.append(user_input) break expr = line if len(expr) == 0: break try: scan = scanner.Scanner() tokens = list() tokens = scan.parseExpression(expr[0]) printTokens(tokens) parsers = Parsers() expressionTree = parsers.parse(tokens) evaluator = evaluators.Evaluators() result = evaluator.evaluate(expressionTree) print '\nResult:', print result except CalcExceptions, x: print 'Error!:', print x.message
def __init__(self, parent=None, address=None, port=None): super(PiScanClient, self).__init__(parent) common.setInstance(self) ui_file = 'scan_client.ui' ui_file = QFile(ui_file) ui_file.open(QFile.ReadOnly) loader = QUiLoader() self.window = loader.load(ui_file) ui_file.close() self.parentWindow = parent #layout = QGridLayout(self) #layout.setContentsMargins(0, 0, 0, 0) #layout.addWidget(self.window) self.dataReceived.connect(self.handleReceived) self.connectDialog = connect.ConnectDialog(self.window, address, port) self.scanner = scanner.Scanner(self.window) self.dialogs = dialogs.Dialogs(self.window) self.contextStack = self.window.findChild(QStackedWidget, 'contextStack') #self.setWindowMode(common.WindowMode.CONNECT) self.showConnectDialog() #self.setWindowMode(common.WindowMode.SCANNER) #self.window.show() self.inmsg = messages_pb2.ServerToClient()
def test_scanner_get_html(self, mockCompressedStream): with patch.object(scanner.Scanner, 'create_http_request') as mockURLLIB3: testScanner = scanner.Scanner() testScanner.set("url", self.testURL) html = testScanner.get_html() self.assertTrue(True if "<html" in str(html) else False)
def main(): #filename = input("Escriba el nombre del archivo (sin extension): ") filename = "Pruebas.txt" if not os.path.exists(filename): print("El archivo no existe") return 0 f = open(filename, "r") line = f.read( ) #Extrae un string que contiene todos los caracteres del archivo print("") print("") print("SALIDA DEL COMPILADOR") print("") resultadoScanner = scanner.Scanner(line) resultadoScanner.scanTokens() token = 0 error = False if resultadoScanner.tokenError != []: error = True for error in resultadoScanner.tokenError: print(error) if not error: #Imprimir tokens while token < len(resultadoScanner.tokenlst) and error == False: resultadoScanner.tokenlst[token].printToken() token += 1
def run(self, source): scanner = scn.Scanner(self, source) tokens = scanner.scan_tokens() parser = prs.Parser(self, tokens) expression = parser.parse() self.interpreter.interpret(expression)
def test_part_one(self): "Test part one example of Scanner object" # 1. Create Scanner object from text myobj = scanner.Scanner(text=aoc_04.from_text(PART_ONE_TEXT)) # 2. Check the part one result self.assertEqual(myobj.part_one(verbose=False), PART_ONE_RESULT)
def scan_only(self): self.logger.info(f"Only scanning {self.context}:{self.path}") ignorals = self.config.get_ignorals(self.context) scn = scanner.Scanner(self.path, ignorals, self.states_filename) scn.scan() self.push() self.get_status(brief=True) self.logger.info(f"Finished: {self.context}:{self.path}")
def test_turbo(self): s = scanner.Scanner("test_scanner", "/tmp/scanner-test") s.scan(turbo=True) print(s.items()) self.assertEquals(s['./one.zero']['size'], 10240) self.assertEquals(s['./one.zero']['checksum'], "deferred") s.scan() self.assertNotEquals(s['./one.zero']['checksum'], "deferred")
def checkSymLinks(config): """ Scans Local directory which contain symlinks """ log.info("Scanning directory {} for symlinks") scandir = scanner.Scanner() for file in scandir.scanDirectory(config.LOCAL_DIR): stats = os.path.stat(file, follow_symlink=False):
def test_part_two(self): "Test part two example of Scanner object" # 1. Create Scanner object from text myobj = scanner.Scanner(part2=True, text=aoc_04.from_text(PART_TWO_TEXT)) # 2. Check the part two result self.assertEqual(myobj.part_two(verbose=False), PART_TWO_RESULT)
def run(line): scan = scanner.Scanner(line) tokens = scan.scan_tokens() parser = lox_parser.Parser(tokens) statements = parser.parse() if config.hadError: return interpreter.interpret(statements)
def test_text_init(self): "Test the Scanner object creation from text" # 1. Create Scanner object from text myobj = scanner.Scanner(text=aoc_04.from_text(EXAMPLE_TEXT)) # 2. Make sure it has the expected values self.assertEqual(myobj.part2, False) self.assertEqual(len(myobj.text), 4) self.assertEqual(len(myobj.passports), 4)
def get_items_from_file(file_name): with open(file_name) as f: data = f.readlines() import scanner scanner = scanner.Scanner(data) data = scanner.group() data = merge_groups(data) return data
def test_empty_init(self): "Test the default Scanner creation" # 1. Create default Scanner object myobj = scanner.Scanner() # 2. Make sure it has the default values self.assertEqual(myobj.part2, False) self.assertEqual(myobj.text, None) self.assertEqual(len(myobj.passports), 0)
def main(path): try: with open(path, 'rb') as content_file: content = content_file.read() except IOError: print("Error: File not found.") return 1 _scanner = scanner.Scanner(content) _parser = parser.Parser(_scanner) print(_symbol_table) return 0
def test_qps(self): import elapsed timer = elapsed.ElapsedTimer() dir = "/users/austind/src/cb/test/source1" s = scanner.Scanner("test", dir, checksums=False) print("starting QPS test") s.scan(turbo=True) print(f"So far: {timer.elapsed():5.2f}s") s.scan() print(f"Total: {timer.elapsed():5.2f}s") os.remove(f"{dir}/.cb.test.json.bz2")
def main(): try: scan = scanner.Scanner() scan.findLocals() scan.scanUrl('lightron.org') scan.scanUrl('www.desentupidorarolabosta.com.br') scan.printall() except KeyboardInterrupt: print "Exit" sys.exit()
def test_drop(self): s = scanner.Scanner("test_scanner", "/tmp/scanner-test") s.scan() print(s.items()) self.assertEquals(s["./one.zero"]["size"], 10240) self.assertEquals(s["directory/three.zero"]["size"], 10240) self.assertTrue(os.path.exists("/tmp/scanner-test/one.zero")) self.assertEquals(s.consumption(), 30720) s.drop("./one.zero") self.assertFalse(os.path.exists("/tmp/scanner-test/one.zero")) self.assertEquals(s.consumption(), 20480)
def build_sources(self): source_contexts = self.config.get_contexts_for_key("source") self.prune_sources(source_contexts) for source_context, source in source_contexts.items(): self.sources[source_context] = config.host_for(source) path = f"{self.path}/{source_context}" self.scanners[source_context] = \ scanner.Scanner(source_context, path, name=f"{self.context}:{source_context}") self.random_source_list.append(source_context) random.shuffle(self.random_source_list)
def test_config(self): cfg = config.Config.instance() cfg.init("test-config.txt", "source", "backup", hostname="localhost") contexts = list(cfg.get_contexts_for_key("source").keys()) print(contexts) context = contexts[1] path = config.path_for(cfg.get(context, "source")) print(path) s = scanner.Scanner(context, path) s.scan() filename = list(s.keys())[0] self.assertTrue(os.path.exists(f"{path}/{filename}"))
def proc_sinais(): while True: from pygame import mixer import scanner #mixer.init() #mixer.music.load('F:\car_alarm_short.mp3') sample_rate = 2.048e6 gain = 49.6 scn = scanner.Scanner(sample_rate, gain) center_freq = 433.9e6 thresh = 10 scn.start_monitor_psd(center_freq) #print('apertado!') #for i in scanner.lista: # print('{}\n'.format(i)) if scanner.flag[0] == 1: for i in range(len(scanner.lista)): if i % 2 == 0: if scanner.lista[i] < 0.5: simbolos = simbolos + '0' #if (i==0) and (scanner.lista[i]<0.5): # simbolos=simbolos+'0' #elif (scanner.lista[i-1]>0.05) and (scanner.lista[i]<0.5): # simbolos=simbolos+'0' else: simbolos = simbolos + '1' #print('simbolos é: '+simbolos) try: x = morse[simbolos] #print(morse[simbolos], end='') except: x = 'erro!' #print('erro!') #for i in range(int(len(simbolos)/3)): # if simbolos[3*i:3*i+3]=='000': # print('s') # elif simbolos[3*i:3*i+3] =='111': # print('o') # else: # print('deu merda') scanner.lista = [] simbolos = '' scanner.flag[0] = 0 return x
def long_pull(self, callback, user_data=None, addr=None, parent=None): content_type = self._response.headers.get('Content-Type') match = re.search('boundary=([0-9A-Fa-f]+)', content_type) if not match: raise LongPullException('Cannot find boundary in content-type') boundary = '--' + match.group(1) boundary_pat = re.compile('^' + boundary) header = True length = len(boundary) document = scanner.Scanner('') for chunk in self._response.iter_content(chunk_size=None): document.string += chunk while document.rest_len() >= length: if header: if not document.check(boundary_pat): print "Framing error!" raise LongPullException('Framing error') head = document.scan_until('\r\n\r\n') mime_headers = head.split('\r\n') values = dict([(v.lower().split(':')) for v in mime_headers[1:] if v.find(':') > 0]) header = False try: length = int(values['content-length']) except ValueError: raise LongPullException( 'Cannot get length from mime header: ' + mime_headers) else: rest = document.rest() body = rest[:length] document.reset() document.string = rest[length:] callback(self._parent, body, self._addr) length = len(boundary) header = True if not self._response: break if not self._response: break
def test_text_init(self): "Test the Scanner object creation from text" # 1. Create Scanner object from text myobj = scanner.Scanner(text=aoc_19.from_text(EXAMPLE_ZERO)) # 2. Make sure it has the expected values self.assertEqual(myobj.part2, False) self.assertEqual(len(myobj.text), 26) self.assertEqual(myobj.number, 0) self.assertEqual(len(myobj.bcns), 25) self.assertEqual(len(myobj.orientations), 24) self.assertEqual(len(myobj.orientations[0]), 25) self.assertEqual(myobj.coordinates, None) self.assertEqual(myobj.adjusted, set()) # 3. Check methods one = scanner.Scanner(text=aoc_19.from_text(EXAMPLE_ONE)) myobj.coordinates = (0, 0, 0) self.assertEqual(len(one.find_matches(myobj.bcns)), 25) self.assertEqual(one.coordinates, (68, -1246, -43)) self.assertEqual(myobj.manhattan_distance(one), 1357)
def long_pull(self, callback, user_data=None): fileno = self._response.fileno() self._socket = socket.fromfd(fileno, socket.AF_INET, socket.SOCK_STREAM) self._socket.setblocking(False) content_type = dict(self._response.getheaders())['content-type'] match = re.search('boundary=([0-9A-Fa-f]+)', content_type) if not match: raise LongPullException('Cannot find boundary in content-type') boundary = '--' + match.group(1) boundary_pat = re.compile('^' + boundary) header = True length = len(boundary) document = scanner.Scanner('') while True: chunk = self._read_chunk() document.string += chunk while document.rest_len() >= length: if header: if not document.check(boundary_pat): print "Framing error!" raise LongPullException('Framing error') head = document.scan_until('\r\n\r\n') mime_headers = head.split('\r\n') values = dict([(v.lower().split(':')) for v in mime_headers[1:] if v.find(':') > 0]) header = False try: length = int(values['content-length']) except ValueError: raise LongPullException( 'Cannot get length from mime header: ' + mime_headers) else: rest = document.rest() body = rest[:length] document.reset() document.string = rest[length:] callback(body) length = len(boundary) header = True
def part_two(args, input_lines): "Process part two of the puzzle" # 1. Create the puzzle solver solver = scanner.Scanner(part2=True, text=input_lines) # 2. Determine the solution for part two solution = solver.part_two(verbose=args.verbose, limit=args.limit) if solution is None: print("There is no solution") else: print("The solution for part two is %s" % (solution)) # 3. Return result return solution is not None