Пример #1
0
def _get_cached_value(hashsum, func_get_value):
    os.makedirs(TEMP_FOLDER, exist_ok=True)

    path = os.path.join(TEMP_FOLDER, hashsum)
    logger.debug(f"Using history file: {path}")

    if not os.path.exists(path):
        data = func_get_value()
        save_file(path, pickle.dumps(data), mode="wb", encoding=None)

    data = pickle.loads(read_content(path, mode="rb", encoding=None))
    return data
Пример #2
0
    def post(self, id):
        try:
            user_id = get_jwt_identity()

            if user_id != id:
                return {'error': True, 'payload': 'Not authorised'}, 401

            if 'file' not in request.files:
                return {'error': True, 'payload': 'File not found'}, 404

            file = request.files['file']
            user = User.objects.get(id=id)

            if request.files and 'file' in request.files:
                file = request.files['file']
                filename = str(user.id) + file.filename

                if save_file(filename, file):
                    user.update(image=filename)

                return {'image': filename}, 200
            else:
                return {'error': True, 'payload': 'Something went wrong'}, 400
        except InvalidQueryError:
            raise SchemaValidationError
        except DoesNotExist:
            raise DocumentMissing
        except Exception:
            raise InternalServerError
Пример #3
0
    def post(self):
        try:
            user_id = get_jwt_identity()
            user = User.objects.get(id=user_id)

            body = {
                'title': request.form['title'],
                'content': request.form['content'],
                'hashtags': request.form.getlist('hashtags[]')
            }
            post = Post(**body, author=user)
            post.save()

            if request.files and 'file' in request.files:
                file = request.files['file']
                filename = str(post.id) + file.filename

                if save_file(filename, file):
                    post.update(set__image=filename)

            socketio.emit('update', {}, room='mentor' + str(user.id))

            return {'id': str(post.id)}, 200
        except (FieldDoesNotExist, ValidationError):
            raise SchemaValidationError
        except DoesNotExist:
            raise DocumentMissing
        except Exception as e:
            raise InternalServerError
Пример #4
0
    def post(self, id):
        try:
            user_id = get_jwt_identity()
            chat = Chat.objects.get(id=id)
            user = User.objects.get(id=user_id)

            message = Message(text=request.form['text'], author=user)
            chat.messages.append(message)

            message.save()
            chat.save()

            if 'file[]' in request.files:
                filenames = []
                files = request.files.getlist('file[]')
                for file in files:
                    filename = str(message.id) + file.filename
                    if save_file(filename, file):
                        filenames.append(filename)

                message.images = filenames
                message.save()

            socketio.emit('update', {}, room='chat' + str(chat.id))

            return { 'id': str(message.id) }, 200
        except DoesNotExist:
            raise DocumentMissing
        except Exception as e:
            raise InternalServerError
Пример #5
0
    def put(self, id):
        try:
            post = Post.objects.get(id=id)
            body = {
                'title': request.form['title'],
                'content': request.form['content'],
                'hashtags': request.form.getlist('hashtags[]')
            }

            if request.files and 'file' in request.files:
                file = request.files['file']
                filename = str(post.id) + file.filename

                if save_file(filename, file):
                    body['image'] = filename

            post.update(**body)
            return '', 200
        except InvalidQueryError:
            raise SchemaValidationError
        except DoesNotExist:
            raise DocumentMissing
        except Exception:
            raise InternalServerError
Пример #6
0
def shapeshifter_cli():
    args = parse_arguments()

    data_source_path = args.data_source_path

    config_filepath = "{0}/{1}".format(os.path.dirname(os.path.abspath(__file__)), CONFIG_FILENAME)
    config = load_config(config_filepath)

    paging_config = None
    if args.page_iterator and args.page_range:
        [min_range, max_range] = args.page_range.split(",")
        paging_config = {
            "iterator": args.page_iterator,
            "min": int(min_range),
            "max": int(max_range)
        }

    shapeshifter = Shapeshifter(config=config, delimiter=args.delimiter, quotechar=args.quotechar, headers=args.headers, paging=paging_config)

    if data_source_path.startswith("http://") or data_source_path.startswith("https://"):
        input_datatype = "json"

        if args.keep_datatype:
            output_datatype = input_datatype
        else:
            output_datatype = "csv"
        
        output_filepath = args.output_filepath if args.output_filepath else "{0}.{1}".format(
            "web_datasource", output_datatype)
    else:
        input_datatype = check_filetype(args.data_source_path)
        if input_datatype not in Shapeshifter.SUPPORTED_DATA_TYPES:
            print("File extension not supported (check if it was a .json or .csv file)")
            return

        if args.keep_datatype:
            output_datatype = input_datatype
        else:
            output_datatype = "json" if input_datatype == "csv" else "csv"

        # If no output filepath was supplied, use the same filepath as the input and just switch the filetype
        output_filepath = args.output_filepath if args.output_filepath else "{0}.{1}".format(
            args.data_source_path.split(".")[0], output_datatype)

    if input_datatype == "json":
        shapeshifter.from_json(data_source_path)
    elif input_datatype == "csv":
        shapeshifter.from_csv(data_source_path)
    else:
        print("Whatever you did to get here was definitely not supported")
        return

    if len(args.include) > 0:
        include_list = args.include.split(ARG_DELIMITER)
        shapeshifter.include(*include_list)

    if len(args.exclude) > 0:
        exclude_list = args.exclude.split(ARG_DELIMITER)
        shapeshifter.exclude(*exclude_list)

    if len(args.only) > 0:
        only_list = args.only.split(ARG_DELIMITER)
        shapeshifter.only(*only_list)
    
    if args.filter_by_value:
        [filter_field, filter_operator, filter_value] = args.filter_by_value.split(" ")
        shapeshifter.filter(filter_field, filter_operator, filter_value)

    if output_datatype == "json":
        output_data = shapeshifter.to_json()
    elif output_datatype == "csv":
        output_data = shapeshifter.to_csv()
    else:
        print("Whatever you did to get here was definitely not supported")
        return

    save_file(output_filepath, output_data)
    print("File saved to %s" % output_filepath)
Пример #7
0
from utils import path, file
from word import Word

IMAGE_PATH = path.get_file_path("test1.jpg", "in", "test")

word = Word.read_from_img(IMAGE_PATH)
img = word.print_to_img()
file.save_file(img, file_name='test1.jpg', data_attr='test')
print(img)
# img = io.print_to_img(text)