Skip to content

Commit

Permalink
Merge pull request #98 from a-thieme/code-cleanup
Browse files Browse the repository at this point in the history
code cleanup
  • Loading branch information
zjkmxy authored Oct 13, 2024
2 parents 7b56d41 + 1e3ec0b commit a22c253
Show file tree
Hide file tree
Showing 36 changed files with 153 additions and 170 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ venv.bak/
**/.DS_Store
.vscode/
.leveldb/
.pytest_cache/
repo.db
NDN_Repo.egg-info/

Expand Down
3 changes: 2 additions & 1 deletion docs/src/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,5 @@ To run ndn-python-repo with systemd on Linux, perform the following steps:

#. Examine logs::

$ sudo journalctl -u ndn-python-repo.service
$ sudo journalctl -u ndn-python-repo.service

2 changes: 1 addition & 1 deletion docs/src/misc_pkgs/client_side.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ There are four parts:

#. **PutfileClient**: insert files into the repo.
#. **GetfileClient**: get files from the repo.
#. **DeleteClient**: detele data packets from the repo.
#. **DeleteClient**: delete data packets from the repo.
#. **CommandChecker**: check process status from the repo.

The example programs in :mod:`examples/` illustrate how to use these packages.
Expand Down
2 changes: 1 addition & 1 deletion docs/src/misc_pkgs/pub_sub.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Process

Under the hood the ``PubSub`` module transmits a series of Interest and Data packets:

1. The subscriber calls ``subscribe(topic, cb)``. This makes the subcriber listen on
1. The subscriber calls ``subscribe(topic, cb)``. This makes the subscriber listen on
``"/<topic>/notify"``.

2. The publisher invokes ``publish(topic, msg)``. This method sends an Interest with name
Expand Down
4 changes: 2 additions & 2 deletions docs/src/specification/security.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Required Settings

.. warning::
Unfortunately current implementation does not follow these requirements by default.
This may cause some potential vulnerbilities. Will be fixed in future versions.
This may cause some potential vulnerabilities. Will be fixed in future versions.


Recommended Settings
Expand All @@ -31,7 +31,7 @@ Recommended Settings

- Since there is no replay attack, the Repo does not have to remember the list of ``SignatureNonce``.

- If the Repo is provided as a network service, the certifcate obtained from the network provider should be used.
- If the Repo is provided as a network service, the certificate obtained from the network provider should be used.
In this case, the prefix registration command and repo's publication data are signed using the same key.

- For example, if one employs a Repo as a public service of NDN testbed,
Expand Down
2 changes: 0 additions & 2 deletions examples/command_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
"""

import argparse
import asyncio as aio
import logging
from ndn.app import NDNApp
from ndn.encoding import Name
from ndn.security import KeychainDigest
from ndn_python_repo.clients import CommandChecker


Expand Down
4 changes: 1 addition & 3 deletions examples/delfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
"""

import argparse
import asyncio as aio
import logging
from ndn.app import NDNApp
from ndn.encoding import Name
from ndn.security import KeychainDigest
from ndn_python_repo.clients import DeleteClient


Expand Down Expand Up @@ -61,7 +59,7 @@ def main():
# process default values
start_block_id = int(args.start_block_id) if args.start_block_id else None
end_block_id = int(args.end_block_id) if args.end_block_id else None
if args.register_prefix == None:
if args.register_prefix is None:
args.register_prefix = args.name_at_repo
args.register_prefix = Name.from_str(args.register_prefix)

Expand Down
2 changes: 0 additions & 2 deletions examples/getfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
"""

import argparse
import asyncio as aio
import logging
from ndn.app import NDNApp
from ndn.encoding import Name
from ndn.security import KeychainDigest
from ndn_python_repo.clients import GetfileClient


Expand Down
2 changes: 0 additions & 2 deletions examples/leavesync.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#!/usr/bin/env python3
import argparse
import logging
from ndn.app import NDNApp
from ndn.encoding import Name
from ndn.security import KeychainDigest
from ndn_python_repo.clients import SyncClient
import uuid

async def run_leave_sync_client(app: NDNApp, **kwargs):
client = SyncClient(app=app, prefix=kwargs['client_prefix'], repo_name=kwargs['repo_name'])
Expand Down
2 changes: 1 addition & 1 deletion examples/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async def run_publisher(app: NDNApp, publisher_prefix: NonStrictName):
msg = f'pubsub message generated at {str(datetime.datetime.now())}'.encode()
await pb.publish(topic, msg)

# wait for msg to be fetched by subsciber
# wait for msg to be fetched by subscriber
await aio.sleep(10)
app.shutdown()

Expand Down
3 changes: 1 addition & 2 deletions examples/putfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"""

import argparse
import asyncio as aio
import logging
import multiprocessing
from ndn.app import NDNApp
Expand Down Expand Up @@ -72,7 +71,7 @@ def main():
level=logging.INFO)

