예제 #1
0
    def populate_index(self):
        """"""
        self.BLOCK_REPOPULATE = False
        if self.index is None or self.index != self.thimpra.impst.idxu.index :
            self.index = self.thimpra.impst.idxu.index
        data     = sorted([(self.index.dic.get(k),k) for i, k in enumerate(self.index.dic) if not k.startswith(self.index.SEP_KEY_INTERN)], reverse=False, key=lambda lst:lst[0][self.index.UID])
        store    = self.get('treestore1')
        store.clear()
        drow     = None
        i        = 0
        tsize    = 0
        psize    = 0
        accounts = self.thimpra.impst.idxu.getAccountList()
        allCatg, allUsers, allAccounts, tmp = [], [], [ accounts[a] for a in accounts], ''
        for row, key in data :
            
            tsize += row[self.index.SIZE]
            if self.filterIds==None or row[self.index.UID] in self.filterIds:
            
                drow = list(row[:-1])
                psize += row[self.index.SIZE]

                if drow[self.index.CATG] not in allCatg :
                    allCatg.append(drow[self.index.CATG])
                tmp = self.index.getUser(drow[self.index.USER])                
                if tmp not in allUsers :
                    allUsers.append(tmp)
                drow[self.index.PARTS]     = ('%s' % drow[self.index.PARTS]).rjust(2,' ')
                drow[self.index.UID]       = ('%s' % drow[self.index.UID]).rjust(4,' ')
                drow[self.index.HASH]      = '%s' % drow[self.index.HASH][0:6]+'…'
                drow[self.index.SIZE]      = Sys.readableBytes(row[self.index.SIZE]).rjust(11,' ')+'  '
                drow[self.index.USER]      = self.index.getUser(drow[self.index.USER])
                drow[self.index.ACCOUNT]   = self.index.acclist[drow[self.index.ACCOUNT]]
                drow.append('#222222' if i%2!=0 else '#1C1C1C')
                drow.append('#640000' if i%2!=0 else '#900000')
                store.append(None, drow)
                i += 1
                self.progressbar.set_fraction(10+i*90/len(data)/100.0)
            
        # repopulate only if not search
        if self.filterIds==None :
            #~ Sys.pwlog([(' Populating search filters...', Const.CLZ_0, True)])
            self.populate_search_filters(allCatg, allUsers, allAccounts)
        
        self.get('checkbutton1').set_sensitive(True)
        
        self.get('label12').set_label(' '+(Sys.readableBytes(psize)+' / ' if psize != tsize else '')+Sys.readableBytes(tsize))
        
        self.get('button9').set_sensitive(True)
        self.get('button12').set_sensitive(True)
        return False
예제 #2
0
    def buildFile(self, fromPath, label="", catg=""):
        count = self.getCountParts(fromPath)
        Sys.pwlog([(" Get Hash... ", Const.CLZ_7, True)])
        sha256 = hash_sha256_file(fromPath)
        Sys.pwlog([(" hash : ", Const.CLZ_0), (sha256, Const.CLZ_2, True), (" Build File...", Const.CLZ_0, True)])

        kg = KeyGen(128)
        size = Sys.getsize(fromPath)
        row = self.idxu.index.get(sha256)
        if row is None:
            if label == "":
                label, ext = Sys.getFileExt(Sys.basename(fromPath))
            else:
                label, ext = Sys.getFileExt(label)
            if catg == "":
                catg = self.idxu.index.getAutoCatg(ext)
            size = Sys.getsize(fromPath)

            Sys.pwlog(
                [
                    (" Splitting ", Const.CLZ_1),
                    (label, Const.CLZ_7),
                    (ext, Const.CLZ_7),
                    (" (", Const.CLZ_0),
                    (Sys.readableBytes(size), Const.CLZ_3),
                    (")", Const.CLZ_0, True),
                ]
            )
            Sys.cli_emit_progress(0)
            Sys.sleep(0.2)
            km = Kirmah(kg.key)
            km.DIR_OUTBOX = self.outbox
            # hlst genetate with sha256
            hlst = km.ck.getHashList(sha256, int(count), True)
            usr = self.idxu.conf.get("name", "infos")
            ownerHash = self.idxu.mb.getHashName(usr)
            km.split(fromPath, hlst)
            Sys.pwlog([(" done ", Const.CLZ_2, True)])
            row = [kg.key, label, ext, count, catg, hlst, usr, ownerHash, sha256, size]
            self.backupAddMap(row)

        else:

            Sys.pwlog(
                [
                    (" File Already exist ! ", Const.CLZ_1, True),
                    (" id : ".rjust(10, " "), Const.CLZ_0),
                    (str(row[ImpraIndex.UID]), Const.CLZ_1, True),
                    (" label : ".rjust(10, " "), Const.CLZ_0),
                    (row[ImpraIndex.LABEL], Const.CLZ_3, True),
                ]
            )

            row = None
        return row
