Exemple #1
0
def create(table_name, fields, check_ex):
    table_path = table_name
    if os.path.exists(table_path):
        if check_ex:
            return
        else:
            raise ValueError("table already exists")
    os.makedirs(table_path)
    # print(table_name, fields, check_ex)
    schema = open(os.path.join(table_path, "table.json"), "w+")
    # print(fields)
    l = []
    for x, y in fields:
        os.makedirs(os.path.join(table_name, x))
        l.append({"field": x, "type": y})
    out = {
        'schema': l,
        'file_sizes': consts.FILE_SIZES,
        'file_num': 0,
        'last_i': 0
    }
    # print(out)
    json.dump(out, schema, indent=True)
    schema.close()
    ZipManager.zipTable(table_name)
Exemple #2
0
	def GetModule(filename):
		""" Give module object from python file path. Warning, the name of python_file must be the same of the classe name.
		"""

		dir_name = os.path.dirname(filename)

		### if python_file is ...../toto.amd/Atomic_Model.py, then the parent dir is zipfile.
		if zipfile.is_zipfile(dir_name):
			zf = ZipManager.Zip(dir_name)
			return zf.GetModule()
		elif zipfile.is_zipfile(filename):
			zf = ZipManager.Zip(filename)
			return zf.GetModule()
		### if python file is on the web !
		elif filename.startswith(('http','https')):
			net = Net(filename)
			return net.GetModule()
		### pure python file
		else:

			### add path to sys.path
			if dir_name not in sys.path:
				sys.path.append(dir_name)

			module_name = os.path.basename(filename).split('.py')[0]

			# find and load module
			try:
				f, fn, description = imp.find_module(module_name, [dir_name])
				module = imp.load_module(module_name, f, fn, description)
				f.close()
				return module

			except Exception, info:
				return sys.exc_info()
Exemple #3
0
def dex2jar(f):
    # 如果操作的是apk,则将apk中全部的dex转换为jar文件输出到在apk同级目录下
    if f.endswith('.apk'):
        output_jar_prefix = os.path.splitext(f)[0] + '_'
        temp_dex_dir = PathManager.get_temp_dir_path()
        ZipManager.unzip_dexFile_to_dest(f, temp_dex_dir)
        filenames = os.listdir(temp_dex_dir)
        for dex_name in filenames:
            if dex_name.endswith('.dex'):
                dex_file_path = os.path.join(temp_dex_dir, dex_name)
                jar_file = output_jar_prefix + os.path.splitext(
                    dex_name)[0] + '.jar'
                retcode, msg = star.runcmd2([
                    PathManager.get_dex2jar_path(), dex_file_path, '-f', '-o',
                    jar_file
                ])
                if retcode == 0:
                    star.run_cmd_asyn([PathManager.get_jdgui_path(), jar_file])
                    print('dex2jar ok')
        shutil.rmtree(temp_dex_dir)
        return retcode, msg
    elif f.endswith('.dex'):
        jarFile = os.path.splitext(f)[0] + '.jar'
        retcode, msg = star.runcmd2(
            [PathManager.get_dex2jar_path(), f, '-f', '-o', jarFile])
        if retcode == 0:
            star.run_cmd_asyn([PathManager.get_jdgui_path(), jarFile])
            print('dex2jar ok')
        return retcode, msg
    else:
        print(u'选择文件不合法,文件格式需要是apk或者dex格式')
        os.system("pause")
        return 0, None
Exemple #4
0
    def __init__(self, table_name):
        ZipManager.unzipTable(table_name)
        scheme_path = os.path.join(table_name, "table.json")
        if not os.path.isfile(scheme_path):
            raise FileNotFoundError(
                "Table '{}' does not exists".format(table_name))

        schema_file = open(scheme_path, "r")
        full_file = json.load(schema_file)
        schema_file.close()

        self.line_batches = full_file["file_sizes"]
        self.file_num = full_file["file_num"] + 1
        self.last_i = full_file["last_i"]
        consts.FILE_SIZES = self.line_batches
        schema = full_file['schema']
        # define local objects that help us get through the rough command ;-)

        self.name = table_name
        self.field_names = []
        self.name2type = {}
        for x in schema:
            field_name = x["field"]
            field_type = x["type"]
            self.field_names.append(field_name)
            self.name2type[field_name] = field_type
