Exemplo n.º 1
0
def __patch(obj, name, new):
    n = NONE()
    orig = getattr(obj, name, n)
    if orig is not n:
        if orig == new:
            raise Exception("Shouldn't happen, new and orig are the same")
        setattr(obj, name, new)
    return
Exemplo n.º 2
0
    def __write_index(self, new_index, number=0, edit=False, ind_kwargs=None):
        if ind_kwargs is None:
            ind_kwargs = {}
        p = os.path.join(self.path, '_indexes')
        if isinstance(new_index, str) and not new_index.startswith("path:"):
            if len(new_index.splitlines()) < 4 or new_index.splitlines()[3] != '# inserted automatically':
                from .indexcreator import Parser
                par = Parser()
                # s = par.parse(new_index).splitlines()
                custom_imports, s = par.parse(new_index)
                s = s.splitlines()
                name = s[0][2:]
                c = s[1][2:]
                comented = ['\n\n#SIMPLIFIED CODE']
                list([comented.append("#" + x) for x in new_index.splitlines()])
                comented.append('#SIMPLIFIED CODE END\n\n')

                s = header_for_indexes(
                    name, c, ind_custom=custom_imports) + "\n".join(s[2:]) + "\n".join(comented)
                new_index = s
            else:
                name = new_index.splitlines()[0][2:]
                name = name.strip()

            if name in self.indexes_names and not edit:
                raise IndexConflict("Already exists")
            if edit:
                previous_index = [x for x in os.listdir(p) if x.endswith(
                    '.py') and x[2:-3] == name]
                if not previous_index:
                    raise PreconditionsException(
                        "Can't edit index that's not yet in database")
                number = int(previous_index[0][:2])
            if number == 0 and not edit and not name == 'id':
                raise PreconditionsException(
                    "Id index must be the first added")
            ind_path = "%.2d%s" % (number, name)
            if not edit:
                self.__check_if_index_unique(name, number)

            ind_path_f = os.path.join(p, ind_path + '.py')
            if os.path.exists(ind_path_f):
                os.rename(ind_path_f, ind_path_f + '_last')  # save last working index code
            with io.FileIO(ind_path_f, 'w') as f:
                f.write(bytes(new_index, 'utf-8'))

            ind_obj = self._read_index_single(p, ind_path + '.py')

        elif isinstance(new_index, str) and new_index.startswith("path:"):
            path = new_index[5:]
            if not path.endswith('.py'):
                path += '.py'
            ind_obj = self._read_index_single(p, path, ind_kwargs)
            name = ind_obj.name
            if name in self.indexes_names and not edit:
                raise IndexConflict("Already exists")
        elif isinstance(new_index, Index):
            # it will first save index as a string, and then compile it
            # it will allow to control the index object on the DB side
            ind = new_index
            init_arguments = new_index.__class__.__init__.__code__.co_varnames[
                3:]  # ignore self, path and name
            for curr in init_arguments:
                if curr not in ('args', 'kwargs'):
                    v = getattr(ind, curr, NONE())
                    if not isinstance(v, NONE):
                        ind_kwargs[curr] = v
            if edit:
                # code duplication...
                previous_index = [x for x in os.listdir(p) if x.endswith(
                    '.py') and x[2:-3] == ind.name]
                if not previous_index:
                    raise PreconditionsException(
                        "Can't edit index that's not yet in database")
                number = int(previous_index[0][:2])
            if ind.name in self.indexes_names and not edit:
                raise IndexConflict("Already exists")
            if number == 0 and not edit and not ind.name == 'id':
                raise PreconditionsException(
                    "Id index must be the first added")
            if not edit:
                self.__check_if_index_unique(ind.name, number)
            self._add_single_index(p, number, ind)
            ind_path = "%.2d%s" % (number, ind.name)
            ind_obj = self._read_index_single(p, ind_path + '.py', ind_kwargs)
            name = ind_obj.name
        else:
            raise PreconditionsException("Argument must be Index instance, path to index_file or valid string index format")
        return ind_obj, name