예제 #3
0
    def getInfo(self, uid):
        """"""
        done = False
        key = self.idxu.index.getById(uid)
        row = self.idxu.index.get(key)
        if row is not None:
            account = self.idxu.switchFileAccount(row[self.idxu.index.ACCOUNT])
            km = Kirmah(row[self.idxu.index.KEY])
            hlst = km.ck.getHashList(key, row[self.idxu.index.PARTS], True)

            Sys.pwlog(
                [
                    ("id ".rjust(14, " ") + ": ", Const.CLZ_0),
                    (str(row[ImpraIndex.UID]), Const.CLZ_1, True),
                    ("hash ".rjust(14, " ") + ": ", Const.CLZ_0),
                    (row[ImpraIndex.HASH], Const.CLZ_2, True),
                    ("name ".rjust(14, " ") + ": ", Const.CLZ_0),
                    (row[ImpraIndex.LABEL] + row[ImpraIndex.EXT], Const.CLZ_7, True),
                    ("size ".rjust(14, " ") + ": ", Const.CLZ_0),
                    (Sys.readableBytes(row[ImpraIndex.SIZE]), Const.CLZ_6, True),
                    ("category ".rjust(14, " ") + ": ", Const.CLZ_0),
                    (row[ImpraIndex.CATG], Const.CLZ_5, True),
                    ("user ".rjust(14, " ") + ": ", Const.CLZ_0),
                    (self.idxu.index.getUser(row[ImpraIndex.USER]), Const.CLZ_3),
                    (" (" + row[ImpraIndex.USER] + ")", Const.CLZ_5, True),
                    ("account ".rjust(14, " ") + ": ", Const.CLZ_0),
                    (self.idxu.conf.get("user", "imap", row[ImpraIndex.ACCOUNT]), Const.CLZ_4, True),
                    (Const.LINE_SEP_CHAR * Const.LINE_SEP_LEN, Const.CLZ_0, True),
                    ("subject ".rjust(14, " ") + ": ", Const.CLZ_0),
                    (hlst["head"][2], Const.CLZ_1, True),
                ]
            )

            ids = self.idxu.ih.searchBySubject(hlst["head"][2], True)

            for i, uid in enumerate(ids):
                if i < len(hlst["data"]):

                    Sys.pwlog(
                        [
                            ("attach file ".rjust(14, " ") + ": ", Const.CLZ_0),
                            (hlst["data"][i][1] + Kirmah.EXT, Const.CLZ_2),
                            (" (", Const.CLZ_0),
                            (str(int(uid)), Const.CLZ_1),
                            (") (", Const.CLZ_0),
                            (str(hlst["data"][i][4]), Const.CLZ_3),
                            (")", Const.CLZ_0, True),
                        ]
                    )
                else:
                    Sys.pwlog(
                        [(" Wrong id (to del)".ljust(14, " ") + ": ", Const.CLZ_2), (str(uid), Const.CLZ_2, True)]
                    )
        return done