Exemple #5
0
    def Load(filename, label):
        """ Load AMD from constructor.
		"""

        assert (filename.endswith('.amd'))

        python_path = os.path.join(filename,
                                   ZipManager.getPythonModelFileName(filename))

        cls = GetClass(python_path)

        m = AMDComponent.BlockModelAdapter(cls, label)

        load_file_result = m.LoadFile(filename)

        if isinstance(load_file_result, Exception):
            wx.MessageBox(
                _('Error loading %s model : %s ' % (label, load_file_result)),
                _('Error'), wx.OK | wx.ICON_ERROR)
            return None
        else:
            ### mandatory due to the LoadFile call before
            m.label = label

            return AMDComponent.ChekFilename(filename, m)
Exemple #6
0
    def GetModule(filename):
        """ Give module object from python file path. Warning, the name of python_file must be the same of the classe name.
		"""

        dir_name = os.path.dirname(filename)

        ### if python_file is ...../toto.amd/Atomic_Model.py, then the parent dir is zipfile.
        if zipfile.is_zipfile(dir_name):
            zf = ZipManager.Zip(dir_name)
            return zf.GetModule()
        elif zipfile.is_zipfile(filename):
            zf = ZipManager.Zip(filename)
            return zf.GetModule()
        ### if python file is on the web !
        elif filename.startswith(('http', 'https')):
            net = Net(filename)
            return net.GetModule()
        ### pure python file
        else:

            ### add path to sys.path recursively
            current_dirname = dir_name
            while (current_dirname != os.path.dirname(current_dirname)):
                if current_dirname not in sys.path:
                    sys.path.append(current_dirname)
                current_dirname = os.path.dirname(current_dirname)

            module_name = os.path.basename(filename).split('.py')[0]

            # find and load module
            try:
                #name, ext = os.path.splitext(module_name)
                #pkg = '.'.join(modulename.split('.')[0:-1])
                #module = importlib.import_module(name, package=pkg)

                f, fn, description = imp.find_module(module_name, [dir_name])
                module = imp.load_module(module_name, f, fn, description)
                f.close()
                return module

            except Exception as info:
                sys.stderr.write(
                    _("Module %s not imported from %s!\n" %
                      (module_name, dir_name)))
                return sys.exc_info()
Exemple #7
0
def recompile(modulename):
    """	recompile module from modulename
	"""

    ### modulename is file type
    if os.path.isfile(modulename) and os.path.exists(modulename):
        import ZipManager
        zf = ZipManager.Zip(modulename)
        return zf.Recompile()
    else:

        try:
            ### first, see if the module can be imported at all...
            name, ext = os.path.splitext(modulename)
            pkg = '.'.join(modulename.split('.')[0:-1])
            tmp = importlib.import_module(name, package=pkg)
        #tmp = __import__(modulename, globals(), locals(), fromlist = [modulename.split('.')[-1]])

        except Exception as info:
            return info

        ### Use the imported module to determine its actual path
        pycfile = os.path.abspath(tmp.__file__)
        modulepath = pycfile.replace(
            '.pyc', '.py')  #string.replace(pycfile, ".pyc", ".py")

        ### Try to open the specified module as a file
        code = open(modulepath, 'rU').read()

        ### see if the file we opened can compile.  If not, return the error that it gives.
        ### if compile() fails, the module will not be replaced.
        try:
            compile(code, modulename, "exec")
        except:
            return "Error in compilation: " + str(
                sys.exc_info()[0]) + "\r\n" + listf(
                    format_exception(sys.exc_info()[0],
                                     sys.exc_info()[1],
                                     sys.exc_info()[2]))
        else:

            ### Ok, it compiled.  But will it execute without error?
            try:
                exec(compile(open(modulepath).read(), modulepath, 'exec'),
                     globals())
            except Exception:
                #return "Error '%s' happened on line %d" % (info[0], info[1][1])
                return "Error in execution: " + str(
                    sys.exc_info()[0]) + "\r\n" + listf(
                        format_exception(sys.exc_info()[0],
                                         sys.exc_info()[1],
                                         sys.exc_info()[2]))

            else:
                ### at this point, the code both compiled and ran without error.  Load it up
                ### replacing the original code.
                return importlib.reload(tmp)  #sys.modules[modulename])
