def _add_hostname_to_hosts(): """Adds the hostname to /etc/hosts, if it is not being assigned by DNS -- required for rabbitmq """ hostname_file = open('/etc/hostname','r') hostname = hostname_file.readline().rstrip() private_ip = _get_self_private_ip() host_found = _check_name_resolves(hostname) if not host_found: stdout = TemporaryFile() stderr = TemporaryFile() err = subprocess.call( ('/usr/bin/sudo','/usr/bin/perl', '-i.orig', '-n','-e', r"""BEGIN {($h,$ip)=@ARGV; $ip='127.0.1.1' unless $ip } next if/\Q$h\E/; s/^(127\.0\.0\.1\s+localhost)$/$1\n$ip $h/; print""", hostname, private_ip, '/etc/hosts'), stdout=stdout, stderr=stderr) if err: stdout.seek(0) out='\n'.join(stdout.readlines()) stderr.seek(0) err_text='\n'.join(stderr.readlines()) raise OSError('Error updating /etc/hosts. Result code: {0}\n{1}\n{2}'.format( err,out,err_text )) stdout.close() stderr.close()
class stdIOCollector(collector.collectorBase.Collector): def __init__(self, stdout="", stderr=""): super().__init__(name='stdIO') def __del__(self): if self.stdoutLogger is not None: self.stdoutLogger.close() if self.stderrLogger is not None: self.stderrLogger.close() def prepare(self, conf: dict) -> dict: self.stdoutLogger = TemporaryFile('w+t') self.stderrLogger = TemporaryFile('w+t') logger = {'stdout': self.stdoutLogger, 'stderr': self.stderrLogger} return logger def start(self): super().start() def stop(self): super().stop() def getData(self): self.stdoutLogger.flush() self.stdoutLogger.seek(0) self.stderrLogger.flush() self.stderrLogger.seek(0) return self.stdoutLogger.readlines() + self.stderrLogger.readlines()
def handle_request(request, object_id, size=None): book = get_object_or_404(Book, pk=object_id) if size is None: size = request.REQUEST.get('size',None) handle_number = book.identifier.split(':')[2].replace('/', '_') file_name_complete = 'var/media/' + handle_number + '_' + size + '.pdf' file_name_inprogress = 'var/media/' + handle_number + '_' + size if os.path.exists(file_name_complete): print "found complete PDF" PDF_file = open(file_name_complete, 'r') return HttpResponse(File(PDF_file).readlines(), mimetype='application/pdf') elif os.path.exists(file_name_inprogress): print "found PDF in progress" tempfile = TemporaryFile() tempfile.write('PDF compilation in progress, please check back later...') tempfile.seek(0); return HttpResponse(tempfile.readlines(), mimetype='text/plain') else: # Fire the missiles f = open(file_name_inprogress, 'w') PDF_file = File(f) t = threading.Thread(target=compile_PDF, args=[request, book, PDF_file, size]) t.setDaemon(True) t.start() tempfile = TemporaryFile() tempfile.write('PDF compilation initiated, please check back later...') tempfile.seek(0); return HttpResponse(tempfile.readlines(), mimetype='text/plain')
def handle_request(request, object_id, size=None): book = get_object_or_404(Book, pk=object_id) if size is None: size = request.REQUEST.get('size',None) handle_number = book.identifier.split(':')[2].replace('/', '_') file_name_complete = settings.MEDIA_ROOT + '/' + handle_number + '_' + size + '.pdf' file_name_inprogress = settings.MEDIA_ROOT + '/' + handle_number + '_' + size if os.path.exists(file_name_complete): #logger.error("found complete PDF") PDF_file = open(file_name_complete, 'r') return HttpResponse(File(PDF_file).readlines(), mimetype='application/pdf') elif os.path.exists(file_name_inprogress): #logger.error("found PDF in progress") tempfile = TemporaryFile() tempfile.write('PDF compilation in progress, please check back later...') tempfile.seek(0); return HttpResponse(tempfile.readlines(), mimetype='text/plain') else: # Fire the missiles #logger.error("generating fresh PDF") f = open(file_name_inprogress, 'w') PDF_file = File(f) t = threading.Thread(target=compile_PDF, args=[request, book, PDF_file, size]) t.setDaemon(True) t.start() tempfile = TemporaryFile() tempfile.write('PDF compilation initiated, please check back later...') tempfile.seek(0); return HttpResponse(tempfile.readlines(), mimetype='text/plain')
def test_id_overflow(): # Create an atom array >= 100k atoms length = 100000 a = struc.AtomArray(length) a.coord = np.zeros(a.coord.shape) a.chain_id = np.full(length, "A") # Create residue IDs over 10000 a.res_id = np.arange(1, length + 1) a.res_name = np.full(length, "GLY") a.hetero = np.full(length, False) a.atom_name = np.full(length, "CA") a.element = np.full(length, "C") # Write stack to pdb file and make sure a warning is thrown with pytest.warns(UserWarning): temp = TemporaryFile("w+") pdb_file = pdb.PDBFile() pdb_file.set_structure(a) pdb_file.write(temp) # Assert file can be read properly temp.seek(0) a2 = pdb.get_structure(pdb.PDBFile.read(temp)) assert (a2.array_length() == a.array_length()) # Manually check if the written atom id is correct temp.seek(0) last_line = temp.readlines()[-1] atom_id = int(last_line.split()[1]) assert (atom_id == 1) temp.close() # Write stack as hybrid-36 pdb file: no warning should be thrown with pytest.warns(None) as record: temp = TemporaryFile("w+") tmp_pdb_file = pdb.PDBFile() tmp_pdb_file.set_structure(a, hybrid36=True) tmp_pdb_file.write(temp) assert len(record) == 0 # Manually check if the output is written as correct hybrid-36 temp.seek(0) last_line = temp.readlines()[-1] atom_id = last_line.split()[1] assert (atom_id == "A0000") res_id = last_line.split()[4][1:] assert (res_id == "BXG0") temp.close()
def check_commit_id(path): cur_dir = os.getcwd() commit = None stderr = TemporaryFile() try: os.chdir(path) git_cmd = 'git log -1 --format="%H" | cut -c1-32' git_out = Popen(git_cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=stderr, close_fds=True) errmsg = stderr.readlines() if errmsg: logging.debug("git error message (in %s): %s" % (path, '\n'.join(errmsg))) try: commit = git_out.stdout.readlines()[0].strip() except IndexError: pass finally: os.chdir(cur_dir) stderr.close() return commit
def local_ds_files(ds): """ Gets the list of files corresponding to a published dataset stored on cms_dbs_ph_analysis_02. Args: ds: the path to the published dataset, ending in /USER Returns: A list of the LFN-s of the dataset. """ tf = TemporaryFile() stdout = sys.stdout stdout.flush() sys.stdout = tf print "Query" ret = call_das_cli('--query=file dataset=%s instance=cms_dbs_ph_analysis_02' % ds, '--limit=0') print ret tf.flush() tf.seek(0) sys.stdout = stdout fl = [] for li in tf.readlines(): if "/store/" in li: fl.append(li.strip()) tf.close() return fl
def check_revision_number(path): cur_dir = os.getcwd() revision = None stderr = TemporaryFile() try: os.chdir(path) svn_cmd = "svn info 2>/dev/null | awk '{if(NR == 5) {print $2}}'" svn_out = Popen(svn_cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=stderr, close_fds=True) errmsg = stderr.readlines() if errmsg: logging.debug("svn error message (in %s): %s" % (path, '\n'.join(errmsg))) try: revision = svn_out.stdout.readlines()[0].strip() except IndexError: pass finally: os.chdir(cur_dir) stderr.close() return revision
def _process_data_1(self, cr, uid, ids, data, context): #try: fileobj = TemporaryFile('w+') fileobj.write(data) fileobj.seek(0) lines = [] for line in fileobj.readlines(): #log.info('++++++++++++++++\r\nline=%s' % line) lines = line.split(',') #if len(lines) == 0: break if self._isnumeric(lines[0]) == True: id = int(lines[0]) date_from = datetime.strptime(lines[1], '%m/%d/%Y %H:%M:%S').strftime('%Y-%m-%d %H:%M:%S') date_to = datetime.strptime(lines[2].replace("\n",""), '%m/%d/%Y %H:%M:%S').strftime('%Y-%m-%d %H:%M:%S') #log.info('id=%s,df=%s,dt=%s' % (id, date_from, date_to)) #check existing day = datetime.strptime(date_from, '%Y-%m-%d %H:%M:%S').strftime('%Y-%m-%d') attds = self.pool.get('hr.attendance') attd_ids = attds.search(cr, uid, [('employee_id','=',id),('day','=',day)], context=context) #log.info(attd_ids) log.info('employee_id=%d,attd_ids=%s,len=%d,day=%s' % (id,attd_ids,len(attd_ids), day)) if len(attd_ids) == 0: attds.create(cr, uid, {'employee_id':id,'name':date_from,'action':'sign_in','source':'import'}) attds.create(cr, uid, {'employee_id':id,'name':date_to,'action':'sign_out','source':'import'}) #log.info('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++') fileobj.close()
class BuildTests(unittest.TestCase): def setUp(self): self.fnull = TemporaryFile() def tearDown(self): self._run(["./waf","distclean"]) self.fnull.close() def _run(self, args): sub = subprocess.Popen(args, stderr=self.fnull, stdout=self.fnull) sub.communicate() self.fnull.seek(0) tail = ''.join(self.fnull.readlines()[-10:]) return sub, tail def _configure(self, gtk, backend, delint): arglist = ["./waf","configure","--gtk", gtk, "--backend", backend] if delint: arglist.append("--enable-delint") sub, tail = self._run(arglist) self.assertEqual(0, sub.returncode, msg=tail) def _build(self): sub, tail = self._run(["./waf","build"]) self.assertEqual(0, sub.returncode, msg=tail) def _configure_and_build(self, gtk, backend, delint): self._configure(gtk, backend, delint) self._build()
def execute_local(args, env=None, zerobyte=False): """ Execute a command locally. This method is a wrapper for :py:class:`subprocess.Popen` with stdout and stderr piped to temporary files and ``shell=True``. :param str args: command with arguments (e.g. 'sbatch myjob.sh') :param dict env: environment variables (default: {}) :return: object with attributes ``stdout``, ``stderr`` \ and ``returncode`` :rtype: :py:obj:`object` """ from tempfile import TemporaryFile from subprocess import Popen # Note: PIPE will cause deadlock if output is larger than 65K stdout, stderr = TemporaryFile("w+"), TemporaryFile("w+") handle = type('Handle', (object, ), { 'stdout': [], 'stderr': [], 'returncode': 0 })() p = Popen(args, stdout=stdout, stderr=stderr, env=env, shell=True) p.wait() if zerobyte: strstdout = stdout.seek(0) or stdout.read() handle.stdout = strstdout.split('\0') else: handle.stdout = stdout.seek(0) or stdout.readlines() handle.stderr = stderr.seek(0) or stderr.readlines() handle.returncode = p.returncode return handle
def execute_local(args, env = {}): """ Execute a command locally. This method is a wrapper for :py:class:`subprocess.Popen` with stdout and stderr piped to temporary files and ``shell=True``. :param str args: command with arguments (e.g. 'sbatch myjob.sh') :param dict env: environment variables (default: {}) :return: object with attributes ``stdout``, ``stderr`` \ and ``returncode`` :rtype: :py:obj:`object` """ from tempfile import TemporaryFile from subprocess import Popen # Note: PIPE will cause deadlock if output is larger than 65K stdout, stderr = TemporaryFile(), TemporaryFile() handle = type('Handle', (object,), {'stdout' : [], 'stderr' : [], 'returncode' : 0})() p = Popen(args, stdout = stdout, stderr = stderr, env = env, shell = True) p.wait() handle.stdout = stdout.seek(0) or stdout.readlines() handle.stderr = stderr.seek(0) or stderr.readlines() handle.returncode = p.returncode return handle
def test_open_in_binary(self): outfile = TemporaryFile('wb+') g = self.getGClass()(outfile=outfile, print_lines=False) g.move(10,10) outfile.seek(0) lines = outfile.readlines() assert(type(lines[0]) == bytes) outfile.close()
def process_qif(self, cr, uid, data_file, journal_id=False, context=None): """ Import a file in the .QIF format""" try: fileobj = TemporaryFile('wb+') fileobj.write(base64.b64decode(data_file)) fileobj.seek(0) file_data = "" for line in fileobj.readlines(): file_data += line fileobj.close() if '\r' in file_data: data_list = file_data.split('\r') else: data_list = file_data.split('\n') header = data_list[0].strip() header = header.split(":")[1] except: raise osv.except_osv(_('Import Error!'), _('Please check QIF file format is proper or not.')) line_ids = [] vals_line = {} total = 0 if header == "Bank": vals_bank_statement = {} for line in data_list: line = line.strip() if not line: continue if line[0] == 'D': # date of transaction vals_line['date'] = dateutil.parser.parse(line[1:], fuzzy=True).date() if vals_line.get('date') and not vals_bank_statement.get('period_id'): period_ids = self.pool.get('account.period').find(cr, uid, vals_line['date'], context=context) vals_bank_statement.update({'period_id': period_ids and period_ids[0] or False}) elif line[0] == 'T': # Total amount total += float(line[1:].replace(',', '')) vals_line['amount'] = float(line[1:].replace(',', '')) elif line[0] == 'N': # Check number vals_line['ref'] = line[1:] elif line[0] == 'P': # Payee bank_account_id, partner_id = self._detect_partner(cr, uid, line[1:], identifying_field='owner_name', context=context) vals_line['partner_id'] = partner_id vals_line['bank_account_id'] = bank_account_id vals_line['name'] = 'name' in vals_line and line[1:] + ': ' + vals_line['name'] or line[1:] elif line[0] == 'M': # Memo vals_line['name'] = 'name' in vals_line and vals_line['name'] + ': ' + line[1:] or line[1:] elif line[0] == '^': # end of item line_ids.append((0, 0, vals_line)) vals_line = {} elif line[0] == '\n': line_ids = [] else: pass else: raise osv.except_osv(_('Error!'), _('Cannot support this Format !Type:%s.') % (header,)) vals_bank_statement.update({'balance_end_real': total, 'line_ids': line_ids, 'journal_id': journal_id}) return [vals_bank_statement]
def run(self): tmpfile = TemporaryFile() shell = Shell(stdout=tmpfile) try: shell.runcmd(self.cmd) shell.end(0, exception=False) except Exception as e: shell.print(str(e) + "\n") tmpfile.seek(0) self.stdout = [x.decode("utf-8") for x in tmpfile.readlines()] tmpfile.close()
def process_binlog(self): """ 伪装 slave 获取数据库中的 binlog :return: """ stream = BinLogStreamReader( connection_settings=self.coon_settings, server_id=100, log_file=self.log_file, log_pos=self.log_pos, only_schemas=self.only_schemas, only_tables=self.only_tables, # blocking=True, resume_stream=self.resume_stream, only_events=[ DeleteRowsEvent, UpdateRowsEvent, WriteRowsEvent ], ) tem_file = TemporaryFile(mode='w+') for binlog_event in stream: for row in binlog_event.rows: # print(binlog_event.extra_data_length) # print(binlog_event.packet.log_pos) try: event_time = datetime.datetime.fromtimestamp(binlog_event.timestamp) except OSError: event_time = datetime.datetime(1980, 1, 1, 0, 0) event = {"schema": binlog_event.schema, "table": binlog_event.table, "event_time": event_time.strftime("%Y-%m-%d %H:%M:%S")} if isinstance(binlog_event, DeleteRowsEvent): event["action"] = "delete" event["data"] = clear_event_type(row["values"]) elif isinstance(binlog_event, UpdateRowsEvent): event["action"] = "update" event["before_data"] = clear_event_type(row["before_values"]) event["after_data"] = clear_event_type(row["after_values"]) elif isinstance(binlog_event, WriteRowsEvent): event["action"] = "insert" event["data"] = clear_event_type(row["values"]) tem_file.write(self.reverse_sql(event)+'\n') tem_file.seek(0) for x in tem_file.readlines()[::-1]: print(x)
def get(self,x=0): if(ImgHandler.img==None): return #print ImgHandler.img tempFile = TemporaryFile() ImgHandler.img.save(tempFile) tempFile.seek(0) lines = tempFile.readlines() #print len(lines) self.output = ''.join(lines) self.set_header("Cache-control", "no-cache") self.set_header("Content-Type","image/png") self.set_header("Content-Length",len(self.output)) self.write(self.output) self.finish()
class TestGFixture(unittest.TestCase): def getGClass(self): return G def setUp(self): self.outfile = TemporaryFile('w+') self.g = self.getGClass()(outfile=self.outfile, print_lines=False, aerotech_include=False) self.expected = "" if self.g.is_relative: self.expect_cmd('G91 ;relative') else: self.expect_cmd('G90 ;absolute') def tearDown(self): self.g.teardown() del self.outfile del self.g # helper functions ####################################################### def expect_cmd(self, cmd): self.expected = self.expected + cmd + '\n' def assert_output(self): string_rep = "" if is_str(self.expected): string_rep = self.expected self.expected = self.expected.split('\n') self.expected = [x.strip() for x in self.expected if x.strip()] self.outfile.seek(0) lines = self.outfile.readlines() if 'b' in self.outfile.mode: lines = [decode2To3(x) for x in lines] lines = [x.strip() for x in lines if x.strip()] self.assertListEqual(lines, self.expected) self.expected = string_rep def assert_almost_position(self, expected_pos): for k, v in expected_pos.items(): self.assertAlmostEqual(self.g.current_position[k], v) def assert_position(self, expected_pos): self.assertEqual(self.g.current_position, expected_pos)
class FilePrinterTest(unittest.TestCase): def test_invalid_construction(self): self.assertRaises(TypeError, FilePrinter, 5) def test_printing(self): self.file = TemporaryFile("w+") self.uut = FilePrinter(self.file) self.uut.print("Test value") self.uut = FilePrinter(self.file) self.uut.print("Test", "value2") self.file.seek(0) lines = self.file.readlines() self.assertEqual(lines, ["Test value\n", "Test value2\n"])
def ejecuta(programa, cadena): stdout = sys.stdout stderr = sys.stderr argv = sys.argv fsalida = TemporaryFile(mode="w+") ferror = TemporaryFile(mode="w+") sys.stdout = fsalida sys.stderr = ferror sys.argv = [programa, cadena] globales = {"__name__": "__main__"} modules = list(sys.modules.keys()) try: exec(compile(open(programa).read(), programa, 'exec'), globales) except SystemExit: pass except: import traceback sei = sys.exc_info() sys.stderr = stderr sys.stdout = stdout traceback.print_exception(sei[0], sei[1], sei[2]) sys.exit(1) for i in list(sys.modules.keys()): if i not in modules: del sys.modules[i] sys.stdout = stdout sys.stderr = stderr sys.argv = argv fsalida.seek(0) ordenes = [] for l in fsalida.readlines(): try: n = int(l) except ValueError: error( 'En la salida del programa he encontrado "{}" en lugar de un número' .format(n)) ordenes.append(n) return ordenes
class CapturedStdout: """Capture sys.out output in temp file to allow function result testing Thanks to Catherine Devlin (catherinedevlin.blogspot.com) for the idea""" def __init__(self): """Capture stdout""" self.backupStdout=sys.stdout self.tmpFile=TemporaryFile() sys.stdout=self.tmpFile def readlines(self, reset=True): """ @param reset: reset buffer for next usage (default is True) @return: array of lines captured and reset buffer""" self.tmpFile.seek(0) lines=self.tmpFile.readlines() if reset: self.reset() return [line.strip("\n").strip("\x00") for line in lines] def reset(self): """Reset stdout buffer""" self.tmpFile.truncate(0) def gotPsyqlException(self, reset=True): """Look if captured output has a PysqlException @param reset: reset buffer for next usage (default is True) @return: True is got exception, else False""" lines=self.readlines(reset) for line in lines: if "Pysql error" in line: return True return False def echoStdout(self): """Echo the current buffer on terminal stdout. Usefull for test debuging""" self.backupStdout.writelines(["%s\n" % line for line in self.readlines(reset=False)]) def restoreStdout(self): sys.stdout=self.backupStdout self.tmpFile.close()
def get_directories(start_dir, find_command): """ Runs a find command and returns a list of directories, checks output for sanity (whether it really is an existing directory). """ def print_logs(stdout, stderr): stdout.seek(0) # move pointer back to beginning before read() stderr.seek(0) delim = 78 * '-' print("%s\nstdout:\n%s\n%s\nstderr:\n%s\n%s" % (delim, stdout.read(), delim, stderr.read(), delim)) print("Running command '%s' ..." % find_command) stdout, stderr = TemporaryFile("w+"), TemporaryFile("w+") try: proc = subprocess.Popen(find_command.split(), stdout=stdout, stderr=stderr) except Exception as ex: print("Exception occured when executing the command, reason: %s" % ex) sys.exit(1) try: returncode = proc.wait() except Exception as ex: print("Exception occured (logs below), reason: %s" % ex) print_logs(stdout, stderr) sys.exit(1) if returncode == 0: print("Command finished.") stdout.seek(0) # move pointer back to beginning before read() # assuming each line of in the output is a returned directory name dirs = [d.strip().replace("./", "") for d in stdout.readlines() \ if os.path.exists(d.strip())] return dirs else: print("Command '%s' failed, return code: %s" % (find_command, returncode)) print_logs(stdout, stderr) sys.exit(1)
def testAddMapping(self): mount = ( "C:/cygwin/bin on /usr/bin type ntfs (binary,auto){LF}" "C:/cygwin/lib on /usr/lib type ntfs (binary,auto){LF}" "C:/cygwin on / type ntfs (binary,auto){LF}" "C: on /cygdrive/c type ntfs (binary,posix=0,user,noumount,auto){LF}" "".format(LF="\n") ); f = TemporaryFile(mode='w+'); f.writelines(mount); f.seek(0); mtab = f.readlines(); f.close(); mapping = { '/usr/bin/': "C:/cygwin/bin/", '/usr/lib/': "C:/cygwin/lib/", '/cygdrive/c/': "C:/", }; self.obj._addMapping(mtab); self.assertEqual(self.obj.getMap(), mapping); self.assertEqual(self.obj.getMountRoot(), "C:/cygwin/");
def testAddMapping(self): pm = self._createPathMapper(self._var_root, self._var_cygwin_p) mount = ( "C:/cygwin/bin on /usr/bin type ntfs (binary,auto){LF}" "C:/cygwin/lib on /usr/lib type ntfs (binary,auto){LF}" "C:/cygwin on / type ntfs (binary,auto){LF}" "C: on /cygdrive/c type ntfs (binary,posix=0,user,noumount,auto){LF}" "".format(LF="\n")) f = TemporaryFile(mode='w+') f.writelines(mount) f.seek(0) mtab = f.readlines() f.close() mapping = { '/usr/bin/': "C:/cygwin/bin/", '/usr/lib/': "C:/cygwin/lib/", '/cygdrive/c/': "C:/", } pm._addMapping(mtab) self.assertEqual(pm.getMap(), mapping) self.assertEqual(pm.getMountRoot(), "C:/cygwin/")
def _process_data_2(self, cr, uid, ids, data, context): #try: fileobj = TemporaryFile('w+') fileobj.write(data) fileobj.seek(0) lines = [] for line in fileobj.readlines(): lines = line.split(',') #if len(lines) == 0: break if self._isnumeric(lines[0]) == True: id = int(lines[0]) sign_date_new = datetime.strptime(lines[1].replace("\n",""), '%m/%d/%Y %H:%M:%S'); sign_date_str = sign_date_new.strftime('%Y-%m-%d %H:%M:%S') #log.info('id=%s,date=%s' % (id, sign_date)) #check existing day = sign_date_new.strftime('%Y-%m-%d') attds = self.pool.get('hr.attendance') attd_ids = attds.search(cr, uid, [('employee_id','=',id),('day','=',day)], context=context) #log.info(attd_ids) #log.info('employee_id=%d,attd_ids=%s,len=%d,day=%s' % (id,attd_ids,len(attd_ids), day)) if len(attd_ids) == 0: attds.create(cr, uid, {'employee_id':id,'name':sign_date_str,'action':'sign_in','source':'import'}) elif len(attd_ids) == 1: #check tgl sign dan action attd = attds.browse(cr, uid, attd_ids, context=context) sign_date_old = datetime.strptime(attd[0].name, '%Y-%m-%d %H:%M:%S') if sign_date_new > sign_date_old: attds.create(cr, uid, {'employee_id':id,'name':sign_date_str,'action':'sign_out','source':'import'}) else: #update existing action from "sign_in" to "sign_out" attds.write(cr, uid, [attd[0].id], {'action':'sign_out'}) #insert new attds.create(cr, uid, {'employee_id':id,'name':sign_date_str,'action':'sign_in','source':'import'}) fileobj.close()
def run_import(self, cr, uid, ids, automatic=False, use_new_cursor=False, context=None): #pool = pooler.get_pool(cr.dbname) #import pdb;pdb.set_trace() import_data = self.browse(cr, uid, ids)[0] cerca =[] fileobj = TemporaryFile('w+') fileobj.write(base64.decodestring(import_data.data)) fileobj.seek(0) lines = fileobj.readlines() testo_log = """Inizio procedura di Importazione""" + time.ctime() + '\n' error = False nome ='' #percorso = '/home/openerp/filecsv' #import pdb;pdb.set_trace() if use_new_cursor: use_new_cursor= use_new_cursor[0] cr = pooler.get_db(use_new_cursor).cursor() # elenco_csv = os.listdir(percorso) res = False try: if use_new_cursor: cr = pooler.get_db(use_new_cursor).cursor() if import_data.clienti_base: pass res = self._import_ana_base(cr, uid, lines,"C", context) if import_data.fornitori_base: pass res = self._import_ana_fornitori(cr, uid, lines,"F", context) if import_data.banche: pass res = self._import_banche(cr, uid, lines, context) if import_data.des_dive_cli: res = self._import_desdive(cr, uid, lines, context) if use_new_cursor: cr.commit() if res: testo_log = testo_log + " Inseriti " + str(res[0]) + " Aggiornati " + str(res[1]) + " \n" else: testo_log = testo_log + " File non riconosciuto " + codfor[0] + " non trovato \n" testo_log = testo_log + " Operazione Teminata alle " + time.ctime() + "\n" #invia e-mail type_ = 'plain' tools.email_send('*****@*****.**', ['*****@*****.**'], 'Importazione da Altre Procedure', testo_log, subtype=type_, ) finally: if use_new_cursor: try: cr.close() except Exception: pass return {'type': 'ir.actions.act_window_close'}
class TestG(unittest.TestCase): def setUp(self): self.outfile = TemporaryFile() self.g = G(outfile=self.outfile, print_lines=False, aerotech_include=False) self.expected = "" if self.g.is_relative: self.expect_cmd('G91') else: self.expect_cmd('G90') def tearDown(self): self.g.teardown() del self.outfile del self.g def test_init(self): self.assertEqual(self.g.is_relative, True) def test_set_home(self): g = self.g g.set_home() self.expect_cmd('G92') self.assert_output() g.set_home(x=10, y=20, A=5) self.expect_cmd('G92 X10.000000 Y20.000000 A5.000000') self.assert_output() self.assert_position({'A': 5.0, 'x': 10.0, 'y': 20.0, 'z': 0}) g.set_home(y=0) self.assert_position({'A': 5.0, 'x': 10.0, 'y': 0.0, 'z': 0}) def test_reset_home(self): self.g.reset_home() self.expect_cmd('G92.1') self.assert_output() def test_relative(self): self.assertEqual(self.g.is_relative, True) self.g.absolute() self.expect_cmd('G90') self.g.relative() self.assertEqual(self.g.is_relative, True) self.expect_cmd('G91') self.assert_output() self.g.relative() self.assertEqual(self.g.is_relative, True) self.assert_output() def test_absolute(self): self.g.absolute() self.assertEqual(self.g.is_relative, False) self.expect_cmd('G90') self.assert_output() self.g.absolute() self.assertEqual(self.g.is_relative, False) self.assert_output() def test_feed(self): self.g.feed(10) self.expect_cmd('G1 F10') self.assert_output() def test_dwell(self): self.g.dwell(10) self.expect_cmd('G4 P10') self.assert_output() def test_setup(self): self.outfile = TemporaryFile() self.g = G(outfile=self.outfile, print_lines=False) self.expected = "" self.expect_cmd(open(os.path.join(HERE, '../header.txt')).read()) self.expect_cmd('G91') self.assert_output() def test_home(self): self.g.home() self.expect_cmd(""" G90 G1 X0.000000 Y0.000000 G91 """) self.assert_output() self.assert_position({'x': 0, 'y': 0, 'z': 0}) def test_move(self): g = self.g g.move(10, 10) self.assert_position({'x': 10.0, 'y': 10.0, 'z': 0}) g.move(10, 10, A=50) self.assert_position({'x': 20.0, 'y': 20.0, 'A': 50, 'z': 0}) g.move(10, 10, 10) self.assert_position({'x': 30.0, 'y': 30.0, 'A': 50, 'z': 10}) self.expect_cmd(""" G1 X10.000000 Y10.000000 G1 X10.000000 Y10.000000 A50.000000 G1 X10.000000 Y10.000000 Z10.000000 """) self.assert_output() def test_abs_move(self): self.g.relative() self.g.abs_move(10, 10) self.expect_cmd(""" G90 G1 X10.000000 Y10.000000 G91 """) self.assert_output() self.assert_position({'x': 10, 'y': 10, 'z': 0}) self.g.abs_move(5, 5, 5) self.expect_cmd(""" G90 G1 X5.000000 Y5.000000 Z5.000000 G91 """) self.assert_output() self.assert_position({'x': 5, 'y': 5, 'z': 5}) self.g.abs_move(15, 0, D=5) self.expect_cmd(""" G90 G1 X15.000000 Y0.000000 D5.000000 G91 """) self.assert_output() self.assert_position({'x': 15, 'y': 0, 'D': 5, 'z': 5}) self.g.absolute() self.g.abs_move(19, 18, D=6) self.expect_cmd(""" G90 G1 X19.000000 Y18.000000 D6.000000 """) self.assert_output() self.assert_position({'x': 19, 'y': 18, 'D': 6, 'z': 5}) self.g.relative() def test_arc(self): with self.assertRaises(RuntimeError): self.g.arc() self.g.arc(x=10, y=0) self.expect_cmd(""" G17 G2 X10.000000 Y0.000000 R5.000000 """) self.assert_output() self.assert_position({'x': 10, 'y': 0, 'z': 0}) self.g.arc(x=5, A=0, direction='CCW', radius=5) self.expect_cmd(""" G16 X Y A G18 G3 X5.000000 A0.000000 R5.000000 """) self.assert_output() self.assert_position({'x': 15, 'y': 0, 'A': 0, 'z': 0}) self.g.arc(x=0, y=10, helix_dim='D', helix_len=10) self.expect_cmd(""" G16 X Y D G17 G2 X0.000000 Y10.000000 R5.000000 G1 D10 """) self.assert_output() self.assert_position({'x': 15, 'y': 10, 'A': 0, 'D': 10, 'z': 0}) self.g.arc(0, 10, helix_dim='D', helix_len=10) self.expect_cmd(""" G16 X Y D G17 G2 X0.000000 Y10.000000 R5.000000 G1 D10 """) self.assert_output() self.assert_position({'x': 15, 'y': 20, 'A': 0, 'D': 20, 'z': 0}) with self.assertRaises(RuntimeError): self.g.arc(x=10, y=10, radius=1) def test_abs_arc(self): self.g.relative() self.g.abs_arc(x=0, y=10) self.expect_cmd(""" G90 G17 G2 X0.000000 Y10.000000 R5.000000 G91 """) self.assert_output() self.assert_position({'x': 0, 'y': 10, 'z': 0}) self.g.abs_arc(x=0, y=10) self.expect_cmd(""" G90 G17 G2 X0.000000 Y10.000000 R0.000000 G91 """) self.assert_output() self.assert_position({'x': 0, 'y': 10, 'z': 0}) self.g.absolute() self.g.abs_arc(x=0, y=20) self.expect_cmd(""" G90 G17 G2 X0.000000 Y20.000000 R5.000000 """) self.assert_output() self.assert_position({'x': 0, 'y': 20, 'z': 0}) self.g.relative() def test_rect(self): self.g.rect(10, 5) self.expect_cmd(""" G1 Y5.000000 G1 X10.000000 G1 Y-5.000000 G1 X-10.000000 """) self.assert_output() self.assert_position({'x': 0, 'y': 0, 'z': 0}) self.g.rect(10, 5, start='UL') self.expect_cmd(""" G1 X10.000000 G1 Y-5.000000 G1 X-10.000000 G1 Y5.000000 """) self.assert_output() self.assert_position({'x': 0, 'y': 0, 'z': 0}) self.g.rect(10, 5, start='UR') self.expect_cmd(""" G1 Y-5.000000 G1 X-10.000000 G1 Y5.000000 G1 X10.000000 """) self.assert_output() self.assert_position({'x': 0, 'y': 0, 'z': 0}) self.g.rect(10, 5, start='LR') self.expect_cmd(""" G1 X-10.000000 G1 Y5.000000 G1 X10.000000 G1 Y-5.000000 """) self.assert_output() self.assert_position({'x': 0, 'y': 0, 'z': 0}) self.g.rect(10, 5, start='LL', direction='CCW') self.expect_cmd(""" G1 X10.000000 G1 Y5.000000 G1 X-10.000000 G1 Y-5.000000 """) self.assert_output() self.assert_position({'x': 0, 'y': 0, 'z': 0}) self.g.rect(10, 5, start='UL', direction='CCW') self.expect_cmd(""" G1 Y-5.000000 G1 X10.000000 G1 Y5.000000 G1 X-10.000000 """) self.assert_output() self.assert_position({'x': 0, 'y': 0, 'z': 0}) self.g.rect(10, 5, start='UR', direction='CCW') self.expect_cmd(""" G1 X-10.000000 G1 Y-5.000000 G1 X10.000000 G1 Y5.000000 """) self.assert_output() self.assert_position({'x': 0, 'y': 0, 'z': 0}) self.g.rect(10, 5, start='LR', direction='CCW') self.expect_cmd(""" G1 Y5.000000 G1 X-10.000000 G1 Y-5.000000 G1 X10.000000 """) self.assert_output() self.assert_position({'x': 0, 'y': 0, 'z': 0}) def test_meander(self): self.g.meander(2, 2, 1) self.expect_cmd(""" G1 X2.000000 G1 Y1.000000 G1 X-2.000000 G1 Y1.000000 G1 X2.000000 """) self.assert_output() self.assert_position({'x': 2, 'y': 2, 'z': 0}) self.g.meander(2, 2, 1.1) self.expect_cmd(""" ;WARNING! meander spacing updated from 1.1 to 1.0 G1 X2.000000 G1 Y1.000000 G1 X-2.000000 G1 Y1.000000 G1 X2.000000 """) self.assert_output() self.assert_position({'x': 4, 'y': 4, 'z': 0}) self.g.meander(2, 2, 1, start='UL') self.expect_cmd(""" G1 X2.000000 G1 Y-1.000000 G1 X-2.000000 G1 Y-1.000000 G1 X2.000000 """) self.assert_output() self.assert_position({'x': 6, 'y': 2, 'z': 0}) self.g.meander(2, 2, 1, start='UR') self.expect_cmd(""" G1 X-2.000000 G1 Y-1.000000 G1 X2.000000 G1 Y-1.000000 G1 X-2.000000 """) self.assert_output() self.assert_position({'x': 4, 'y': 0, 'z': 0}) self.g.meander(2, 2, 1, start='LR') self.expect_cmd(""" G1 X-2.000000 G1 Y1.000000 G1 X2.000000 G1 Y1.000000 G1 X-2.000000 """) self.assert_output() self.assert_position({'x': 2, 'y': 2, 'z': 0}) self.g.meander(2, 2, 1, start='LR', orientation='y') self.expect_cmd(""" G1 Y2.000000 G1 X-1.000000 G1 Y-2.000000 G1 X-1.000000 G1 Y2.000000 """) self.assert_output() self.assert_position({'x': 0, 'y': 4, 'z': 0}) self.g.meander(3, 2, 1, start='LR', orientation='y') self.expect_cmd(""" G1 Y2.000000 G1 X-1.000000 G1 Y-2.000000 G1 X-1.000000 G1 Y2.000000 G1 X-1.000000 G1 Y-2.000000 """) self.assert_output() self.assert_position({'x': -3, 'y': 4, 'z': 0}) def test_clip(self): self.g.clip() self.expect_cmd(""" G16 X Y Z G18 G3 X0.000000 Z4.000000 R2.000000 """) self.assert_output() self.assert_position({'y': 0, 'x': 0, 'z': 4}) self.g.clip(axis='A', direction='-y', height=10) self.expect_cmd(""" G16 X Y A G19 G2 Y0.000000 A10.000000 R5.000000 """) self.assert_output() self.assert_position({'x': 0, 'y': 0, 'z': 4, 'A': 10}) self.g.clip(axis='A', direction='-y', height=-10) self.expect_cmd(""" G16 X Y A G19 G3 Y0.000000 A-10.000000 R5.000000 """) self.assert_output() self.assert_position({'x': 0, 'y': 0, 'z': 4, 'A': 0}) def test_toggle_pressure(self): self.g.toggle_pressure(0) self.expect_cmd('Call togglePress P0') self.assert_output() def test_set_pressure(self): self.g.set_pressure(0, 10) self.expect_cmd('Call setPress P0 Q10') self.assert_output() def test_set_valve(self): self.g.set_valve(0, 1) self.expect_cmd('$DO0.0=1') self.assert_output() def test_rename_axis(self): self.g.rename_axis(z='A') self.g.move(10, 10, 10) self.assert_position({'x': 10.0, 'y': 10.0, 'A': 10, 'z': 10}) self.expect_cmd(""" G1 X10.000000 Y10.000000 A10.000000 """) self.assert_output() self.g.rename_axis(z='B') self.g.move(10, 10, 10) self.assert_position({'x': 20.0, 'y': 20.0, 'z': 20, 'A': 10, 'B': 10}) self.expect_cmd(""" G1 X10.000000 Y10.000000 B10.000000 """) self.assert_output() self.g.rename_axis(x='W') self.g.move(10, 10, 10) self.assert_position({ 'x': 30.0, 'y': 30.0, 'z': 30, 'A': 10, 'B': 20, 'W': 10 }) self.expect_cmd(""" G1 W10.000000 Y10.000000 B10.000000 """) self.assert_output() self.g.rename_axis(x='X') self.g.arc(x=10, z=10) self.assert_position({ 'x': 40.0, 'y': 30.0, 'z': 40, 'A': 10, 'B': 30, 'W': 10 }) self.expect_cmd(""" G16 X Y B G18 G2 X10.000000 B10.000000 R7.071068 """) self.assert_output() self.g.abs_arc(x=0, z=0) self.assert_position({ 'x': 0.0, 'y': 30.0, 'z': 0, 'A': 10, 'B': 0, 'W': 10 }) self.expect_cmd(""" G90 G16 X Y B G18 G2 X0.000000 B0.000000 R28.284271 G91 """) self.assert_output() self.g.meander(10, 10, 10) self.expect_cmd(""" G1 X10.000000 G1 Y10.000000 G1 X-10.000000 """) self.assert_output() # helper functions ####################################################### def expect_cmd(self, cmd): self.expected = self.expected + cmd + '\n' def assert_output(self): string_rep = "" if is_str(self.expected): string_rep = self.expected self.expected = self.expected.split('\n') self.expected = [x.strip() for x in self.expected if x.strip()] self.outfile.seek(0) lines = self.outfile.readlines() lines = [decode2To3(x).strip() for x in lines if x.strip()] self.assertListEqual(lines, self.expected) self.expected = string_rep def assert_position(self, expected_pos): self.assertEqual(self.g.current_position, expected_pos)
def run_import(self, cr, uid, ids, automatic=False, use_new_cursor=False, context=None): #pool = pooler.get_pool(cr.dbname) #import pdb;pdb.set_trace() import_data = self.browse(cr, uid, ids)[0] cerca = [] fileobj = TemporaryFile('w+') fileobj.write(base64.decodestring(import_data.data)) fileobj.seek(0) lines = fileobj.readlines() testo_log = """Inizio procedura di Importazione""" + time.ctime( ) + '\n' error = False nome = '' #percorso = '/home/openerp/filecsv' #import pdb;pdb.set_trace() if use_new_cursor: use_new_cursor = use_new_cursor[0] cr = pooler.get_db(use_new_cursor).cursor() # elenco_csv = os.listdir(percorso) res = False try: if use_new_cursor: cr = pooler.get_db(use_new_cursor).cursor() if import_data.clienti_base: pass res = self._import_ana_base(cr, uid, lines, "C", context) if import_data.fornitori_base: pass res = self._import_ana_fornitori(cr, uid, lines, "F", context) if import_data.banche: pass res = self._import_banche(cr, uid, lines, context) if import_data.des_dive_cli: res = self._import_desdive(cr, uid, lines, context) if use_new_cursor: cr.commit() if res: testo_log = testo_log + " Inseriti " + str( res[0]) + " Aggiornati " + str(res[1]) + " \n" else: testo_log = testo_log + " File non riconosciuto " + codfor[ 0] + " non trovato \n" testo_log = testo_log + " Operazione Teminata alle " + time.ctime( ) + "\n" #invia e-mail type_ = 'plain' tools.email_send( '*****@*****.**', ['*****@*****.**'], 'Importazione da Altre Procedure', testo_log, subtype=type_, ) finally: if use_new_cursor: try: cr.close() except Exception: pass return {'type': 'ir.actions.act_window_close'}
class TestG(unittest.TestCase): def setUp(self): self.outfile = TemporaryFile() self.g = G(outfile=self.outfile, print_lines=False, aerotech_include=False) self.expected = "" if self.g.is_relative: self.expect_cmd('G91') else: self.expect_cmd('G90') def tearDown(self): self.g.teardown() del self.outfile del self.g def test_init(self): self.assertEqual(self.g.is_relative, True) def test_set_home(self): g = self.g g.set_home() self.expect_cmd('G92') self.assert_output() g.set_home(x=10, y=20, A=5) self.expect_cmd('G92 X10.000000 Y20.000000 A5.000000') self.assert_output() self.assert_position({'A': 5.0, 'x': 10.0, 'y': 20.0, 'z': 0}) g.set_home(y=0) self.assert_position({'A': 5.0, 'x': 10.0, 'y': 0.0, 'z': 0}) def test_reset_home(self): self.g.reset_home() self.expect_cmd('G92.1') self.assert_output() def test_relative(self): self.assertEqual(self.g.is_relative, True) self.g.absolute() self.expect_cmd('G90') self.g.relative() self.assertEqual(self.g.is_relative, True) self.expect_cmd('G91') self.assert_output() self.g.relative() self.assertEqual(self.g.is_relative, True) self.assert_output() def test_absolute(self): self.g.absolute() self.assertEqual(self.g.is_relative, False) self.expect_cmd('G90') self.assert_output() self.g.absolute() self.assertEqual(self.g.is_relative, False) self.assert_output() def test_feed(self): self.g.feed(10) self.expect_cmd('G1 F10') self.assert_output() def test_dwell(self): self.g.dwell(10) self.expect_cmd('G4 P10') self.assert_output() def test_setup(self): self.outfile = TemporaryFile() self.g = G(outfile=self.outfile, print_lines=False) self.expected = "" self.expect_cmd(open(os.path.join(HERE, '../header.txt')).read()) self.expect_cmd('G91') self.assert_output() def test_home(self): self.g.home() self.expect_cmd(""" G90 G1 X0.000000 Y0.000000 G91 """) self.assert_output() self.assert_position({'x': 0, 'y': 0, 'z': 0}) def test_move(self): g = self.g g.move(10, 10) self.assert_position({'x': 10.0, 'y': 10.0, 'z': 0}) g.move(10, 10, A=50) self.assert_position({'x': 20.0, 'y': 20.0, 'A': 50, 'z': 0}) g.move(10, 10, 10) self.assert_position({'x': 30.0, 'y': 30.0, 'A': 50, 'z': 10}) self.expect_cmd(""" G1 X10.000000 Y10.000000 G1 X10.000000 Y10.000000 A50.000000 G1 X10.000000 Y10.000000 Z10.000000 """) self.assert_output() def test_abs_move(self): self.g.relative() self.g.abs_move(10, 10) self.expect_cmd(""" G90 G1 X10.000000 Y10.000000 G91 """) self.assert_output() self.assert_position({'x': 10, 'y': 10, 'z': 0}) self.g.abs_move(5, 5, 5) self.expect_cmd(""" G90 G1 X5.000000 Y5.000000 Z5.000000 G91 """) self.assert_output() self.assert_position({'x': 5, 'y': 5, 'z': 5}) self.g.abs_move(15, 0, D=5) self.expect_cmd(""" G90 G1 X15.000000 Y0.000000 D5.000000 G91 """) self.assert_output() self.assert_position({'x': 15, 'y': 0, 'D': 5, 'z': 5}) self.g.absolute() self.g.abs_move(19, 18, D=6) self.expect_cmd(""" G90 G1 X19.000000 Y18.000000 D6.000000 """) self.assert_output() self.assert_position({'x': 19, 'y': 18, 'D': 6, 'z': 5}) self.g.relative() def test_arc(self): with self.assertRaises(RuntimeError): self.g.arc() self.g.arc(x=10, y=0) self.expect_cmd(""" G17 G2 X10.000000 Y0.000000 R5.000000 """) self.assert_output() self.assert_position({'x': 10, 'y': 0, 'z': 0}) self.g.arc(x=5, A=0, direction='CCW', radius=5) self.expect_cmd(""" G16 X Y A G18 G3 X5.000000 A0.000000 R5.000000 """) self.assert_output() self.assert_position({'x': 15, 'y': 0, 'A': 0, 'z': 0}) self.g.arc(x=0, y=10, helix_dim='D', helix_len=10) self.expect_cmd(""" G16 X Y D G17 G2 X0.000000 Y10.000000 R5.000000 G1 D10 """) self.assert_output() self.assert_position({'x': 15, 'y': 10, 'A': 0, 'D': 10, 'z': 0}) self.g.arc(0, 10, helix_dim='D', helix_len=10) self.expect_cmd(""" G16 X Y D G17 G2 X0.000000 Y10.000000 R5.000000 G1 D10 """) self.assert_output() self.assert_position({'x': 15, 'y': 20, 'A': 0, 'D': 20, 'z': 0}) with self.assertRaises(RuntimeError): self.g.arc(x=10, y=10, radius=1) def test_abs_arc(self): self.g.relative() self.g.abs_arc(x=0, y=10) self.expect_cmd(""" G90 G17 G2 X0.000000 Y10.000000 R5.000000 G91 """) self.assert_output() self.assert_position({'x': 0, 'y': 10, 'z': 0}) self.g.abs_arc(x=0, y=10) self.expect_cmd(""" G90 G17 G2 X0.000000 Y10.000000 R0.000000 G91 """) self.assert_output() self.assert_position({'x': 0, 'y': 10, 'z': 0}) self.g.absolute() self.g.abs_arc(x=0, y=20) self.expect_cmd(""" G90 G17 G2 X0.000000 Y20.000000 R5.000000 """) self.assert_output() self.assert_position({'x': 0, 'y': 20, 'z': 0}) self.g.relative() def test_rect(self): self.g.rect(10, 5) self.expect_cmd(""" G1 Y5.000000 G1 X10.000000 G1 Y-5.000000 G1 X-10.000000 """) self.assert_output() self.assert_position({'x': 0, 'y': 0, 'z': 0}) self.g.rect(10, 5, start='UL') self.expect_cmd(""" G1 X10.000000 G1 Y-5.000000 G1 X-10.000000 G1 Y5.000000 """) self.assert_output() self.assert_position({'x': 0, 'y': 0, 'z': 0}) self.g.rect(10, 5, start='UR') self.expect_cmd(""" G1 Y-5.000000 G1 X-10.000000 G1 Y5.000000 G1 X10.000000 """) self.assert_output() self.assert_position({'x': 0, 'y': 0, 'z': 0}) self.g.rect(10, 5, start='LR') self.expect_cmd(""" G1 X-10.000000 G1 Y5.000000 G1 X10.000000 G1 Y-5.000000 """) self.assert_output() self.assert_position({'x': 0, 'y': 0, 'z': 0}) self.g.rect(10, 5, start='LL', direction='CCW') self.expect_cmd(""" G1 X10.000000 G1 Y5.000000 G1 X-10.000000 G1 Y-5.000000 """) self.assert_output() self.assert_position({'x': 0, 'y': 0, 'z': 0}) self.g.rect(10, 5, start='UL', direction='CCW') self.expect_cmd(""" G1 Y-5.000000 G1 X10.000000 G1 Y5.000000 G1 X-10.000000 """) self.assert_output() self.assert_position({'x': 0, 'y': 0, 'z': 0}) self.g.rect(10, 5, start='UR', direction='CCW') self.expect_cmd(""" G1 X-10.000000 G1 Y-5.000000 G1 X10.000000 G1 Y5.000000 """) self.assert_output() self.assert_position({'x': 0, 'y': 0, 'z': 0}) self.g.rect(10, 5, start='LR', direction='CCW') self.expect_cmd(""" G1 Y5.000000 G1 X-10.000000 G1 Y-5.000000 G1 X10.000000 """) self.assert_output() self.assert_position({'x': 0, 'y': 0, 'z': 0}) def test_meander(self): self.g.meander(2, 2, 1) self.expect_cmd(""" G1 X2.000000 G1 Y1.000000 G1 X-2.000000 G1 Y1.000000 G1 X2.000000 """) self.assert_output() self.assert_position({'x': 2, 'y': 2, 'z': 0}) self.g.meander(2, 2, 1.1) self.expect_cmd(""" ;WARNING! meander spacing updated from 1.1 to 1.0 G1 X2.000000 G1 Y1.000000 G1 X-2.000000 G1 Y1.000000 G1 X2.000000 """) self.assert_output() self.assert_position({'x': 4, 'y': 4, 'z': 0}) self.g.meander(2, 2, 1, start='UL') self.expect_cmd(""" G1 X2.000000 G1 Y-1.000000 G1 X-2.000000 G1 Y-1.000000 G1 X2.000000 """) self.assert_output() self.assert_position({'x': 6, 'y': 2, 'z': 0}) self.g.meander(2, 2, 1, start='UR') self.expect_cmd(""" G1 X-2.000000 G1 Y-1.000000 G1 X2.000000 G1 Y-1.000000 G1 X-2.000000 """) self.assert_output() self.assert_position({'x': 4, 'y': 0, 'z': 0}) self.g.meander(2, 2, 1, start='LR') self.expect_cmd(""" G1 X-2.000000 G1 Y1.000000 G1 X2.000000 G1 Y1.000000 G1 X-2.000000 """) self.assert_output() self.assert_position({'x': 2, 'y': 2, 'z': 0}) self.g.meander(2, 2, 1, start='LR', orientation='y') self.expect_cmd(""" G1 Y2.000000 G1 X-1.000000 G1 Y-2.000000 G1 X-1.000000 G1 Y2.000000 """) self.assert_output() self.assert_position({'x': 0, 'y': 4, 'z': 0}) # test we return to absolute self.g.absolute() self.g.meander(3, 2, 1, start='LR', orientation='y') self.expect_cmd(""" G90 G91 G1 Y2.000000 G1 X-1.000000 G1 Y-2.000000 G1 X-1.000000 G1 Y2.000000 G1 X-1.000000 G1 Y-2.000000 G90 """) self.assert_output() self.assert_position({'x': -3, 'y': 4, 'z': 0}) def test_clip(self): self.g.clip() self.expect_cmd(""" G16 X Y Z G18 G3 X0.000000 Z4.000000 R2.000000 """) self.assert_output() self.assert_position({'y': 0, 'x': 0, 'z': 4}) self.g.clip(axis='A', direction='-y', height=10) self.expect_cmd(""" G16 X Y A G19 G2 Y0.000000 A10.000000 R5.000000 """) self.assert_output() self.assert_position({'x': 0, 'y': 0, 'z': 4, 'A': 10}) self.g.clip(axis='A', direction='-y', height=-10) self.expect_cmd(""" G16 X Y A G19 G3 Y0.000000 A-10.000000 R5.000000 """) self.assert_output() self.assert_position({'x': 0, 'y': 0, 'z': 4, 'A': 0}) def test_toggle_pressure(self): self.g.toggle_pressure(0) self.expect_cmd('Call togglePress P0') self.assert_output() def test_set_pressure(self): self.g.set_pressure(0, 10) self.expect_cmd('Call setPress P0 Q10') self.assert_output() def test_set_valve(self): self.g.set_valve(0, 1) self.expect_cmd('$DO0.0=1') self.assert_output() def test_rename_axis(self): self.g.rename_axis(z='A') self.g.move(10, 10, 10) self.assert_position({'x': 10.0, 'y': 10.0, 'A': 10, 'z': 10}) self.expect_cmd(""" G1 X10.000000 Y10.000000 A10.000000 """) self.assert_output() self.g.rename_axis(z='B') self.g.move(10, 10, 10) self.assert_position({'x': 20.0, 'y': 20.0, 'z': 20, 'A': 10, 'B': 10}) self.expect_cmd(""" G1 X10.000000 Y10.000000 B10.000000 """) self.assert_output() self.g.rename_axis(x='W') self.g.move(10, 10, 10) self.assert_position({'x': 30.0, 'y': 30.0, 'z': 30, 'A': 10, 'B': 20, 'W': 10}) self.expect_cmd(""" G1 W10.000000 Y10.000000 B10.000000 """) self.assert_output() self.g.rename_axis(x='X') self.g.arc(x=10, z=10) self.assert_position({'x': 40.0, 'y': 30.0, 'z': 40, 'A': 10, 'B': 30, 'W': 10}) self.expect_cmd(""" G16 X Y B G18 G2 X10.000000 B10.000000 R7.071068 """) self.assert_output() self.g.abs_arc(x=0, z=0) self.assert_position({'x': 0.0, 'y': 30.0, 'z': 0, 'A': 10, 'B': 0, 'W': 10}) self.expect_cmd(""" G90 G16 X Y B G18 G2 X0.000000 B0.000000 R28.284271 G91 """) self.assert_output() self.g.meander(10, 10, 10) self.expect_cmd(""" G1 X10.000000 G1 Y10.000000 G1 X-10.000000 """) self.assert_output() def test_meander_helpers(self): self.assertEqual(self.g.meander_spacing(12, 1.5), 1.5) self.assertEqual(self.g.meander_spacing(10, 2.2), 2) self.assertEqual(self.g.meander_passes(11, 1.5), 8) self.assertEqual(self.g.meander_spacing(1, 0.11), 0.1) def test_triangular_wave(self): self.g.triangular_wave(2, 2, 1) self.expect_cmd(""" G1 X2.000000 Y2.000000 G1 X2.000000 Y-2.000000 """) self.assert_output() self.assert_position({'x': 4, 'y': 0, 'z': 0}) self.g.triangular_wave(1, 2, 2.5, orientation='y') self.expect_cmd(""" G1 X1.000000 Y2.000000 G1 X-1.000000 Y2.000000 G1 X1.000000 Y2.000000 G1 X-1.000000 Y2.000000 G1 X1.000000 Y2.000000 """) self.assert_output() self.assert_position({'x': 5, 'y': 10, 'z': 0}) self.g.triangular_wave(2, 2, 1.5, start='UL') self.expect_cmd(""" G1 X-2.000000 Y2.000000 G1 X-2.000000 Y-2.000000 G1 X-2.000000 Y2.000000 """) self.assert_output() self.assert_position({'x': -1, 'y': 12, 'z': 0}) self.g.triangular_wave(2, 2, 1, start='LR') self.expect_cmd(""" G1 X2.000000 Y-2.000000 G1 X2.000000 Y2.000000 """) self.assert_output() self.assert_position({'x': 3, 'y': 12, 'z': 0}) self.g.triangular_wave(2, 2, 1, start='LR', orientation='y') self.expect_cmd(""" G1 X2.000000 Y-2.000000 G1 X-2.000000 Y-2.000000 """) self.assert_output() self.assert_position({'x': 3, 'y': 8, 'z': 0}) # test we return to absolute self.g.absolute() self.g.triangular_wave(3, 2, 1, start='LR', orientation='y') self.expect_cmd(""" G90 G91 G1 X3.000000 Y-2.000000 G1 X-3.000000 Y-2.000000 G90 """) self.assert_output() self.assert_position({'x': 3, 'y': 4, 'z': 0}) # helper functions ####################################################### def expect_cmd(self, cmd): self.expected = self.expected + cmd + '\n' def assert_output(self): string_rep = "" if is_str(self.expected): string_rep = self.expected self.expected = self.expected.split('\n') self.expected = [x.strip() for x in self.expected if x.strip()] self.outfile.seek(0) lines = self.outfile.readlines() lines = [decode2To3(x).strip() for x in lines if x.strip()] self.assertListEqual(lines, self.expected) self.expected = string_rep def assert_position(self, expected_pos): self.assertEqual(self.g.current_position, expected_pos)
def _import_product_func(self, cr, uid, data, context): form=data['form'] fileobj = TemporaryFile('w+') fileobj.write( base64.decodestring(form['data']) ) # now we determine the file format #import pdb;pdb.set_trace() fileobj.seek(0) pool = pooler.get_pool(cr.dbname) product_obj = pool.get('product.product') partner_obj = pool.get('res_partner') par_imp_product_obj = pool.get('par.imp.product') idPar = data['id'] lista_idspar = par_imp_product_obj.search(cr,uid,[('supplier_id', '=',idPar)]) lista_parametri = par_imp_product_obj.read(cr, uid, lista_idspar, ['column_name_product','column_number_file_import','eval_function'], context) for riga in fileobj.readlines(): riga = riga.replace('"', '') riga = riga.split(";") Articolo = dict() for riga_par in lista_parametri: campo = riga_par['column_name_product'] colonnacsv = int(riga_par['column_number_file_import'])-1 if colonnacsv == -1: #import pdb;pdb.set_trace() # deve costruire il campo dalla funzione, i caratteri di costruzione # sono separati dalla virgola, per o/ra somma soltanto le colonne interessate e / # cli eventuali separatori inseriti 19/10/2010 funzione = riga_par['eval_function'].split(",") calcolo ='' for car in funzione: if type(eval(car)) == str: calcolo=calcolo+car else: calcolo=calcolo+riga[int(car)-1] Articolo[campo]= calcolo else: Articolo[campo] = riga[colonnacsv] # HA COSTRUITO IL DIZIONARIO CON L'ARTICOLO LO CERCA PER CODICE PER DECIDERE SE INSERIRE # O MODIFICARE IL SOLO PREZZO PER ORA C&G 15/10/2010 #import pdb;pdb.set_trace() # CONTROLLO UNITA' DI MISURA if Articolo.has_key('uom_id'): if Articolo['uom_id']: argoment = [('name',"=",Articolo['uom_id'])] item = pool.get('product.uom').search(cr,uid,argoment) if item: Articolo['uom_id']=item[0] Articolo['uom_po_id']=item[0] else: #import pdb;pdb.set_trace() Articolo['uom_id']=1 # raise osv.except_osv(('Warning !'),('L unità di misura non può essere vuota !')) Articolo['uom_po_id']=1 # CONTROLLO Categoria Articolo if Articolo.has_key('categ_id'): if Articolo['categ_id']: item = pool.get('product.category').search(cr,uid,[('name',"=",Articolo['categ_id'])]) if item: Articolo['categ_id']=item[0] else: Articolo['categ_id']=1 # CONTROLLO Marchio Articolo if Articolo.has_key('marchio_ids'): if Articolo['marchio_ids']: item = pool.get('marchio.marchio').search(cr,uid,[('name',"=",Articolo['marchio_ids'])]) if item: Articolo['marchio_ids']=item[0] else: Articolo['marchio_ids']=0 #controlla i campi prezzo if Articolo.has_key('price'): Articolo['price'] = Articolo['price'].replace(',','.') if Articolo.has_key('standard_price'): Articolo['standard_price'] = Articolo['standard_price'].replace(',','.') Articolo['list_price']=Articolo['standard_price'] #import pdb;pdb.set_trace() if Articolo.has_key('ean13'): # CONTROLLA PRIMA CHE IL BAR CODE SIA VALIDO PER PROCEDERE if check_ean(Articolo['ean13']): ok = True else: ok = False else: ok = True if ok: RecArticolo = product_obj.search(cr,uid,[('default_code', '=',Articolo['default_code'])]) if RecArticolo: # ARTICOLO ESISTENTE # import pdb;pdb.set_trace() TemplateId=pool.get('product.product').read(cr, uid, RecArticolo, ['product_tmpl_id']) ok = product_obj.write(cr,uid,RecArticolo,Articolo) ok = pool.get('product.template').write(cr,uid,[TemplateId[0]['id']],Articolo) else: #DEVE PREPARARE LA INSERT con eventuali altri controlli aggiuntivi ArticoloId = product_obj.create(cr,uid,Articolo) #import pdb;pdb.set_trace() # INSERISCE I DATI FORNITORE Fornitore = dict() item = pool.get('product.product').search(cr,uid,[('id',"=",ArticoloId)]) TemplateId=pool.get('product.product').read(cr, uid, item, ['product_tmpl_id']) for riga in TemplateId: Template = riga['product_tmpl_id'] Fornitore = { 'product_id':Template[0], 'name':idPar, 'qty':1, 'min_qty':1, 'delay':1, 'product_uom': Articolo['uom_id'] } FornitoreArtId = pool.get('product.supplierinfo').create(cr,uid,Fornitore) #import pdb;pdb.set_trace() fileobj.close() return {}
def import_var(self, cr, uid, ids, context): import_data = self.browse(cr, uid, ids)[0] fileobj = TemporaryFile('w+') fileobj.write(base64.decodestring(import_data.data)) fileobj.seek(0) PrimaRiga = True for riga in fileobj.readlines(): riga = riga.replace('"', '') riga = riga.split(";") if PrimaRiga: testata = riga PrimaRiga = False else: if import_data['tipo_file'] == 'T': #import pdb;pdb.set_trace() #print riga # il file deve aggiungere o aggiornare su tutti i template le relative tabelle varianti TemplateIds = self.pool.get('product.template').search(cr, uid, []) if TemplateIds: for Template in TemplateIds: # cerca l'esistenza della dimensione #import pdb;pdb.set_trace() idDimensione = self.Check_Dimension(cr, uid, Template, riga, context) param = [('name', '=', riga[2]), ('product_tmpl_id', "=", Template), ('dimension_id', '=', idDimensione[0])] idDimensionValore = self.pool.get("product.variant.dimension.value").search(cr, uid, param) if not idDimensionValore: # non trovato inserisco valore = { 'name':riga[2], 'product_tmpl_id':Template, 'dimension_id':idDimensione[0], 'price_extra':riga[3].replace(',', '.'), } #import pdb;pdb.set_trace() idDimensionValore = self.pool.get("product.variant.dimension.value").create(cr, uid, valore) else: #aggiorno il prezzo valore = { 'price_extra':riga[3].replace(',', '.'), } #import pdb;pdb.set_trace() ok = self.pool.get("product.variant.dimension.value").write(cr, uid, idDimensionValore, valore) if import_data['tipo_file'] == 'C': for colonna in range(3, len(riga) + 1): #cicla su tutte le colonne che ci sono nel csv param = [('name', 'ilike', testata[colonna] + "%")] category_id = self.pool.get('product.category'), search(cr, uid, param) if category_id: param = ['categ_id', "=", category_id[0]] TemplateIds = self.pool.get('product.template').search(cr, uid, param) if TemplateIds: # presi tutti i template che appartengono alla categoria for Template in TemplateIds: idDimensione = self.Check_Dimension(cr, uid, Template, riga, context) param = [('name', '=', riga[2]), ('product_tmpl_id', "=", Template), ('dimension_id', '=', idDimensione[0])] idDimensionValore = self.pool.get("product.variant.dimension.value").search(cr, uid, param) if not idDimensionValore: # non trovato inserisco valore = { 'name':riga[2], 'product_tmpl_id':Template, 'dimension_id':idDimensione[0], 'price_extra':riga[colonna].replace(',', '.'), } idDimensionValore = self.pool.get("product.variant.dimension.value").create(cr, uid, valore) else: #aggiorno il prezzo valore = { 'price_extra':riga[colonna].replace(',', '.'), } ok = self.pool.get("product.variant.dimension.value").write(cr, uid, idDimensionValore, valore) if import_data['tipo_file'] == 'M': for colonna in range(3, len(riga) + 1): #cicla su tutte le colonne che ci sono nel csv param = ['codice', "=", testata[colonna]] TemplateIds = self.pool.get('product.template').search(cr, uid, param) if TemplateIds: #trovato il template che ci interessa Template = TemplateIds[0] idDimensione = self.Check_Dimension(cr, uid, Template, riga, context) param = [('name', '=', riga[2]), ('product_tmpl_id', "=", Template), ('dimension_id', '=', idDimensione[0])] idDimensionValore = self.pool.get("product.variant.dimension.value").search(cr, uid, param) if not idDimensionValore: # non trovato inserisco valore = { 'name':riga[2], 'product_tmpl_id':Template, 'dimension_id':idDimensione[0], 'price_extra':riga[colonna].replace(',', '.'), } idDimensionValore = self.pool.get("product.variant.dimension.value").create(cr, uid, valore) else: #aggiorno il prezzo valore = { 'price_extra':riga[colonna].replace(',', '.'), } ok = self.pool.get("product.variant.dimension.value").write(cr, uid, idDimensionValore, valore) import pdb;pdb.set_trace() fileobj.close() return {}
class S3File(io.IOBase): """File like proxy for s3 files, manages upload and download of locally managed temporary file """ def __init__(self, bucket, key, mode='w+b', *args, **kwargs): super(S3File, self).__init__(*args, **kwargs) self.bucket = bucket self.key = key self.mode = mode self.path = self.bucket + '/' + self.key # converts mode to readable/writable to enable the temporary file to have S3 data # read or written to it even if the S3File is read/write/append # i.e. "r" => "r+", "ab" => "a+b" updatable_mode = re.sub(r'^([rwa]+)(b?)$', r'\1+\2', mode) self._tempfile = TemporaryFile(updatable_mode) try: with s3errors(self.path): if 'a' in mode: # File is in an appending mode, start with the content in file s3.Object(bucket, key).download_fileobj(self._tempfile) self.seek(0, os.SEEK_END) elif 'a' not in mode and 'w' not in mode and 'x' not in mode: # file is not in a create mode, so it is in read mode # start with the content in the file, and seek to the beginning s3.Object(bucket, key).download_fileobj(self._tempfile) self.seek(0, os.SEEK_SET) except Exception: self.close() raise def __enter__(self): return self def __exit__(self, exc_type, exc_val, exc_tb): self.close() def close(self): try: if self.writable(): self.seek(0) with s3errors(self.path): s3.Object(self.bucket, self.key).upload_fileobj(self._tempfile) finally: self._tempfile.close() @property def closed(self): return self._tempfile.closed def fileno(self): return self._tempfile.fileno() def flush(self): return self._tempfile.flush() def isatty(self): return self._tempfile.isatty() def readable(self): return 'r' in self.mode or '+' in self.mode def read(self, n=-1): if not self.readable(): raise IOError('not open for reading') return self._tempfile.read(n) def readinto(self, b): return self._tempfile.readinto(b) def readline(self, limit=-1): if not self.readable(): raise IOError('not open for reading') return self._tempfile.readline(limit) def readlines(self, hint=-1): if not self.readable(): raise IOError('not open for reading') return self._tempfile.readlines(hint) def seek(self, offset, whence=os.SEEK_SET): self._tempfile.seek(offset, whence) return self.tell() def seekable(self): return True def tell(self): return self._tempfile.tell() def writable(self): return 'w' in self.mode or 'a' in self.mode or '+' in self.mode or 'x' in self.mode def write(self, b): if not self.writable(): raise IOError('not open for writing') self._tempfile.write(b) return len(b) def writelines(self, lines): if not self.writable(): raise IOError('not open for writing') return self._tempfile.writelines(lines) def truncate(self, size=None): if not self.writable(): raise IOError('not open for writing') if size is None: size = self.tell() self._tempfile.truncate(size) return size
def import_prezzi_func(self, cr, uid, ids, context=None): import_data = self.browse(cr, uid, ids)[0] cerca =[] fileobj = TemporaryFile('w+') fileobj.write(base64.decodestring(import_data.data)) fileobj.seek(0) testo_log = """Inizio procedura di Importazione Prezzi Speciali """ + time.ctime() + '\n' error = False nome ='' for riga in fileobj.readlines(): nome ='' #import pdb;pdb.set_trace() riga = riga.replace('"', '') riga = riga.replace('\n', '') riga = riga.replace(',', '.') riga = riga.split(";") error = False cli_id = False art_id =False categ_id = False cli_id = self.pool.get('res.partner').search(cr,uid,[('ref','=',riga[0]),('customer','=',1)]) if not cli_id: testo_log = testo_log + " Cliente " + riga[0] + " NON TROVATO \n" error = True else: cerca += [('partner_id','=',cli_id[0])] nome += 'cli' + riga[0] if riga[1]: # c'è un codice articolo ad-hoc art_id = self.pool.get('product.product').search(cr,uid,[('adhoc_code','=',riga[1])]) if not art_id: testo_log = testo_log + " Articolo Ad-Hoc " + riga[1] + " NON TROVATO \n" error = True else: cerca += [('partner_id','=',art_id[0])] nome += 'art '+ riga[1] if riga[2]: # c'è un articolo openerp art_id = self.pool.get('product.product').search(cr,uid,[('default_code','=',riga[2])]) if not art_id: testo_log = testo_log + " Articolo OpenErp " + riga[1] + " NON TROVATO \n" error = True else: cerca += [('partner_id','=',art_id[0])] nome += 'art '+ riga[2] if riga[3]: # c'è una categoria categ_id = self.pool.get('product.category').search(cr,uid,[('name','ilike',riga[3])]) if not categ_id: testo_log = testo_log + " Categoria " + riga[3] + " NON TROVATO \n" error = True else: cerca += [('partner_id','=',categ_id[0])] nome += 'cat '+ riga[3] if not error: if len(cerca): prezzo_id = self.pool.get('product.pricelist.item').search(cr,uid,cerca) if prezzo_id: # ho trovato una riga quind deve fare solo aggiornamenti prezzo = {} if riga[5]: # riga[5] = riga.replace(',', '.') prezzo.update({'price_surcharge':riga[5]}) if riga[4]: riga[4]=riga[4]/100 prezzo.update({'price_discount':riga[4]}) ok = self.pool.get('product.pricelist.item').wite(cr,uid,prezzo_id,prezzo) else: # Scrive un solo record prezzo = {} prezzo.update({'name':nome}) if cli_id: prezzo.update({'partner_id':cli_id[0]}) ver_listino_id = self.pool.get('res.partner').browse(cr,uid,cli_id[0]).property_product_pricelist #import pdb;pdb.set_trace() # version_id[0].id presa la prima riga della versione listino ver_listino_id.version_id[0].id if ver_listino_id: ver_listino_id= ver_listino_id.version_id[0].id prezzo.update({'price_version_id':ver_listino_id}) prezzo.update({'base':1}) if riga[5]: prezzo.update({'price_surcharge':riga[5]}) if riga[4]: riga[4]=float(riga[4])/100 prezzo.update({'price_discount':riga[4]}) if art_id: prezzo.update({'product_id':art_id[0]}) if categ_id: prezzo.update({'categ_id':categ_id[0]}) prezzo_id = self.pool.get('product.pricelist.item').create(cr,uid,prezzo) testo_log = testo_log + " Operazione Teminata alle " + time.ctime() + "\n" #invia e-mail type_ = 'plain' tools.email_send('*****@*****.**', ['*****@*****.**','*****@*****.**'], 'Esito Importazione Prezzi Articoli', testo_log, subtype=type_, ) return {'type': 'ir.actions.act_window_close'}
from tempfile import TemporaryFile f = TemporaryFile() f.write("Hello World") f.seek(0) print(f.readlines()) f.close() $ python example.py ['Hello World'] <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-4058794840952844" data-ad-slot="4835580402" data-ad-format="auto"> (adsbygoogle = window.adsbygoogle || []).push({});
class TestG(unittest.TestCase): def setUp(self): self.outfile = TemporaryFile() self.g = G(outfile=self.outfile, print_lines=False, aerotech_include=False) self.expected = "" if self.g.is_relative: self.expect_cmd('G91') else: self.expect_cmd('G90') def tearDown(self): self.g.teardown() del self.outfile del self.g def test_init(self): self.assertEqual(self.g.is_relative, True) def test_set_home(self): g = self.g g.set_home() self.expect_cmd('G92') self.assert_output() g.set_home(x=10, y=20, A=5) self.expect_cmd('G92 X10.000000 Y20.000000 A5.000000') self.assert_output() self.assert_position({'A': 5.0, 'x': 10.0, 'y': 20.0, 'z': 0}) g.set_home(y=0) self.assert_position({'A': 5.0, 'x': 10.0, 'y': 0.0, 'z': 0}) def test_reset_home(self): self.g.reset_home() self.expect_cmd('G92.1') self.assert_output() def test_relative(self): self.assertEqual(self.g.is_relative, True) self.g.absolute() self.expect_cmd('G90') self.g.relative() self.assertEqual(self.g.is_relative, True) self.expect_cmd('G91') self.assert_output() self.g.relative() self.assertEqual(self.g.is_relative, True) self.assert_output() def test_absolute(self): self.g.absolute() self.assertEqual(self.g.is_relative, False) self.expect_cmd('G90') self.assert_output() self.g.absolute() self.assertEqual(self.g.is_relative, False) self.assert_output() def test_feed(self): self.g.feed(10) self.expect_cmd('F10') self.assert_output() def test_dwell(self): self.g.dwell(10) self.expect_cmd('G4 P10') self.assert_output() def test_setup(self): self.outfile = TemporaryFile() self.g = G(outfile=self.outfile, print_lines=False) self.expected = "" self.expect_cmd(open(os.path.join(HERE, '../header.txt')).read()) self.expect_cmd('G91') self.assert_output() def test_home(self): self.g.home() self.expect_cmd(""" G90 G1 X0.000000 Y0.000000 G91 """) self.assert_output() self.assert_position({'x': 0, 'y': 0, 'z': 0}) def test_move(self): g = self.g g.move(10, 10) self.assert_position({'x': 10.0, 'y': 10.0, 'z': 0}) g.move(10, 10, A=50) self.assert_position({'x': 20.0, 'y': 20.0, 'A': 50, 'z': 0}) self.expect_cmd(""" G1 X10.000000 Y10.000000 G1 X10.000000 Y10.000000 A50.000000 """) self.assert_output() def test_abs_move(self): self.g.relative() self.g.abs_move(10, 10) self.expect_cmd(""" G90 G1 X10.000000 Y10.000000 G91 """) self.assert_output() self.assert_position({'x': 10, 'y': 10, 'z': 0}) self.g.abs_move(5, 5) self.expect_cmd(""" G90 G1 X5.000000 Y5.000000 G91 """) self.assert_output() self.assert_position({'x': 5, 'y': 5, 'z': 0}) self.g.abs_move(15, 0, D=5) self.expect_cmd(""" G90 G1 X15.000000 Y0.000000 D5.000000 G91 """) self.assert_output() self.assert_position({'x': 15, 'y': 0, 'D': 5, 'z': 0}) self.g.absolute() self.g.abs_move(19, 18, D=6) self.expect_cmd(""" G90 G1 X19.000000 Y18.000000 D6.000000 """) self.assert_output() self.assert_position({'x': 19, 'y': 18, 'D': 6, 'z': 0}) self.g.relative() def test_arc(self): with self.assertRaises(RuntimeError): self.g.arc() self.g.arc(x=10, y=0) self.expect_cmd(""" G17 G2 X10 Y0 R5.0 """) self.assert_output() self.assert_position({'x': 10, 'y': 0, 'z': 0}) self.g.arc(x=5, A=0, direction='CCW', radius=5) self.expect_cmd(""" G16 X Y A G18 G3 A0 X5 R5 """) self.assert_output() self.assert_position({'x': 15, 'y': 0, 'A': 0, 'z': 0}) self.g.arc(x=0, y=10, helix_dim='D', helix_len=10) self.expect_cmd(""" G16 X Y D G17 G2 X0 Y10 R5.0 G1 D10 """) self.assert_output() self.assert_position({'x': 15, 'y': 10, 'A': 0, 'D': 10, 'z': 0}) with self.assertRaises(RuntimeError): self.g.arc(x=10, y=10, radius=1) def test_abs_arc(self): self.g.relative() self.g.abs_arc(x=0, y=10) self.expect_cmd(""" G90 G17 G2 X0 Y10 R5.0 G91 """) self.assert_output() self.assert_position({'x': 0, 'y': 10, 'z': 0}) self.g.abs_arc(x=0, y=10) self.expect_cmd(""" G90 G17 G2 X0 Y10 R0.0 G91 """) self.assert_output() self.assert_position({'x': 0, 'y': 10, 'z': 0}) self.g.absolute() self.g.abs_arc(x=0, y=20) self.expect_cmd(""" G90 G17 G2 X0 Y20 R5.0 """) self.assert_output() self.assert_position({'x': 0, 'y': 20, 'z': 0}) self.g.relative() def test_rect(self): self.g.rect(10, 5) self.expect_cmd(""" G1 Y5.000000 G1 X10.000000 G1 Y-5.000000 G1 X-10.000000 """) self.assert_output() self.assert_position({'x': 0, 'y': 0, 'z': 0}) self.g.rect(10, 5, start='UL') self.expect_cmd(""" G1 X10.000000 G1 Y-5.000000 G1 X-10.000000 G1 Y5.000000 """) self.assert_output() self.assert_position({'x': 0, 'y': 0, 'z': 0}) self.g.rect(10, 5, start='UR') self.expect_cmd(""" G1 Y-5.000000 G1 X-10.000000 G1 Y5.000000 G1 X10.000000 """) self.assert_output() self.assert_position({'x': 0, 'y': 0, 'z': 0}) self.g.rect(10, 5, start='LR') self.expect_cmd(""" G1 X-10.000000 G1 Y5.000000 G1 X10.000000 G1 Y-5.000000 """) self.assert_output() self.assert_position({'x': 0, 'y': 0, 'z': 0}) self.g.rect(10, 5, start='LL', direction='CCW') self.expect_cmd(""" G1 X10.000000 G1 Y5.000000 G1 X-10.000000 G1 Y-5.000000 """) self.assert_output() self.assert_position({'x': 0, 'y': 0, 'z': 0}) self.g.rect(10, 5, start='UL', direction='CCW') self.expect_cmd(""" G1 Y-5.000000 G1 X10.000000 G1 Y5.000000 G1 X-10.000000 """) self.assert_output() self.assert_position({'x': 0, 'y': 0, 'z': 0}) self.g.rect(10, 5, start='UR', direction='CCW') self.expect_cmd(""" G1 X-10.000000 G1 Y-5.000000 G1 X10.000000 G1 Y5.000000 """) self.assert_output() self.assert_position({'x': 0, 'y': 0, 'z': 0}) self.g.rect(10, 5, start='LR', direction='CCW') self.expect_cmd(""" G1 Y5.000000 G1 X-10.000000 G1 Y-5.000000 G1 X10.000000 """) self.assert_output() self.assert_position({'x': 0, 'y': 0, 'z': 0}) def test_meander(self): self.g.meander(2, 2, 1) self.expect_cmd(""" G1 X2.000000 G1 Y1.000000 G1 X-2.000000 G1 Y1.000000 G1 X2.000000 """) self.assert_output() self.assert_position({'x': 2, 'y': 2, 'z': 0}) self.g.meander(2, 2, 1.1) self.expect_cmd(""" ;WARNING! meander spacing updated from 1.1 to 1.0 G1 X2.000000 G1 Y1.000000 G1 X-2.000000 G1 Y1.000000 G1 X2.000000 """) self.assert_output() self.assert_position({'x': 4, 'y': 4, 'z': 0}) self.g.meander(2, 2, 1, start='UL') self.expect_cmd(""" G1 X2.000000 G1 Y-1.000000 G1 X-2.000000 G1 Y-1.000000 G1 X2.000000 """) self.assert_output() self.assert_position({'x': 6, 'y': 2, 'z': 0}) self.g.meander(2, 2, 1, start='UR') self.expect_cmd(""" G1 X-2.000000 G1 Y-1.000000 G1 X2.000000 G1 Y-1.000000 G1 X-2.000000 """) self.assert_output() self.assert_position({'x': 4, 'y': 0, 'z': 0}) self.g.meander(2, 2, 1, start='LR') self.expect_cmd(""" G1 X-2.000000 G1 Y1.000000 G1 X2.000000 G1 Y1.000000 G1 X-2.000000 """) self.assert_output() self.assert_position({'x': 2, 'y': 2, 'z': 0}) self.g.meander(2, 2, 1, start='LR', orientation='y') self.expect_cmd(""" G1 Y2.000000 G1 X-1.000000 G1 Y-2.000000 G1 X-1.000000 G1 Y2.000000 """) self.assert_output() self.assert_position({'x': 0, 'y': 4, 'z': 0}) self.g.meander(3, 2, 1, start='LR', orientation='y') self.expect_cmd(""" G1 Y2.000000 G1 X-1.000000 G1 Y-2.000000 G1 X-1.000000 G1 Y2.000000 G1 X-1.000000 G1 Y-2.000000 """) self.assert_output() self.assert_position({'x': -3, 'y': 4, 'z': 0}) def test_clip(self): self.g.clip() self.expect_cmd(""" G16 X Y Z G18 G3 X0 Z4 R2.0 """) self.assert_output() self.assert_position({'y': 0, 'x': 0, 'z': 4}) self.g.clip(axis='A', direction='-y', height=10) self.expect_cmd(""" G16 X Y A G19 G2 A10 Y0 R5.0 """) self.assert_output() self.assert_position({'x': 0, 'y': 0, 'z': 4, 'A': 10}) self.g.clip(axis='A', direction='-y', height=-10) self.expect_cmd(""" G16 X Y A G19 G3 A-10 Y0 R5.0 """) self.assert_output() self.assert_position({'x': 0, 'y': 0, 'z': 4, 'A': 0}) def test_toggle_pressure(self): self.g.toggle_pressure(0) self.expect_cmd('Call togglePress P0') self.assert_output() def test_align_nozzle(self): self.g.align_nozzle('A') self.expect_cmd('Call alignNozzle Q-15 R0.1 L1 I-72 J1') self.assert_output() with self.assertRaises(RuntimeError): self.g.align_nozzle('F') def test_align_zero_nozzle(self): self.g.align_zero_nozzle('A') self.expect_cmd('Call alignZeroNozzle Q-15 R0.1 L1 I-72 J1') self.assert_output() with self.assertRaises(RuntimeError): self.g.align_zero_nozzle('F') def test_set_pressure(self): self.g.set_pressure(0, 10) self.expect_cmd('Call setPress P0 Q10') self.assert_output() def test_set_valve(self): self.g.set_valve(0, 1) self.expect_cmd('$DO0.0=1') self.assert_output() def test_save_alignment(self): self.g.save_alignment() self.expect_cmd('Call save_value Q1') self.assert_output() # helper functions ####################################################### def expect_cmd(self, cmd): self.expected = self.expected + cmd + '\n' def assert_output(self): string_rep = "" if is_str(self.expected): string_rep = self.expected self.expected = self.expected.split('\n') self.expected = [x.strip() for x in self.expected if x.strip()] self.outfile.seek(0) lines = self.outfile.readlines() lines = [decode2To3(x).strip() for x in lines if x.strip()] self.assertListEqual(lines, self.expected) self.expected = string_rep def assert_position(self, expected_pos): self.assertEqual(self.g.current_position, expected_pos)
def process_qif(self, cr, uid, data_file, journal_id=False, context=None): """ Import a file in the .QIF format""" try: fileobj = TemporaryFile('wb+') fileobj.write(base64.b64decode(data_file)) fileobj.seek(0) file_data = "" for line in fileobj.readlines(): file_data += line fileobj.close() if '\r' in file_data: data_list = file_data.split('\r') else: data_list = file_data.split('\n') header = data_list[0].strip() header = header.split(":")[1] except: raise osv.except_osv( _('Import Error!'), _('Please check QIF file format is proper or not.')) line_ids = [] vals_line = {} total = 0 if header == "Bank": vals_bank_statement = {} for line in data_list: line = line.strip() if not line: continue if line[0] == 'D': # date of transaction vals_line['date'] = dateutil.parser.parse( line[1:], fuzzy=True).date() if vals_line.get('date') and not vals_bank_statement.get( 'period_id'): period_ids = self.pool.get('account.period').find( cr, uid, vals_line['date'], context=context) vals_bank_statement.update({ 'period_id': period_ids and period_ids[0] or False }) elif line[0] == 'T': # Total amount total += float(line[1:].replace(',', '')) vals_line['amount'] = float(line[1:].replace(',', '')) elif line[0] == 'N': # Check number vals_line['ref'] = line[1:] elif line[0] == 'P': # Payee bank_account_id, partner_id = self._detect_partner( cr, uid, line[1:], identifying_field='owner_name', context=context) vals_line['partner_id'] = partner_id vals_line['bank_account_id'] = bank_account_id vals_line['name'] = 'name' in vals_line and line[ 1:] + ': ' + vals_line['name'] or line[1:] elif line[0] == 'M': # Memo vals_line['name'] = 'name' in vals_line and vals_line[ 'name'] + ': ' + line[1:] or line[1:] elif line[0] == '^': # end of item line_ids.append((0, 0, vals_line)) vals_line = {} elif line[0] == '\n': line_ids = [] else: pass else: raise osv.except_osv( _('Error!'), _('Cannot support this Format !Type:%s.') % (header, )) vals_bank_statement.update({ 'balance_end_real': total, 'line_ids': line_ids, 'journal_id': journal_id }) return [vals_bank_statement]
class MonitorSession: class States: initial = 'initial' running = 'running' complete = 'complete' @property def badge(self): num_problems = sum(len(lines) for lines in self.problem_lines.values()) return self.config.badge_for_number(num_problems) @property def dirpath(self): return os.path.join(STATE_DIR, 'monitors', self.config.name) @property def problem_lines_filepath(self): return os.path.join(self.dirpath, 'problem_lines') def __init__(self, config, files): self.state = self.States.initial self.config = config self.files = files self.process = None self.output_file = None self.problem_lines = None self.initial_problem_lines = gb( self._read_lines_filepath(self.problem_lines_filepath), self.config.extract_file_from_problem_line, ) log.debug('%s initial problem lines %s', self, self.initial_problem_lines) def start(self): assert self.state == self.States.initial if len(self.files) == 0: self.skip() return files = [f for f in self.files if os.path.exists(f)] if len(files) == 0: # all files were deleted, so mark as clear self.problem_lines = {f: [] for f in self.files} self.state = self.States.complete return self.state = self.States.running log.debug('Starting %s on %d files', self.config.command, len(files)) self.output_file = TemporaryFile(mode='w+') try: self.process = Popen( [*self.config.command, *files], stdout=self.output_file, stderr=self.output_file, ) except Exception as exc: self.problem_lines = {'.': [str(exc).replace('\n', ' ')]} self.process = None def join(self): if self.state == self.States.complete: return assert self.state == self.States.running if self.process is None: self.state = self.States.complete return self.process.communicate() self.output_file.flush() self.output_file.seek(0) olines = self.output_file.readlines() log.debug('%s output lines: %s', self, olines) self.problem_lines = gb( (line.strip() for line in olines), self.config.extract_file_from_problem_line, ) # make sure we detect removal of problems by setting empty arrays for files that we # processed but didn't get output for for file in self.files: self.problem_lines.setdefault(file, []) self.output_file.close() self.output_file = None self.process = None self.state = self.States.complete def skip(self): # don't bother running, just use initial values as end values assert self.state == self.States.initial self.problem_lines = self.initial_problem_lines self.state = self.States.complete def save(self): # only able to save once it has been joined assert self.state == self.States.complete assert self.problem_lines is not None new_problem_lines_by_file = { **self.initial_problem_lines, **self.problem_lines, } log.debug('%s new_problem_lines_by_file %s', self, new_problem_lines_by_file) problem_line_diff = diff_problem_lines(self.initial_problem_lines, new_problem_lines_by_file) if len(problem_line_diff) == 0: log.debug('No change, no need to save %s', self) return log.info('Changes in %s:', self) for diff_entry in problem_line_diff: log.info(' %s %s', diff_entry[0], diff_entry[2]) expanded_sorted_lines = [ line for file, lines in sorted(new_problem_lines_by_file.items(), key=lambda k_v: k_v[0] or '') for line in lines ] self._write_lines_filepath(self.problem_lines_filepath, expanded_sorted_lines) def __str__(self): return self.config.name def _read_lines_filepath(self, filepath): if not os.path.exists(filepath): return [] with open(filepath) as file: return [ self.config.normalize_path(line) for line in file.readlines() ] def _write_lines_filepath(self, filepath, problem_lines): os.makedirs(os.path.dirname(filepath), exist_ok=True) with open(filepath, 'w') as file: for line in problem_lines: print(line, file=file)
def _worker (self, task, task_queue): """ Executes AnyBody console application on the task object """ # task = task[0] process_number = self.counter self.counter += 1 if not os.path.exists(task.folder): raise IOError('Unable to find folder: ' + task.folder) macrofile = NamedTemporaryFile(mode='w+b', prefix ='{}'.format(self.logfile_prefix+'_'), suffix='.anymcr', dir = task.folder, delete = False) macrofile.write( '\n'.join(task.macro).encode('UTF-8') ) macrofile.flush() anybodycmd = [os.path.realpath(self.anybodycon_path), '--macro=', macrofile.name, '/ni'] tmplogfile = TemporaryFile() proc = Popen(anybodycmd, stdout=tmplogfile, stderr=tmplogfile,shell= False) _pids.add(proc.pid) starttime = time.clock() timeout =starttime + self.timeout while proc.poll() is None: if time.clock() > timeout: proc.terminate() proc.communicate() tmplogfile.seek(0,2) tmplogfile.write('ERROR: Timeout. Terminate by batch processor'.encode('UTF-8') ) break time.sleep(0.3) else: if proc.pid in _pids: _pids.remove(proc.pid) processtime = round(time.clock()-starttime, 2) tmplogfile.seek(0) rawoutput = "\n".join( s.decode('UTF-8') for s in tmplogfile.readlines() ) tmplogfile.close() macrofile.close() output = _parse_anybodycon_output(rawoutput) # Remove any ERRORs which should be ignored if 'ERROR' in output: def check_error(error_string): return all( [(err not in error_string) for err in self.ignore_errors]) output['ERROR'][:] = [err for err in output['ERROR'] if check_error(err)] if not output['ERROR']: del output['ERROR'] logfile_path = None if self.keep_logfiles or 'ERROR' in output: with open(os.path.splitext(macrofile.name)[0]+'.log','w+b') as logfile: logfile.write(rawoutput.encode('UTF-8')) logfile_path = logfile.name else: try: os.remove(macrofile.name) except: print( 'Error removing macro file') task.processtime = processtime task.logfile = logfile_path task.error = 'ERROR' in output task.process_number = process_number task.output = output task_queue.put(task)