예제 #4
0
    def print(self,order='ID', matchIds=None):
        """Print index content as formated bloc"""
        #~ Sys.clear()
        #~ Cli.print_header()
        #~ AbstractCli.printLineSep(Const.LINE_SEP_CHAR,Const.LINE_SEP_LEN)

        inv    = order.startswith('-')
        if inv : order = order[1:]
        orderIndex = self.COLS.index(order)
        if orderIndex is None : orderIndex = self.COLS.index('ID')
        d = sorted([(self.dic.get(k),k) for i, k in enumerate(self.dic) if not k.startswith(self.SEP_KEY_INTERN)], reverse=inv, key=lambda lst:lst[0][orderIndex])

        sizeid = 1+Sys.ceil(len(str(len(d))))
        if sizeid < 3 : sizeid = 3
        addsize = abs(3 - sizeid);
        
        sort = '^' if inv else '_' #'ↆ'

        space = (4+addsize, 8, 38, 10, 3, 5, 11, 24-addsize, 13)
        for i, s in enumerate(self.COLS[:-1]):
            symb, c = sort if order == s else ' ', Sys.Clz.BG4+Sys.Clz.fgB7 if order != s else Sys.Clz.BG7+Sys.Clz.fgB4
            Sys.echo ((' '+s+symb).ljust(space[i],' ') , c, False, False)
        Sys.echo('', c)
        Sys.echo(Const.LINE_SEP_CHAR*Const.LINE_SEP_LEN, Sys.CLZ_HEAD_LINE)

        a = ''
        tsize = 0
        psize = 0
        acc   = {}
        wrap  = '… ' if Sys.isUnix() else '/ '
        for v,k in d :
            if matchIds==None or v[self.UID] in matchIds:
                if v[self.SIZE] == '' : v[self.SIZE] = 0
                a = ''
                Sys.echo(str(v[self.UID]).rjust(sizeid+1,' ')                               , Sys.Clz.bg1+Sys.Clz.fgB7, False)
                Sys.echo(' '+str(k).ljust(9,' ')[0:6]+wrap                                  , Sys.Clz.fgN2, False)
                if len(v[self.LABEL])>36 : a = wrap
                try:
                    Sys.echo(str(v[self.LABEL][:36]+a).ljust(38,' ')                        , Sys.Clz.fgN7, False)
                except:
                    pass
                    j = 0
                    for c in v[self.LABEL][:36] :                        
                        try:
                            Sys.echo(str(c)                                                 , Sys.Clz.fgN7, False, False)
                        except:
                            Sys.echo('?'                                                    , Sys.Clz.fgN7, False, False)
                        j += 1
                    Sys.echo(''.ljust(38-j,' ')                      , Sys.Clz.fgN7, False, False)

                a = ''
                Sys.echo(Sys.readableBytes(v[self.SIZE])[:9].rjust(9,' ')+' '*2             , Sys.Clz.fgN5, False)
                Sys.echo(str(v[self.PARTS]).rjust(2 ,'0') +' '*2                            , Sys.Clz.fgN1, False)
                Sys.echo(str(v[self.EXT][:6]).ljust(7,' ')                                  , Sys.Clz.fgn3, False)
                Sys.echo(self.getUser(str(v[self.USER])).ljust(11  ,' ')                    , Sys.Clz.fgn7, False)
                #~ Sys.echo(str(v[self.CATG]).ljust(30 ,' ')                       , Clz.fgN3)
                if len(v[self.CATG])>22 : a = wrap
                Sys.echo(str(v[self.CATG][:22]+a).ljust(24 ,' ')                            , Sys.Clz.fgN3, False)
                a = ''
                if len(v)-2==self.ACCOUNT:
                    if v[self.ACCOUNT] in self.acclist :
                        if len(self.acclist[v[self.ACCOUNT]])>11 : a = '…'
                        Sys.echo(str(self.acclist[v[self.ACCOUNT]][:11]+a).ljust(12 ,' ')    , Sys.Clz.fgN4)
                    else :
                        Sys.echo(str(v[self.ACCOUNT][:11]+'!').ljust(12 ,' ')                , Sys.Clz.fgN4)
                    if v[self.ACCOUNT] in acc :
                        acc[v[self.ACCOUNT]] += int(v[self.SIZE])
                    else : acc[v[self.ACCOUNT]] = int(v[self.SIZE])
                else: Sys.dprint()
            
                psize += int(v[self.SIZE])
            tsize += int(v[self.SIZE])
        if len(d)==0:
            Sys.echo(' empty', Sys.Clz.fgB1)

        Sys.echo(Const.LINE_SEP_CHAR*Const.LINE_SEP_LEN, Sys.CLZ_HEAD_LINE)
        c = Sys.Clz.fgB2
        if psize != tsize : c = Sys.Clz.fgB7
        Sys.echo(' size : ', Sys.Clz.fgB3, False)
        Sys.echo(Sys.readableBytes(psize)[:9].rjust(9,' '), c, False)
        if psize != tsize :
            Sys.echo(' / ', Sys.Clz.fgB3, False)
            Sys.echo(Sys.readableBytes(tsize), Sys.Clz.fgB2, False)
        Sys.dprint()
        Sys.echo(Const.LINE_SEP_CHAR*Const.LINE_SEP_LEN, Sys.CLZ_HEAD_LINE)