Exemple #8
0
    def __init__(self, **kwargs):
        """ Constructor.
		"""

        self.model = kwargs.pop('model')

        wx.Frame.__init__(self, **kwargs)

        ### plug-in panel
        self.pluginPanel = PluginsPanel(self)

        ### two panel into plug-in panel
        rpanel = self.pluginPanel.GetRightPanel()
        lpanel = self.pluginPanel.GetLeftPanel()

        ### checklist to insert in right panel
        self.CheckList = BlockPluginsList(rpanel)
        wx.CallAfter(self.CheckList.Populate, (self.model))

        ### Buttons for insert or delete plug-ins
        self.addBtn = wx.Button(lpanel, wx.ID_ADD, size=(140, -1))
        self.delBtn = wx.Button(lpanel, wx.ID_DELETE, size=(140, -1))
        self.editBtn = wx.Button(lpanel, wx.ID_EDIT, size=(140, -1))
        self.updateBtn = wx.Button(lpanel, wx.ID_APPLY, size=(140, -1))
        self.addBtn.SetToolTipString(_("Add new plug-ins"))
        self.delBtn.SetToolTipString(_("Delete all existing plug-ins"))
        self.editBtn.SetToolTipString(_("Edit plug-in file"))
        self.updateBtn.SetToolTipString(_("Update plug-in list"))

        ### add widget to plug-in panel
        self.pluginPanel.SetPluginsList(self.CheckList)
        self.pluginPanel.AddWidget(3, self.addBtn)
        self.pluginPanel.AddWidget(4, self.editBtn)
        self.pluginPanel.AddWidget(5, self.updateBtn)
        self.pluginPanel.AddWidget(6, self.delBtn)

        #### zipfile (amd or cmd)
        try:
            self.zf = ZipManager.Zip(self.model.model_path)
            cond = ZipManager.Zip.HasPlugin(self.model.model_path)
        except AttributeError:
            sys.stdout.write(_('PluginsGUI in mode alone.\n'))
            cond = False

        ### enable del, add and update buttons
        self.delBtn.Enable(cond)
        self.addBtn.Enable(not cond)
        self.editBtn.Enable(cond)
        self.updateBtn.Enable(False)

        self.Bind(wx.EVT_BUTTON, self.OnAdd, id=self.addBtn.GetId())
        self.Bind(wx.EVT_BUTTON, self.OnDelete, id=self.delBtn.GetId())
        self.Bind(wx.EVT_BUTTON, self.OnEdit, id=self.editBtn.GetId())
        self.Bind(wx.EVT_BUTTON, self.OnRefresh, id=self.updateBtn.GetId())

        self.CenterOnParent(wx.BOTH)
        self.Layout()
def recompile2(modulename):
    """	recompile module from modulename
	"""

    ### modulename is file type
    if os.path.isfile(modulename) and os.path.exists(modulename):
        import ZipManager
        zf = ZipManager.Zip(modulename)
        return zf.Recompile()
    else:

        try:
            #name, ext = os.path.splitext(modulename)
            #pkg = '.'.join(modulename.split('.')[0:-1])
            #tmp = importlib.import_module(name, package=pkg)

            ### first, see if the module can be imported at all...
            tmp = __import__(modulename,
                             globals(),
                             locals(),
                             fromlist=[modulename.split('.')[-1]])

        except Exception as info:
            return info

        ### Use the imported module to determine its actual path
        pycfile = os.path.abspath(tmp.__file__)
        modulepath = pycfile.replace(".pyc", ".py")

        ### Try to open the specified module as a file
        code = open(modulepath, 'rU').read()

        ### see if the file we opened can compile.  If not, return the error that it gives.
        ### if compile() fails, the module will not be replaced.
        compile(code, modulename, "exec")

        ### Ok, it compiled.  But will it execute without error?
        exec(compile(open(modulepath).read(), modulepath, 'exec'), globals())

        reloadall(tmp)

        ### at this point, the code both compiled and ran without error.  Load it up
        ### replacing the original code.
        return importlib.reload(sys.modules[modulename])
	def Load(filename, label):
		""" Load AMD from constructor.
		"""

		assert(filename.endswith('.amd'))

		python_path = os.path.join(filename, ZipManager.getPythonModelFileName(filename))

		cls = GetClass(python_path)

		m = AMDComponent.BlockModelAdapter(cls, label)

		load_file_result = m.LoadFile(filename)

		if isinstance(load_file_result, Exception):
			wx.MessageBox(_('Error loading %s model : %s '%(label, load_file_result)), _('Error'), wx.OK | wx.ICON_ERROR)
			return None
		else:
			### mandatory due to the LoadFile call before
			m.label = label

			return AMDComponent.ChekFilename(filename, m)