# ``register_prefix`` is by default identical to ``name_at_repo``
if args.register_prefix == None:
if args.register_prefix is None:
args.register_prefix = args.name_at_repo
args.register_prefix = Name.from_str(args.register_prefix)
if args.forwarding_hint:
Expand Down
1 change: 0 additions & 1 deletion examples/subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
@Date 2020-05-10
"""

import asyncio as aio
import logging
from ndn.app import NDNApp
from ndn.encoding import Name, NonStrictName
Expand Down
1 change: 0 additions & 1 deletion examples/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from ndn.encoding import Name
from ndn.security import KeychainDigest
from ndn_python_repo.clients import SyncClient
import uuid


async def run_sync_client(app: NDNApp, **kwargs):
Expand Down
8 changes: 4 additions & 4 deletions ndn_python_repo/clients/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@
import sys
sys.path.insert(1, os.path.join(sys.path[0], '..'))

import argparse
import asyncio as aio
from ..command import RepoCommandParam, ObjParam, EmbName, RepoStatCode
from .command_checker import CommandChecker
from ..utils import PubSub
import logging
from ndn.app import NDNApp
from ndn.encoding import Name, Component, DecodeError, NonStrictName
from ndn.encoding import Name, NonStrictName
from typing import Optional
from hashlib import sha256

Expand Down Expand Up @@ -46,7 +45,7 @@ async def delete_file(self, prefix: NonStrictName, start_block_id: int = 0,
:param prefix: NonStrictName. The name of the file stored in the remote repo.
:param start_block_id: int. Default value is 0.
:param end_block_id: int. If not specified, repo will attempt to delete all data packets\
with segment number starting from `start_block_id` continously.
with segment number starting from `start_block_id` continuously.
:param register_prefix: If repo is configured with ``register_root=False``, it unregisters\
``register_prefix`` after receiving the deletion command.
:param check_prefix: NonStrictName. The repo will publish process check messages under\
Expand Down Expand Up @@ -89,9 +88,10 @@ async def _wait_for_finish(self, check_prefix: NonStrictName, request_no: bytes)
:param check_prefix: NonStrictName. The prefix under which the check message will be\
published.
:param process_id: int. The process id to check for delete process
:param request_no: int. The request number to check for delete process (formerly process id)
:return: Number of deleted packets.
"""
# fixme: why is check_prefix never used?
checker = CommandChecker(self.app)
n_retries = 3
while n_retries > 0:
Expand Down
7 changes: 4 additions & 3 deletions ndn_python_repo/clients/putfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def _on_interest(self, int_name, _int_param, _app_param):
self.logger.info(f'On interest: {Name.to_str(int_name)}')
seq = Component.to_number(int_name[-1])
name_wo_seq = Name.to_str(int_name[:-1])
if name_wo_seq in self.encoded_packets and seq >= 0 and seq < len(self.encoded_packets[name_wo_seq]):
if name_wo_seq in self.encoded_packets and 0 <= seq < len(self.encoded_packets[name_wo_seq]):
encoded_packets = self.encoded_packets[name_wo_seq]
self.app.put_raw_packet(encoded_packets[seq])
self.logger.info(f'Serve data: {Name.to_str(int_name)}')
Expand Down Expand Up @@ -188,9 +188,10 @@ async def _wait_for_finish(self, check_prefix: NonStrictName, request_no: bytes)
:param check_prefix: NonStrictName. The prefix under which the check message will be\
published.
:param process_id: int. The process id to check.
:return: number of inserted packets.
:param request_no: bytes. The request number to check.
:return: int number of inserted packets.
"""
# fixme: why is check_prefix not used?
checker = CommandChecker(self.app)
n_retries = 5
while n_retries > 0:
Expand Down
8 changes: 2 additions & 6 deletions ndn_python_repo/clients/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,14 @@
import sys
sys.path.insert(1, os.path.join(sys.path[0], '..'))

import asyncio as aio
from .command_checker import CommandChecker
from ..command import RepoCommandParam, SyncParam, EmbName, RepoStatCode
from ..command import RepoCommandParam, SyncParam
from ..utils import PubSub
import logging
import multiprocessing
from ndn.app import NDNApp
from ndn.encoding import Name, NonStrictName, Component, Links
from ndn.encoding import Name, NonStrictName
import os
import platform
from hashlib import sha256
from typing import Optional, List

class SyncClient(object):

Expand Down
2 changes: 0 additions & 2 deletions ndn_python_repo/cmd/install.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import os
import platform
import shutil
import sys
# from pkg_resources import resource_filename
import importlib.resources


Expand Down
5 changes: 1 addition & 4 deletions ndn_python_repo/cmd/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import argparse
import asyncio as aio
import logging
# import pkg_resources
import importlib.metadata
import sys
from ndn.app import NDNApp
from ndn.encoding import Name
from ndn_python_repo import *


Expand Down Expand Up @@ -43,7 +40,7 @@ def process_config(cmdline_args):
Read and process config file. Some config options are overridden by cmdline args.
"""
config = get_yaml(cmdline_args.config)
if cmdline_args.repo_name != None:
if cmdline_args.repo_name is not None:
config['repo_config']['repo_name'] = cmdline_args.repo_name
return config

