Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: [p1c2u]
33 changes: 33 additions & 0 deletions .github/workflows/python-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Test python code

on:
push:
pull_request:
types: [opened, synchronize]

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [2.7, 3.5, 3.6, 3.7, 3.8, 3.9]
fail-fast: false
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install -r requirements_dev.txt
pip install -e .
- name: Test
run: python setup.py test
- name: Upload coverage
uses: codecov/codecov-action@v1
2 changes: 0 additions & 2 deletions dictpath/accessors.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"""Dictpath accessors module"""
from contextlib import contextmanager

from six import iteritems


class DictOrListAccessor(object):

Expand Down
39 changes: 39 additions & 0 deletions dictpath/parsers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""Dictpath parsers module"""
from six import text_type


def parse_parts(parts, sep='/'):
parsed = []
it = reversed(parts)
for part in it:
if isinstance(part, int):
parsed.append(part)
if not part:
continue
if sep in part:
for x in reversed(part.split(sep)):
if x and x != '.':
parsed.append(x)
else:
if part and part != '.':
parsed.append(part)
parsed.reverse()
return parsed


def parse_args(args, sep='/'):
parts = []
for a in args:
if hasattr(a, 'parts'):
parts += a.parts
else:
if isinstance(a, text_type):
parts.append(a)
elif isinstance(a, int):
parts.append(a)
else:
raise TypeError(
"argument should be a text object or a Path "
"object returning str, not %r"
% type(a))
return parse_parts(parts, sep)
20 changes: 5 additions & 15 deletions dictpath/paths.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Dictpath paths module"""
from contextlib import contextmanager
import io
import sys

from six import text_type

Expand All @@ -13,7 +11,8 @@

class BasePath(object):

def __init__(self, *args, separator=SEPARATOR):
def __init__(self, *args, **kwargs):
separator = kwargs.pop('separator', SEPARATOR)
self.parts = parse_args(args)
self.separator = separator

Expand Down Expand Up @@ -96,19 +95,16 @@ def __ge__(self, other):

class AccessorPath(BasePath):

def __init__(
self, accessor, *args, separator=SEPARATOR,
):
def __init__(self, accessor, *args, **kwargs):
separator = kwargs.pop('separator', SEPARATOR)
super(AccessorPath, self).__init__(
*args, separator=separator)
self.accessor = accessor

@classmethod
def _from_parsed_parts(cls, accessor, parts, separator=SEPARATOR):
self = super(AccessorPath, cls).__new__(cls)
self.accessor = accessor
self = cls(accessor, separator=separator)
self.parts = parts
self.separator = separator
return self

def __iter__(self):
Expand Down Expand Up @@ -165,12 +161,6 @@ def get(self, key, default=None):
return self / key
return default

@classmethod
def _from_parsed_parts(cls, accessor, parts, separator=SEPARATOR):
self = cls(accessor, separator=separator)
self.parts = parts
return self

def _make_child(self, args):
parts = parse_args(args, self.separator)
parts_joined = self.parts + parts
Expand Down
7 changes: 4 additions & 3 deletions tests/unit/test_parsers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from six import u

from dictpath.paths import BasePath
from dictpath.parsers import parse_parts, parse_args
Expand Down Expand Up @@ -74,21 +75,21 @@ def test_empty(self):
assert result == []

def test_string(self):
args = ['test']
args = [u('test')]

result = parse_args(args, self.separator)

assert result == ['test']

def test_string_many(self):
args = ['test', 'test2']
args = [u('test'), u('test2')]

result = parse_args(args, self.separator)

assert result == ['test', 'test2']

def test_path(self):
args = [BasePath('test')]
args = [BasePath(u('test'))]

result = parse_args(args, self.separator)

Expand Down
Loading