Skip to content

Commit

Permalink
基本存档读写
Browse files Browse the repository at this point in the history
  • Loading branch information
XcantloadX committed Jul 17, 2024
0 parents commit 14c104f
Show file tree
Hide file tree
Showing 7 changed files with 576 additions and 0 deletions.
162 changes: 162 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
.pdm.toml
.pdm-python
.pdm-build/

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.analysis.typeCheckingMode": "basic"
}
Empty file added app/deserializer/__init__.py
Empty file.
144 changes: 144 additions & 0 deletions app/deserializer/deserializer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
from typing import Any, TypeVar, Type, get_type_hints, get_origin, get_args
from struct import unpack, calcsize
from logging import getLogger
from io import IOBase, BufferedReader

from deserializer.types import *

logger = getLogger(__name__)

T = TypeVar('T')

def deserialize(data: BufferedReader, t: 'Type[T]') -> T:
if _is_primitive(t):
return _read_primitive(data, t) # type: ignore
elif get_origin(t) == FixedString:
return _read_fixed_string(data, t) # type: ignore
elif get_origin(t) == FixedArray:
return _read_fixed_array(data, t) # type: ignore
else:
return _read_object(data, t)

_s = 0
def _read_object(data: BufferedReader, t: 'Type[T]') -> 'tuple[int, T]':
ins = _make_ins(t)

total_size = 0

for field_name, field_type in get_type_hints(t).items():
logger.debug('Deserializing: field %s with type %s', field_name, field_type)
if field_type == None:
raise ValueError(f"Field {field_name} has no type annotation")

if _is_primitive(field_type):
logger.debug('Detected: primitive type. Trying to deserialize.')
size, value = _read_primitive(data, field_type)
total_size += size
# data = data[size:]

elif get_origin(field_type) == FixedString:
logger.debug('Detected: FixedString type. Trying to deserialize.')
size, value = _read_fixed_string(data, field_type)
total_size += size
# data = data[size:]

# 对于泛型类型,FixedArray[Int8] 和 FixedArray[Int16] 和 FixedArray
# 是不同的类型,不能直接 == 比较
elif get_origin(field_type) == FixedArray:
logger.debug('Detected: FixedArray type. Trying to deserialize.')
size, value = _read_fixed_array(data, field_type)
total_size += size
# data = data[size:]
else:
logger.debug('Detected: custom object type. Trying to deserialize.')
size, value = _read_object(data, field_type)
total_size += size
# data = data[size:]

print(field_name, field_type, value)

# 设置属性
setattr(ins, field_name, value)
global _s
_s += total_size
print(_s)
return total_size, ins

def _make_ins(t: type) -> Any:
"""
根据传入的类型,返回该类型的实例
"""
if get_origin(t) == FixedArray:
return FixedArray()
elif get_origin(t) == FixedString:
return FixedString()

else:
return t()

def _is_primitive(t: type) -> bool:
return any(t == pt for pt in (
Int8, Int16, Int32, Int64,
UInt8, UInt16, UInt32, UInt64,
byte, short, int_, long,
ubyte, ushort, uint, ulong,
))

Primitives = TypeVar('Primitives', Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64)
def _read_primitive(data: BufferedReader, t: 'Type[Primitives]') -> 'tuple[int, Primitives]':
fmt = {
Int8: 'b',
Int16: 'h',
Int32: 'i',
Int64: 'q',
UInt8: 'B',
UInt16: 'H',
UInt32: 'I',
UInt64: 'Q',
# byte、short、int_、long 只是别名,无需重复
}[t]
size = calcsize(fmt)
return size, unpack(fmt, data.read(size))[0]

def _read_fixed_string(data: BufferedReader, t: FixedString) -> 'tuple[int, str]':
str_len = get_args(get_args(t)[0])[0]
return str_len, data.read(str_len).decode('utf-8').rstrip('\x00')

def _read_fixed_array(data: BufferedReader, t: FixedArray) -> 'tuple[int, FixedArray]':
ele_type, arr_len = get_args(t)
arr_len = get_args(arr_len)[0]
arr = []
total_size = 0
for _ in range(arr_len):
size, ele = deserialize(data, ele_type)
arr.append(ele)
total_size += size
return total_size, FixedArray(arr)


def _calc_size(t: type, size: int = 0):
if _is_primitive(t):
return size + calcsize({
Int8: 'b',
Int16: 'h',
Int32: 'i',
Int64: 'q',
UInt8: 'B',
UInt16: 'H',
UInt32: 'I',
UInt64: 'Q',
}[t])
elif get_origin(t) == FixedString:
return size + get_args(get_args(t)[0])[0]
elif get_origin(t) == FixedArray:
ele_type, arr_len = get_args(t)
arr_len = get_args(arr_len)[0]
return _calc_size(ele_type, size) * arr_len
else:
return sum(_calc_size(v, size) for k, v in get_type_hints(t).items())

def calc_size(t: type):
"""
计算结构体大小
"""
return _calc_size(t)
Loading

0 comments on commit 14c104f

Please sign in to comment.