Expand Down
9 changes: 5 additions & 4 deletions ndn_python_repo/cmd/port.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# noinspection GrazieInspection
"""
This script ports sqlite db file from repo-ng to ndn-python-repo.
It takes as input a repo-ng sqlite database file, traverse the database and inserts data to
It takes as input a repo-ng sqlite database file, traverses the database, and inserts data into
an ndn-python-repo using TCP bulk insertion.
@Author [email protected]
Expand All @@ -12,7 +13,7 @@
import os
import sqlite3
import sys
from ndn.encoding import Name, Component, ndn_format_0_3, tlv_var
from ndn.encoding import Name, ndn_format_0_3, tlv_var


def create_sqlite3_connection(db_file):
Expand Down Expand Up @@ -71,9 +72,9 @@ def main() -> int:
required=True, help='Port of python repo')
args = parser.parse_args()

if args.addr == None:
if args.addr is None:
args.addr = '127.0.0.1'
if args.port == None:
if args.port is None:
args.addr = '7376'

src_db_file = os.path.expanduser(args.dbfile)
Expand Down
31 changes: 23 additions & 8 deletions ndn_python_repo/handle/command_handle_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
from ndn.app import NDNApp
from ndn.encoding import Name, NonStrictName, FormalName, Component
from ndn.encoding.tlv_model import DecodeError
from typing import List, Dict

from ..command import RepoStatQuery, RepoCommandRes, RepoStatCode, RepeatedNames
from ..command import RepoStatQuery, RepoCommandRes, RepoStatCode, RepeatedNames, RepoCommandParam
from ..storage import Storage
from ..utils import PubSub

from hashlib import sha256

class CommandHandle(object):
"""
Expand Down Expand Up @@ -65,6 +65,21 @@ async def _delete_process_state_after(self, process_id: bytes, delay: int):
if process_id in self.m_processes:
del self.m_processes[process_id]

def parse_msg(self, msg):
try:
cmd_param = RepoCommandParam.parse(msg)
request_no = sha256(bytes(msg)).digest()
if not cmd_param.objs:
raise DecodeError('Missing objects')
for obj in cmd_param.objs:
if obj.name is None:
raise DecodeError('Missing name for one or more objects')
except (DecodeError, IndexError) as exc:
self.logger.warning(f'Parameter interest blob decoding failed w/ exception: {exc}')
return

return cmd_param, request_no

@staticmethod
def add_name_to_set_in_storage(set_name: str, storage: Storage, name: NonStrictName) -> bool:
"""
Expand All @@ -91,7 +106,7 @@ def add_name_to_set_in_storage(set_name: str, storage: Storage, name: NonStrictN
return False

@staticmethod
def get_name_from_set_in_storage(set_name: str, storage: Storage) -> List[FormalName]:
def get_name_from_set_in_storage(set_name: str, storage: Storage) -> list[FormalName]:
"""
Get all names from set ``set_name`` in the storage.
:param set_name: str
Expand Down Expand Up @@ -131,14 +146,14 @@ def remove_name_from_set_in_storage(set_name: str, storage: Storage, name: NonSt

# this will overwrite
@staticmethod
def add_dict_in_storage(dict_name: str, storage: Storage, dict: Dict) -> bool:
def add_dict_in_storage(dict_name: str, storage: Storage, s_dict: dict) -> bool:
ret = storage._get(dict_name.encode('utf-8'))
dict_bytes = json.dumps(dict).encode('utf-8')
dict_bytes = json.dumps(s_dict).encode('utf-8')
storage._put(dict_name.encode('utf-8'), dict_bytes)
return (ret is not None)
return ret is not None

@staticmethod
def get_dict_in_storage(dict_name: str, storage: Storage) -> Dict:
def get_dict_in_storage(dict_name: str, storage: Storage) -> dict:
res_bytes = storage._get(dict_name.encode('utf-8'))
return json.loads(res_bytes.decode('utf-8'))

Expand Down Expand Up @@ -166,7 +181,7 @@ def remove_registered_prefix_in_storage(storage: Storage, prefix):
return ret

@staticmethod
def add_sync_states_in_storage(storage: Storage, sync_group: FormalName, states: Dict):
def add_sync_states_in_storage(storage: Storage, sync_group: FormalName, states: dict):
store_key = [Component.from_str('sync_states')] + sync_group
logging.info(f'Added new sync states to storage: {Name.to_str(sync_group)}')
return CommandHandle.add_dict_in_storage(Name.to_str(store_key), storage, states)
Expand Down
Loading

0 comments on commit a22c253

Please sign in to comment.