Exemple #11
0
            ### dump attributes in fn file
            cPickle.dump(obj=PickledCollection(obj_dumped),
                         file=open(fn, "wb"),
                         protocol=0)

        except Exception, info:

            sys.stderr.write(
                _("Problem saving (during the dump): %s -- %s\n") %
                (str(fileName), info))
            return False
        else:

            try:

                zf = ZipManager.Zip(fileName)

                ### create or update fileName
                if os.path.exists(fileName):

                    zf.Update(replace_files=[fn, python_path, image_path])
                else:
                    zf.Create(add_files=[fn, python_path, image_path])

                os.remove(fn)

                ## chemin absolu du repertoir contenant le fichier a exporter (str() pour eviter l'unicode)
                newExportPath = str(os.path.dirname(fileName))

                mainW = getTopLevelWindow()
                ### si export dans un repertoir local on insert le chemin dans le fichier de config
    def SelectProp(self, evt):
        """
		"""

        row, col = evt.GetRow(), evt.GetCol()

        table = self.GetTable()

        typ = table.dataTypes[row][1]
        prop = self.GetCellValue(row, 0)

        if prop == 'fill' or re.findall(
                "[.]*color[.]*", prop, flags=re.IGNORECASE):
            val = self.GetCellValue(row, 1)
            dlg = wx.ColourDialog(self.parent)
            dlg.GetColourData().SetChooseFull(True)
            if dlg.ShowModal() == wx.ID_OK:
                data = dlg.GetColourData()
                val = str([RGBToHEX(data.GetColour().Get())])
                self.SetCellValue(row, 1, val)
            else:
                dlg.Destroy()
                return False

            dlg.Destroy()

            self.AcceptProp(row, col)

        elif prop == 'font':
            val = eval(self.GetCellValue(row, 1))
            default_font = wx.Font(val[0], val[1], val[2], val[3], False,
                                   val[4])
            data = wx.FontData()
            if sys.platform == 'win32':
                data.EnableEffects(True)
            data.SetAllowSymbols(False)
            data.SetInitialFont(default_font)
            data.SetRange(10, 30)
            dlg = wx.FontDialog(self.parent, data)
            if dlg.ShowModal() == wx.ID_OK:
                data = dlg.GetFontData()
                font = data.GetChosenFont()
                color = data.GetColour()
                val = [
                    font.GetPointSize(),
                    font.GetFamily(),
                    font.GetStyle(),
                    font.GetWeight(),
                    font.GetFaceName()
                ]
                self.SetCellValue(row, 1, str(val))
            else:
                dlg.Destroy()
                return False

            dlg.Destroy()

            self.AcceptProp(row, col)

        elif prop == 'label':

            d = LabelGUI.LabelDialog(self.parent, self.parent.model)
            d.ShowModal()

            self.SetCellValue(row, 1, str(self.parent.model.label))
            self.AcceptProp(row, col)

        elif prop == 'image_path':

            dlg = ib.ImageDialog(self, os.path.join(HOME_PATH))
            dlg.Centre()

            if dlg.ShowModal() == wx.ID_OK:
                val = os.path.normpath(dlg.GetFile())
                if val != self.GetCellValue(row, 1):
                    self.SetCellValue(row, 1, val)
                    self.canvas.UpdateShapes([self.parent.model])
            else:
                dlg.Destroy()
                return False

            dlg.Destroy()

            self.AcceptProp(row, col)

        elif 'filename' in str(prop).lower():
            wcd = _('Data files All files (*)|*')
            val = self.GetCellValue(row, 1)
            default_dir = os.path.dirname(val) if os.path.exists(
                os.path.dirname(val)) else HOME_PATH
            dlg = wx.FileDialog(self,
                                message=_("Select file ..."),
                                defaultDir=default_dir,
                                defaultFile="",
                                wildcard=wcd,
                                style=wx.OPEN | wx.CHANGE_DIR)
            if dlg.ShowModal() == wx.ID_OK:
                val = os.path.normpath(dlg.GetPath())
                if val != self.GetCellValue(row, 1):
                    self.SetCellValue(row, 1, val)
                    self.canvas.UpdateShapes([self.parent.model])
            else:
                dlg.Destroy()
                return False

            dlg.Destroy()

            self.AcceptProp(row, col)

        elif prop == 'python_path':

            model = self.parent.model

            ### for .amd or .cmd
            if model.model_path != '':
                wcd = _(
                    'Atomic DEVSimPy model (*.amd)|*.amd|Coupled DEVSimPy model (*.cmd)|*.cmd|All files (*)|*'
                )
            else:
                wcd = _('Python files (*.py)|*.py|All files (*)|*')

            default_dir = os.path.dirname(model.python_path) if os.path.exists(
                os.path.dirname(model.python_path)) else DOMAIN_PATH
            dlg = wx.FileDialog(self,
                                message=_("Select file ..."),
                                defaultDir=default_dir,
                                defaultFile="",
                                wildcard=wcd,
                                style=wx.OPEN | wx.CHANGE_DIR)
            if dlg.ShowModal() == wx.ID_OK:
                new_python_path = os.path.normpath(dlg.GetPath())

                ### if the user would like to load a compressed python file, he just give the name of compressed file that contain the python file
                if zipfile.is_zipfile(new_python_path):
                    zf = zipfile.ZipFile(new_python_path, 'r')
                    new_python_path = os.path.join(
                        new_python_path,
                        filter(
                            lambda f: f.endswith('.py') and f != 'plugins.py',
                            zf.namelist())[0])
                    ### update model path
                    model.model_path = os.path.dirname(new_python_path)

                self.SetCellValue(row, 1, new_python_path)

                # behavioral args update (because depends of the new class coming from new python file)
                new_cls = Components.GetClass(new_python_path)

                if inspect.isclass(new_cls):

                    ### update attributes (behavioral ang graphic)
                    model.args = Components.GetArgs(new_cls)
                    model.SetAttributes(Attributable.GRAPHICAL_ATTR)

                    ### TODO: when ScopeGUI and DiskGUI will be amd models, delete this line)
                    ### delete xlabel and ylabel attributes if exist
                    model.RemoveAttribute('xlabel')
                    model.RemoveAttribute('ylabel')
                    ### Update of DEVSimPy model from new python behavioral file (ContainerBlock is not considered because he did not behavioral)
                    if new_cls.__name__ in ('To_Disk', 'MessagesCollector'):
                        model.__class__ = Container.DiskGUI
                    elif new_cls.__name__ == 'QuickScope':
                        model.__class__ = Container.ScopeGUI
                        model.AddAttribute("xlabel")
                        model.AddAttribute("ylabel")
                    elif True in map(lambda a: 'DomainStructure' in str(a),
                                     new_cls.__bases__):
                        model.__class__ = Container.ContainerBlock
                    else:
                        model.__class__ = Container.CodeBlock

                    ### if we change the python file from zipfile we compresse the new python file and we update the python_path value
                    if zipfile.is_zipfile(model.model_path):
                        zf = ZipManager.Zip(model.model_path)
                        zf.Update([new_python_path])
                        #model.model_path =

                    ### update flag and color if bad filename
                    if model.bad_filename_path_flag:
                        model.bad_filename_path_flag = False
                else:
                    Container.MsgBoxError(evt, self, new_cls)
                    dlg.Destroy()
                    return False
            else:
                dlg.Destroy()
                return False

            dlg.Destroy()

            self.AcceptProp(row, col)

        elif typ == "list":
            frame = ListEditor(self,
                               wx.ID_ANY,
                               _('List editor'),
                               values=self.GetCellValue(row, 1))
            if frame.ShowModal() == wx.ID_CANCEL:
                self.SetCellValue(row, 1, frame.GetValueAsString())
            else:
                frame.Destroy()

            self.AcceptProp(row, col)

        elif typ == 'dict':
            frame = DictionaryEditor(self,
                                     wx.ID_ANY,
                                     _('List editor'),
                                     values=self.GetCellValue(row, 1))
            if frame.ShowModal() == wx.ID_CANCEL:
                self.SetCellValue(row, 1, frame.GetValueAsString())
            else:
                frame.Destroy()

            self.AcceptProp(row, col)
        elif 'choice' in typ:
            self.AcceptProp(row, col)
        else:
            pass

        ### all properties grid update (because the python classe has been changed)
        ### here, because OnAcceptProp should be executed before
        if prop == 'python_path':

            ### Update table from new model
            table.UpdateRowBehavioralData(model)
            self.SetTable(table, False)
            self.ForceRefresh()
            self.AutoSizeColumns()

            # code updating
            if isinstance(model, Achievable):
                new_code = CodeCB(self.parent, wx.ID_ANY, model)
                #self.parent.boxH.Remove(0)
                if hasattr(self.parent, '_boxH'):
                    # DeleteWindows work better in vista
                    if wx.VERSION_STRING < '4.0':
                        self.parent._boxH.DeleteWindows()
                        self.parent._boxH.AddWindow(new_code,
                                                    1,
                                                    wx.EXPAND,
                                                    userData='code')
                    else:
                        self.parent._boxH.Clear()
                        self.parent._boxH.Add(new_code,
                                              1,
                                              wx.EXPAND,
                                              userData='code')

                    self.parent._boxH.Layout()
                else:
                    sys.stdout.write("_boxH is unknown!")
