Esempio n. 1
0
def update(directory: str, file: object, *allow_extensions):
    """
    file-update interface, to update a server-side file by given the filename, file object, and file-stored folder path

    @Args:
    * [directory] str
    * [file] object
    * [allow_extensions] list of str

    @Returns:
    * [bool] if file saved, return true, else false
    * [str] if success, return the filename, else the failure message
    """

    # folder not exists, return false, no file could found in a vacant folder
    if not FileUtils.exists(directory):
        return False, "the folder not exists"

    if file and allowed_ext(file.filename, allow_extensions):

        # if no file exists
        if not FileUtils.exists(os.path.join(directory, file.filename)):
            return False, "the file not exists"

        # save and update the target file
        file.save(os.path.join(directory, file.filename))

        # return to caller
        return True, os.path.join(directory, file.filename)
Esempio n. 2
0
    def message(self, priority, title=None, msg=None, exception=None):
        import traceback

        # check log is validate
        if self.m_bUseLog:
            self.m_file = _check_valid_file(self.m_dir, self.m_file)

        error_line = "{0} <{1}>".format(TimeTicker.time_with_msg(),
                                        _priority_name(priority))

        if title is not None:
            error_line += "\t[{0}]".format(title)

        if msg is not None:
            error_line += "\t{0}".format(msg)

        # if SystemUtils.is_windows():
        if "Windows" in platform.system():
            error_line += "\r\n"
        else:
            error_line += "\n"

        if exception is not None:
            error = ''.join(
                traceback.format_exception(etype=type(exception),
                                           value=exception,
                                           tb=exception.__traceback__))
            error_line += "{0}".format(error)

        if self.m_bUseLog:
            FileUtils.write_file(self.m_file,
                                 Convert.string_to_binary(error_line), True)

        if self.m_bStdOut:
            print(error_line)
Esempio n. 3
0
def upload(directory: str, file: object, rename: bool, *allow_extensions):
    """
    file-upload interface, given a folder and a file from request, then saving it!

    @Args:
    * [directory] str
    * [file] request.files
    * [rename] bool
    * [allow_extensions] list of str

    @Returns:
    * [bool] if file saved, return true, else false
    * [str] if success, return the filename, else the failure message
    """

    # folder not exists, create one
    if not FileUtils.exists(directory):
        FileUtils.mkdir(directory)

    if not file:
        return False, "file is null"

    if allowed_ext(file.filename, allow_extensions):

        if rename:
            success_name = rename_file(file.filename)
            file.save(os.path.join(directory, success_name))
        else:
            success_name = file.filename
            file.save(os.path.join(directory, success_name))

        return True, success_name

    return False, "file extension is not allowed"
Esempio n. 4
0
 def __init__(self, b_stdout=True, b_log=False, directory=None, file=None):
     self.m_bStdOut = b_stdout
     self.m_bUseLog = b_log
     if self.m_bUseLog:
         FileUtils.mkdir(directory)
         self.m_file = file
         self.m_dir = directory
Esempio n. 5
0
def write_json_file(dictionary: dict, filename: str):
    """
    Write dictionary to json file
    
    @Args: 
    * [dictionary] (dict), key value pairs  
    * [filename] (str), filename with path  
    """
    if len(dictionary) > 0:
        raw = convert.string_to_binary(json.dumps(dictionary))
        fileutil.write_file(filename, raw)
Esempio n. 6
0
    def data_in_hashcode(self, priority, title: str, data: bytes):
        # check log is validate
        f = _check_valid_file(self.m_dir, self.m_file)
        line = "{0} <{1}> [{2}]".format(TimeTicker.time_with_msg(),
                                        _priority_name(priority), title)

        # if SystemUtils.is_windows():
        if "Windows" in platform.system():
            line += "\r\n    Data: {0}\r\n".format(Hashcode.md5(data))
        else:
            line += "\n    Data: {0}\n".format(Hashcode.md5(data))

        if self.m_bUseLog:
            FileUtils.write_file(f, Convert.string_to_binary(line), True)

        if self.m_bStdOut:
            print(line)
Esempio n. 7
0
def download(directory: str, filename: str):
    """
    file-download interface, download a file from the server

    @Args:
    * [directory] str
    * [filename] target file name

    @Returns:
    * [bool] if file saved, return true, else false
    * [str/link] if success, return the download file link, else the failure message
    """

    # folder not exists, return false, no file could found in a vacant folder
    if not FileUtils.exists(directory):
        return False, "the folder not exists"

    # file is vacant
    if not FileUtils.exists(os.path.join(directory, filename)):
        return False, "the file not exists"

    # given caller the download link
    return True, FileUtils.gen_file_path(directory, filename)
Esempio n. 8
0
def load_json_file(filename: str):
    """
    Create a blank file or load data from json file

    @Args:
    * [filename] (str), filename with path
    
    @Returns:  
    *[dict] if load success. None, failed, empty data
    """
    # load json file from given filename
    raw = fileutil.read_file(filename)
    if raw:
        return decode_json(convert.binary_to_string(raw))
    else:
        return {}