예제 #5
0
    def getFile(self, uid):
        """"""
        # ~ Sys.pwlog([(Const.LINE_SEP_CHAR*Const.LINE_SEP_LEN , Const.CLZ_0, True)])
        done = False
        key = self.idxu.index.getById(uid)
        row = self.idxu.index.get(key)
        filePath = None
        try:
            if row is not None:
                account = self.idxu.switchFileAccount(row[self.idxu.index.ACCOUNT])
                km = Kirmah(row[self.idxu.index.KEY])
                hlst = km.ck.getHashList(key, row[self.idxu.index.PARTS], True)
                ids = self.idxu.ih.searchBySubject(hlst["head"][2], True)
                Sys.cli_emit_progress(0)
                Sys.sleep(0.2)
                Sys.pwlog(
                    [
                        (" Downloading : ", Const.CLZ_7),
                        (row[self.idxu.index.LABEL] + row[self.idxu.index.EXT], Const.CLZ_2),
                        (" (", Const.CLZ_0),
                        (Sys.readableBytes(row[self.idxu.index.SIZE]), Const.CLZ_3),
                        (")", Const.CLZ_0),
                        (" please wait...", Const.CLZ_7, True),
                    ]
                )

                if len(ids) >= row[self.idxu.index.PARTS]:
                    self.getFileParts(row, ids)

                    Sys.pwlog([(" Merging parts...", Const.CLZ_7, True)])
                    Sys.cli_emit_progress(0)
                    Sys.sleep(0.2)
                    filePath = km.merge(
                        hlst,
                        self.deploy + row[self.idxu.index.CATG] + Sys.sep + row[self.idxu.index.LABEL],
                        ext=row[self.idxu.index.EXT],
                        uid=row[self.idxu.index.UID],
                        dirs=self.inbox,
                    )

                    Sys.pwlog(
                        [
                            (" Deployed as ", Const.CLZ_7),
                            (filePath, Const.CLZ_2),
                            (" (", Const.CLZ_0),
                            (Sys.readableBytes(Sys.getsize(filePath)), Const.CLZ_3),
                            (") ", Const.CLZ_0, True),
                            (" Checking integrity...", Const.CLZ_7, True),
                        ]
                    )
                    Sys.sleep(0.2)
                    sha256 = hash_sha256_file(filePath)
                    done = sha256 == row[ImpraIndex.HASH]
                    done = True

                else:
                    print("incomplete")

        except Exception as e:
            print(e)
        Sys.pwlog([(" done" if done else "ko", Const.CLZ_2 if done else Const.CLZ_1, True)])
        return done, filePath