Exemple #13
0
def load(file_name, table_name, ignored):
    ZipManager.unzipTable(table_name)
    # table = Table.Table(table_name, "ab")
    fp_list = {}
    field_names = []
    scheme_path = os.path.join(table_name, "table.json")
    schema_file = open(scheme_path, "r")
    full_file = json.load(schema_file)
    schema_file.close()
    file_num = full_file["file_num"]
    last_i = full_file["last_i"]
    schema = full_file['schema']

    with open(file_name, "r") as csvfile:
        file_reader = csv.reader(csvfile)
        i = 0
        current_batch = last_i
        max_size = consts.FILE_SIZES
        current_fp_index = file_num
        for x in schema:
            field_name = x["field"]
            field_type = x["type"]
            field_names.append(field_name)
            fp_list[field_name] = write_column(table_name, field_name,
                                               field_type, current_fp_index)
        for row in file_reader:
            if current_batch > max_size - 1:
                current_batch = 0
                current_fp_index += 1
                for field_name in field_names:
                    fp_list[field_name].fp.close()
                    fp_list[field_name].fp = open_file(table_name, field_name,
                                                       current_fp_index)
            if i < ignored:
                i += 1
                continue
            for val, field_name in zip(row, field_names):
                col = fp_list[field_name]

                if col.type == "varchar":
                    col.fp.write(bytes(val + "\x00", encoding='utf8'))

                elif col.type == "int":
                    col.fp.write((
                        b"\x00" +
                        struct.pack(">q", 0)) if val in ["", "NULL"] else (
                            b"\x01" + struct.pack(">q", int(val))))

                elif col.type == "float":
                    if val in ["", "NULL"]:
                        # negative zero(-0.0) , NULL
                        out = struct.pack(">Q", 2**63)
                    else:
                        out = struct.pack(">d", float(val))
                    col.fp.write(out)

                elif col.type == "timestamp":
                    col.fp.write((
                        b"\x00" +
                        struct.pack(">Q", 0)) if val in ["", "NULL"] else (
                            b"\x01" + struct.pack(">Q", int(val))))
            current_batch += 1
        for field_name in field_names:
            fp_list[field_name].fp.close()
        scheme_path = os.path.join(table_name, "table.json")
        schema_file = open(scheme_path, "r+")
        full_file = json.load(schema_file)
        schema_file.seek(0)
        full_file["file_num"] = current_fp_index
        full_file["last_i"] = current_batch
        json.dump(full_file, schema_file, indent=True)
        schema_file.close()
        ZipManager.zipTable(table_name)
Exemple #14
0
	def Save(self, obj_dumped, fileName = None):
		""" Function that save the codeblock on the disk.
		"""
		assert(fileName.endswith(tuple(DumpZipFile.ext)))

		### local copy of paths
		python_path = obj_dumped.python_path
		image_path = obj_dumped.image_path

		### Now, file paths are in the compressed file
		if os.path.isabs(python_path):
			path = os.path.join(fileName, os.path.basename(obj_dumped.python_path))
			if os.path.exists(path):
				obj_dumped.python_path = path

		if os.path.isabs(image_path):
			obj_dumped.image_path = os.path.join(fileName, os.path.basename(obj_dumped.image_path))

		obj_dumped.model_path = fileName

		### args is constructor args and we save these and not the current value
		if hasattr(obj_dumped, 'args'):
			obj_dumped.args = Components.GetArgs(Components.GetClass(obj_dumped.python_path))
		try:

			fn = 'DEVSimPyModel.dat'

			### dump attributes in fn file
			pickle.dump(	obj = PickledCollection(obj_dumped),
							file = open(fn, "wb"),
							protocol = 0)

		except Exception as info:

			sys.stderr.write(_("Problem saving (during the dump): %s -- %s\n")%(str(fileName),info))
			return False
		else:

			try:

				zf = ZipManager.Zip(fileName)

				### create or update fileName
				if os.path.exists(fileName):
					zf.Update(replace_files = [fn, python_path, image_path])
				else:
					zf.Create(add_files = [fn, python_path, image_path])

				os.remove(fn)

				## abs path of the directory that contains the file to export (str() to avoid unicode)
				newExportPath = str(os.path.dirname(fileName))

				mainW = getTopLevelWindow()
				### if export on local directory, we insert the path in the config file
				if not os.path.basename(DOMAIN_PATH) in newExportPath.split(os.sep):
					### update of .devsimpy config file
					mainW.exportPathsList = eval(mainW.cfg.Read("exportPathsList"))
					if newExportPath not in mainW.exportPathsList:
						mainW.exportPathsList.append(str(newExportPath))
					mainW.cfg.Write("exportPathsList", str(eval("mainW.exportPathsList")))

				### if lib is already in the lib tree, we update the tree
				mainW.tree.UpdateDomain(newExportPath)
				### to sort lib tree
				mainW.tree.SortChildren(mainW.tree.root)

			except Exception as info:
				sys.stderr.write(_("Problem saving (during the zip handling): %s -- %s\n")%(str(fileName),info))
				return False
			else:

				return True