Esempio n. 9
0
    def parsing_reg_file(self, rule_file: str, args: dict):
        """
        Parse the configuration file, and then verify the validity of the input parameters

        @Args:
        * [ruleFile] str, path of file
        * [args] dict, data contained with key and value

        @Returns:
        * [dict(str:obj)] dict type
        """

        if not FileUtils.exists(rule_file):
            raise Exceptions.NoAvailableResourcesFoundException(
                "file: {} not found".format(rule_file))

        # parse the file
        for line in open(rule_file, "rt"):
            # trim tailor
            line = self.trim_tailer_from_text(line)

            # split token from spaces
            seps = line.split(' ')

            # check size
            if len(seps) != 3:
                raise Exceptions.InvalidArithException(
                    "regular line: {} is broken, [argument key] [mapping key] ["
                    "regular expression]".format(line))

                # append regular token to list
            self.tokens.append(
                RegularToken(seps[0], seps[1], R"{}".format(seps[2])))

        # After parsing the file, first extract the dictionary consisting of {old key-regular match} from the file,
        # and then perform regular filtering on the parameters
        filtered_args = self.use_args_regular_check(args)

        return filtered_args
Esempio n. 10
0
def siki_verified_args(request: LocalProxy, xml: object, fromFile=True):
    """
    expand the parameters from the http request and simplify the process of obtaining the parameters

    @Args:
    * [request] LocalProxy type, the request from flask
    * [xml] str type, the path of siki-style xml file, or string
    * [fromFile] default is true, means xml source comes from file, set to false, could read configurations from xml string

    @Returns:
    * [bool] success or failed
    * [dict(str:obj)] if success return http arguments, or failed with json message returned
    * [dict(str:obj)] not always, {dict: file}
    """

    # simplify the HTTP request
    flask_args = FlaskRequestSimplify.simplify_request(request)

    if flask_args[0] is None:  # no variables found
        return False, "flask arguments are none", None

    # verify parameters
    if fromFile and not FileUtils.exists(xml):
        return False, "rule file is broken", None

    # update filtered arguments
    args = ArgsUsesSikiComplianceCheck.apply_siki_rules(
        xml, flask_args[0], fromFile)

    if len(args) <= 0:
        return False, "no arguments passed the check", None

    # return to caller filtered result
    if flask_args[1]:
        return True, args, flask_args[1]

    else:
        return True, args, None
Esempio n. 11
0
def regular_verified_args(request: LocalProxy, rule: str):
    """
    expand the parameters from the http request and simplify the process of obtaining the parameters

    @Args:
    * [request] LocalProxy type, the request from flask
    * [rule] str type, the path of regular check file, or lists of regular expressions
    * [fromFile] default is true, means source comes from a file, set to false, could read configurations from list

    @Returns:
    * [bool] success or failed
    * [dict(str:obj)] if success return http arguments, or failed with json message returned
    * [dict(str:obj)] not always, {dict: file}
    """

    # simplify the HTTP request
    flask_args = FlaskRequestSimplify.simplify_request(request)

    if flask_args[0] is None:  # no variables found
        return False, "flask arguments are none", None

    # verify parameters
    if not rule or not FileUtils.exists(rule):
        return False, "rule file is broken", None

    # update filtered arguments
    args = ArgsUsesRegularExpression.apply_reg_rules(rule, flask_args[0])

    if len(args) <= 0:
        return False, "no arguments passed the check", None

    # return to caller filtered result
    if flask_args[1]:
        return True, args, flask_args[1]

    else:
        return True, args, None
Esempio n. 12
0
 def set_value(self, value):
     self._value = value
     self._md5 = FileUtils.compute_str_md5(str(value))
Esempio n. 13
0
 def __init__(self, key, value=None):
     self._key = key
     self._value = value
     self._md5 = FileUtils.compute_str_md5(str(value))
Esempio n. 14
0
def _gen_logfile(directory, file):
    import datetime
    timestamp = datetime.datetime.now().strftime("%Y%m%d")

    return FileUtils.gen_file_path(directory, file, "log", timestamp)
Esempio n. 15
0
        line = "{0} <{1}> [{2}]".format(TimeTicker.time_with_msg(),
                                        _priority_name(priority), title)

        # if SystemUtils.is_windows():
        if "Windows" in platform.system():
            line += "\r\n    Data: {0}\r\n".format(Hashcode.md5(data))
        else:
            line += "\n    Data: {0}\n".format(Hashcode.md5(data))

        if self.m_bUseLog:
            FileUtils.write_file(f, Convert.string_to_binary(line), True)

        if self.m_bStdOut:
            print(line)


if __name__ == "__main__":
    log = Logger(False, True, "logs", "test")

    try:
        raise NullPointerException("invalid ptr")
    except Exception as e:
        log.message(Priority.INFO, "title", "check message", e)

    print(log.file_path())

    raw = FileUtils.read_file("callback.py")
    log.data_in_base64(Priority.INFO, "data", raw)

    log.data_in_hashcode(Priority.ERROR, "hashcode", raw)