Skip to content

Commit

Permalink
Merge pull request #7 from moreal/document-library
Browse files Browse the repository at this point in the history
document library
  • Loading branch information
moreal committed Jun 26, 2019
2 parents 57eb225 + e53778b commit 07620f6
Show file tree
Hide file tree
Showing 20 changed files with 319 additions and 19 deletions.
23 changes: 23 additions & 0 deletions .readthedocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# .readthedocs.yml
# Read the Docs configuration file
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details

# Required
version: 2

# Build documentation in the docs/ directory with Sphinx
sphinx:
configuration: docs/conf.py

# Build documentation with MkDocs
#mkdocs:
# configuration: mkdocs.yml

# Optionally _build your docs in additional formats such as PDF and ePub
# formats: all

# Optionally set the version of Python and requirements required to _build your docs
python:
version: 3.7
install:
- requirements: docs/requirements.txt
18 changes: 9 additions & 9 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# pystructs

[![Documentation Status](https://readthedocs.org/projects/pystructs/badge/?version=latest)](https://pystructs.readthedocs.io/en/latest/?badge=latest)
[![Build Status](https://travis-ci.com/moreal/pystructs.svg?branch=master)](https://travis-ci.com/moreal/pystructs)
[![codecov](https://codecov.io/gh/moreal/pystructs/branch/master/graph/badge.svg)](https://codecov.io/gh/moreal/pystructs)

Expand Down
20 changes: 20 additions & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = .
BUILDDIR = _build

# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
56 changes: 56 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# http://www.sphinx-doc.org/en/master/config

# -- Path setup --------------------------------------------------------------

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
import sys
sys.path.insert(0, os.path.abspath('.'))


# -- Project information -----------------------------------------------------

project = 'pystructs'
copyright = '2019, Moreal'
author = 'Moreal'


# -- General configuration ---------------------------------------------------

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
"sphinx.ext.autodoc",
"sphinx.ext.viewcode",
]

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = []


# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'sphinx_rtd_theme'

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']

master_doc = 'index'
7 changes: 7 additions & 0 deletions docs/fields/field.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Field
------

.. module:: pystructs.fields

.. autoclass:: Field
:inherited-members:
8 changes: 8 additions & 0 deletions docs/fields/struct.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

Struct
------

.. module:: pystructs.fields

.. autoclass:: Struct
:inherited-members:
42 changes: 42 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
.. pystructs documentation master file, created by
sphinx-quickstart on Mon Jun 24 09:49:05 2019.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
Welcome to pystructs' documentation!
=====================================

.. code-block:: python
from typing import List
from pystructs import fields
class Attribute(fields.Struct):
type = fields.BytesField(size=1)
length = fields.Int32Field(byteorder='big')
value = fields.VariableBytesField(related_field='length')
class StunMessage(fields.Struct):
type = fields.BytesField(size=1)
length = fields.Int32Field(byteorder='big')
attributes: List[Attribute] = fields.MultipleField(count='length', field=Attribute())
message = StunMessage(<bytes>)
message.initialize()
# Just use!
.. toctree::
:maxdepth: 2
:caption: Getting Started

user/intro

.. toctree::
:maxdepth: 2
:caption: Fields

fields/field
fields/struct
35 changes: 35 additions & 0 deletions docs/make.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@ECHO OFF

pushd %~dp0

REM Command file for Sphinx documentation

if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=source
set BUILDDIR=build

if "%1" == "" goto help

%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.http://sphinx-doc.org/
exit /b 1
)

%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
goto end

:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%

:end
popd
4 changes: 4 additions & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sphinx
sphinx-rtd-theme
sphinx-autodoc-typehints
-e .
14 changes: 14 additions & 0 deletions docs/user/intro.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Introduction
============

About
-----

`pystructs` began with the desire to create a library that could handle bytes comfortably,
such as the structure in c. It was because it was too hard to parse the bytes in python.

Therefore, it had to be easy for people to use when writing the fields.
Furthermore, we hoped that there would be a function to handle variable bytes depending on the variable values.

| So, in `pystructs`, write fields into `Struct` like `model` of `django`.
| Also we can handle variable value in `pystructs`.
1 change: 1 addition & 0 deletions pystructs/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .fields import *
1 change: 1 addition & 0 deletions pystructs/fields/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from pystructs.fields.field import *
from pystructs.fields.bytes import *
from pystructs.fields.integer import *
from pystructs.fields.struct import *
Expand Down
5 changes: 5 additions & 0 deletions pystructs/fields/bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
from pystructs.fields.field import Field


__all__ = [
"BytesField"
]


class BytesField(Field):
bytes: Optional[bytes] # bytes of root field

Expand Down
62 changes: 55 additions & 7 deletions pystructs/fields/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,53 @@
from pystructs.fields import Struct


class Field:
parent: Struct = None
__all__ = [
"Field"
]


__prev: Optional[Field]
__size: int
class Field:
"""
Abstract class for parsing bytes by calculating offset and size variable
"""

def __init__(self, size: int):
self.__size = size
self.__prev = None
self.parent: Struct = None
self.__size: int = size
self.__prev: Optional[Field] = None

def fetch(self):
"""
process to work value from bytes
"""
raise NotImplementedError()

def initialize(self, root: Field):
def initialize(self, root: 'Field'):
"""
process to initialize, eg. link_fields, set bytes of root field, etc...
:param root: root field
:type root: Field
"""

raise NotImplementedError()

@property
def is_root(self) -> bool:
"""
bool value about this field is root
:type: bool
"""
return self.__prev is None

@property
def prev(self) -> Field:
"""
previous field of this, to be used in calculation offset
:type: Field
"""
return self.__prev

@prev.setter
Expand All @@ -36,8 +61,31 @@ def prev(self, field):

@property
def offset(self) -> int:
"""
offset of this
:type: int
"""
return 0 if self.is_root else (self.__prev.offset + self.__prev.size)

@property
def size(self) -> int:
"""
size of this
:type: int
"""
return self.__size

@property
def parent(self) -> Field:
"""
parent field of this, eg. Struct created by user
:type: Field
"""
return self.__parent

@parent.setter
def parent(self, value):
self.__parent = value
7 changes: 7 additions & 0 deletions pystructs/fields/integer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
from pystructs.fields.bytes import BytesField


__all__ = [
"IntField",
"Int32Field",
"Int64Field",
]


class IntField(BytesField):
def __init__(self, size: int, byteorder='little'):
super().__init__(size)
Expand Down

0 comments on commit 07620f6

Please sign in to comment.