Exemple #15
0
    def select(self, out, _fields, where, group, having, order):
        """
		out - name of outfile name or None in case of no outfile
		_fields - field names as supplied by the client(list)
		field is either a string for literal field, tuple for aggregator or list for nickname
		nickname is: [field, nickname]
		aggregator is: (field, aggergation)
		where - dictionary of op, const, field
		group - list of fields to group by
		having - like where
		order - list of: [(field, ASC/DESC)*]
		"""
        fields, nicknames, needed_fields = self.get_final_field(_fields)

        print_to_screen = False
        if out is None:
            out = "tmpOutput.csv"
            print_to_screen = True

        fields_list = list(needed_fields)
        if where:
            if not where["field"] in fields_list:
                fields_list.append(where["field"])

        def c_list(lst, varType):
            c_lst = (varType * len(lst))()
            c_lst[:] = lst
            return c_lst

        paths = [
            os.path.join(self.name, field).encode("ascii")
            for field in fields_list
        ]
        c_paths = c_list(paths, ctypes.c_char_p)

        col_types = [{
            "int": 0,
            "varchar": 1,
            "float": 2,
            "timestamp": 3
        }[self.type_from_name(field)] for field in fields_list]
        c_col_types = c_list(col_types, ctypes.c_int)

        out_fields = [
            fields_list.index(nicknames[field] if not isinstance(
                nicknames[field], tuple) else nicknames[field][1])
            for field in fields
        ]
        c_out_fields = c_list(out_fields, ctypes.c_int)

        #order - list of: [(field, ASC/DESC)*]
        order_indeces = [
            fields.index(nicknames[x[0]]
                         if not isinstance(nicknames[x[0]], tuple) else x[0])
            for x in order
        ]
        c_order_indeces = c_list(order_indeces, ctypes.c_int)

        order_directions = [{"asc": 0, "desc": 1}[x[1]] for x in order]
        c_order_directions = c_list(order_directions, ctypes.c_int)

        group_indeces = [fields.index(nicknames[x]) for x in group]
        c_group_indeces = c_list(group_indeces, ctypes.c_int)

        agg_types = {'min': 0, 'max': 1, 'sum': 2, 'count': 3, 'avg': 4}
        aggs_list = [
            agg_types[nicknames[field][0]] for field in fields
            if isinstance(nicknames[field], tuple)
        ]
        c_aggs_list = c_list(aggs_list, ctypes.c_int)

        aggs_fields = [
            fields.index(nicknames[field] if nicknames[field] in
                         fields else field) for field in fields
            if isinstance(nicknames[field], tuple)
        ]
        c_aggs_fields = c_list(aggs_fields, ctypes.c_int)

        where_op = -1  #no where
        where_const = ctypes.c_void_p(0)  #nullptr
        where_field = -1
        if (where):
            where_op = {
                "<": 0,
                "<=": 1,
                ">": 2,
                ">=": 3,
                "==": 4,
                "<>": 5,
                "is": 6,
                "is not": 7
            }[where["op"]]
            where_field = fields_list.index(where["field"])
            where_type = self.type_from_name(where["field"])
            if (where_type == "int"):
                where_const = ctypes.c_void_p(
                    csvdbLib.CreateIntWhereConst(where["const"]))
            if (where_type == "timestamp"):
                where_const = ctypes.c_void_p(
                    csvdbLib.CreateTimestampWhereConst(where["const"]))
            if (where_type == "varchar"):
                where_const = ctypes.c_void_p(
                    csvdbLib.CreateVarcharWhereConst(
                        where["const"].encode("ascii")))
            if (where_type == "float"):
                where_const = ctypes.c_void_p(
                    csvdbLib.CreateFloatWhereConst(where["const"]))

        having_op = -1  #no having
        having_const = ctypes.c_void_p(0)  #nullptr
        having_field = -1
        if (having):
            having_op = {
                "<": 0,
                "<=": 1,
                ">": 2,
                ">=": 3,
                "==": 4,
                "<>": 5,
                "is": 6,
                "is not": 7
            }[having["op"]]
            having_field = fields.index(having["field"])
            having_type = self.type_from_name(nicknames[having["field"]])
            if (having_type == "int"):
                having_const = ctypes.c_void_p(
                    csvdbLib.CreateIntWhereConst(having["const"]))
            if (having_type == "timestamp"):
                having_const = ctypes.c_void_p(
                    csvdbLib.CreateTimestampWhereConst(having["const"]))
            if (having_type == "varchar"):
                having_const = ctypes.c_void_p(
                    csvdbLib.CreateVarcharWhereConst(
                        having["const"].encode("ascii")))
            if (having_type == "float"):
                having_const = ctypes.c_void_p(
                    csvdbLib.CreateFloatWhereConst(having["const"]))

        table = ctypes.c_void_p(csvdbLib.Table_Create(len(c_paths), c_paths, c_col_types, \
              len(c_out_fields), c_out_fields, \
              where_field, where_op, where_const, \
              self.line_batches, self.file_num,
              len(order), c_order_indeces,
              c_order_directions, (self.name + os.sep).encode("ascii"),
              len(group), c_group_indeces,
              len(aggs_fields), c_aggs_list, c_aggs_fields,
              having_field, having_op, having_const))
        csvdbLib.Table_select(table, out.encode("ascii"))
        csvdbLib.Table_delete(table)

        if print_to_screen:
            with open("tmpOutput.csv", "r") as csvfile:
                file_reader = csv.reader(csvfile)
                for row, _ in zip(file_reader,
                                  range(consts.MAX_PRINT_IN_SELECT)):
                    print(line_joiner(row))
            os.remove("tmpOutput.csv")
        ZipManager.cleanTable(self.name)