예제 #6
0
    def sendFile(self, data, retry=False):
        """"""
        done = None
        key = None
        if data is not None:
            key, label, ext, count, catg, hlst, usr, ownerHash, sha256, size = data
            self.idxu.index.addUser(usr, ownerHash)
            account = self.idxu.switchFileAccount()
            sendIds = []
            cancel = False
            d = None

            Sys.cli_emit_progress(0)
            Sys.sleep(0.2)
            if not retry:
                Sys.pwlog(
                    [
                        (" Sending... ", Const.CLZ_7),
                        (" (", Const.CLZ_0),
                        (
                            " ~" + Sys.readableBytes(Sys.getsize(self.outbox + hlst["data"][0][1] + Kirmah.EXT)),
                            Const.CLZ_3,
                        ),
                        (" per msg ) ", Const.CLZ_0, True),
                    ]
                )
            else:
                Sys.pwlog(
                    [
                        (" Retry sending last file... ", Const.CLZ_0),
                        (label + ext, Const.CLZ_7),
                        (" (" + catg + ")", Const.CLZ_3, True),
                    ]
                )

            ignore = False

            for i, row in enumerate(hlst["data"]):
                """"""
                if retry:
                    if not Io.file_exists(self.outbox + row[1] + Kirmah.EXT):
                        continue
                    elif not ignore:
                        Sys.pwlog([(" Ignoring file 1 to " + str(i), Const.CLZ_1, True)])
                        ignore = True

                d = Sys.datetime.now()
                msg = self.idxu.mb.build(usr, "all", hlst["head"][2], self.outbox + row[1] + Kirmah.EXT)
                try:
                    mid = self.idxu.ih.send(msg.as_string(), self.rootBox)
                except Exception as e:
                    Sys.pwarn((("addFile : ", (str(e), Sys.CLZ_WARN_PARAM), " !"),))
                    Sys.echo("waiting 5 s and retry")
                    Sys.sleep(5)
                    # force reconnect
                    self.impst.idxu.switchFileAccount(account, True)
                    # retry
                    mid = self.idxu.ih.send(msg.as_string(), self.rootBox)
                finally:
                    if not mid is None:
                        status, resp = self.idxu.ih.fetch(mid[1], "(UID BODYSTRUCTURE)", True)
                        if status == self.idxu.ih.OK:
                            sendIds.append((mid[1], row))
                            Sys.pwlog(
                                [
                                    (" part ", Const.CLZ_0),
                                    (str(row[0]).rjust(2, "0"), Const.CLZ_2),
                                    (" sent as msg ", Const.CLZ_0),
                                    (str(mid[1]).rjust(5, "0"), Const.CLZ_1),
                                    (" (", Const.CLZ_7),
                                    (str(int(row[4]) + 1).rjust(2, "0"), Const.CLZ_2),
                                    ("/", Const.CLZ_7),
                                    (str(count), Const.CLZ_3),
                                    (") in ", Const.CLZ_7),
                                    (Sys.getDelta(d), Const.CLZ_4, True),
                                ]
                            )

                            Sys.cli_emit_progress(int((i + 1) * 100 / len(hlst["data"])))

                            Sys.removeFile(self.outbox + row[1] + Kirmah.EXT)
                        else:
                            Sys.pwarn((("error occured when sending part ", (row[0], Sys.Clz.fgb3), " !"),))

            diff = self.checkSendIds(sendIds, hlst["head"][2])
            if len(sendIds) == count or retry:
                self.idxu.get(True)
                self.idxu.index.add(key, label, hlst["head"][1], ext, ownerHash, catg, sha256, size, account)
                done = self.idxu.update()
                Io.removeFile(self.addmapPath + Kirmah.EXT)

            # resending missing parts
            else:
                Sys.pwarn((("TODO => must resending ", ("missing", Sys.CLZ_WARN_PARAM), " parts"),))
                print(diff)

            # clean
            for mid, row in sendIds:
                if Io.file_exists(self.outbox + row[1] + Kirmah.EXT):
                    Sys.removeFile(self.outbox + row[1] + Kirmah.EXT)
            if cancel:
                delids = [mid for mid, row in senids]
                print(delids)
                self.idxu.ih.delete(delids, True)
        return done, key