示例#1
0
文件: gpg.py 项目: BrainPad/cliboa
    def encrypt(self,
                src_path,
                dest_path,
                recipients,
                passphrase=None,
                always_trust=False):
        with open(src_path, "rb") as f:
            status = self._gpg.encrypt_file(
                file=f,
                recipients=recipients,
                always_trust=always_trust,
                passphrase=passphrase,
                output=dest_path + ".gpg",
            )

        if status.ok is False:
            self.log.error(status.stderr)
            raise CliboaException("Failed to encrypt gpg.")

        self.log.info(status.ok)
        self.log.info(status.status)
示例#2
0
    def execute(self, *args):
        for k, v in self.__dict__.items():
            self._logger.info("%s : %s" % (k, v))

        files = super().get_target_files(self._src_dir, self._src_pattern)
        self._logger.info("Files found %s" % files)
        for f in files:
            _, ext = os.path.splitext(f)
            if ext == ".zip":
                self._logger.info("Decompress zip file %s" % f)
                with zipfile.ZipFile(f) as zp:
                    zp.extractall(self._dest_dir if self.
                                  _dest_dir is not None else self._src_dir)
            elif ext == ".tar":
                self._logger.info("Decompress tar file %s" % f)
                with tarfile.open(f, "r:*") as tf:
                    tf.extractall(self._dest_dir if self.
                                  _dest_dir is not None else self._src_dir)
            elif ext == ".bz2":
                self._logger.info("Decompress bz2 file %s" % f)
                dcom_name = os.path.splitext(os.path.basename(f))[0]
                with open(f, mode="rb") as fb:
                    rb = fb.read()
                decom_path = (os.path.join(self._dest_dir, dcom_name)
                              if self._dest_dir is not None else os.path.join(
                                  self._src_dir, dcom_name))
                with open(decom_path, mode="wb") as fb:
                    fb.write(bz2.decompress(rb))
            elif ext == ".gz":
                self._logger.info("Decompress gz file %s" % f)
                dcom_name = os.path.splitext(os.path.basename(f))[0]
                decom_path = (os.path.join(self._dest_dir, dcom_name)
                              if self._dest_dir is not None else os.path.join(
                                  self._src_dir, dcom_name))
                with gzip.open(f, "rb") as i, open(decom_path, "wb") as o:
                    o.write(i.read())
            else:
                raise CliboaException(
                    "Unmatched any available decompress type %s" % f)
示例#3
0
    def execute(self, *args):
        input_valid = IOInput(self._io)
        input_valid()

        for k, v in self.__dict__.items():
            self._logger.info("%s : %s" % (k, v))

        files = glob(self._src_path)
        if len(files) > 1:
            raise CliboaException("Input file must be only one.")

        if len(files) == 0:
            raise FileNotFound("The specified csv file not found.")

        with open(files[0], "r", encoding=self._encoding) as f:

            # save per one column
            if self.__columns:
                reader = csv.DictReader(f, delimiter=",")
                for row in reader:
                    # extract only the specified columns
                    row_dict = {}
                    for c in self.__columns:
                        if not row.get(c):
                            continue
                        row_dict[c] = row.get(c)
                    self._s.save(row_dict)
            else:
                reader = csv.reader(f)
                header = next(reader, None)
                for row in reader:
                    row_dict = dict(zip(header, row))
                    self._s.save(row_dict)

        # cache downloaded file names
        ObjectStore.put(self._step, files)
示例#4
0
 def execute(self, *args):
     raise CliboaException("Something wrong")
示例#5
0
 def __call__(self):
     for p in self.__param_list:
         if not p:
             raise CliboaException(
                 "The essential parameter is not specified in %s." % self.__cls_name
             )