Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Текстовый архив с файлами к ответу на вопрос https://ru.stackoverflow.com/questions/1610502/
- Скрипт для извлечения файлов из текстового архива
- """
- Скрипт извлекает файлы из текстового архива.
- python script.py <путь_к_архиву>
- """
- import logging
- import os
- import sys
- def extract_files(archive_file):
- filename = None
- chunk = []
- with open(archive_file, "rb") as file:
- for line in file:
- if line.startswith(b"--- "):
- if filename:
- body = b"".join(chunk[:-1])
- os.makedirs(os.path.dirname(filename), exist_ok=True)
- with open(filename, "wb") as output_file:
- output_file.write(body)
- logging.info(f"Извлечен файл: {filename}")
- filename = line[4:].strip()
- chunk = []
- elif filename is not None:
- chunk.append(line)
- if __name__ == "__main__":
- for file in sys.argv[1:]:
- extract_files(file)
- --- ./go.mod
- module statistic
- go 1.23.6
- --- ./internal/config/config.go
- package config
- type Config struct {
- Postgres string
- }
- func MustLoadByPath(path string) Config {
- return Config{}
- }
- --- ./internal/postgres/postgres.go
- package postgres
- import (
- "context"
- "log/slog"
- )
- type Pool struct{}
- func (*Pool) Ping(context.Context) error {
- return nil
- }
- type Db struct {
- Pool *Pool
- }
- func NewStorageWithPool(ctx context.Context, postgres string, log *slog.Logger) (Db, error) {
- return Db{&Pool{}}, nil
- }
- --- ./cmd/main.go
- package main
- import (
- "context"
- "log/slog"
- "os"
- "statistic/internal/config"
- "statistic/internal/postgres"
- )
- func main() {
- log := slog.New(
- slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}),
- )
- cfg := config.MustLoadByPath("./config/config.yaml")
- ctx := context.Background()
- db, err := postgres.NewStorageWithPool(ctx, cfg.Postgres, log)
- if err != nil {
- log.Error("failed init db " + err.Error())
- return
- }
- if err = db.Pool.Ping(ctx); err == nil {
- log.Info("db connected suc")
- } else {
- log.Info("db connected failed " + err.Error())
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment