Skip to content

Commit

Permalink
Apply black (#1158)
Browse files Browse the repository at this point in the history
  • Loading branch information
serhii73 authored Apr 10, 2023
1 parent 9234be3 commit 3173d73
Show file tree
Hide file tree
Showing 64 changed files with 12,830 additions and 9,235 deletions.
7 changes: 7 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[flake8]

max-line-length = 119
ignore = W503, E203, E501, E722, F401

exclude =
docs/conf.py
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Apply the auto-formatter black
24c1637dcc77aac006db8ebcfbf5199fa7abac5d
6 changes: 6 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,9 @@ jobs:
- name: Upload coverage.xml to codecov
if: ${{ matrix.python-version == '3.9' && matrix.env.TOXENV == 'latest'}}
uses: codecov/codecov-action@v3

pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: pre-commit/action@v3.0.0
2 changes: 2 additions & 0 deletions .isort.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[settings]
profile = black
15 changes: 15 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
repos:
- repo: https://github.com/PyCQA/flake8
rev: 6.0.0 # 6.0.0 drops Python 3.7 support
hooks:
- id: flake8
args: ['--config=.flake8']
- repo: https://github.com/psf/black.git
rev: 23.3.0
hooks:
- id: black
exclude: ^dateparser/data/date_translation_data/
- repo: https://github.com/pycqa/isort
rev: 5.12.0 # 5.12 drops Python 3.7 support
hooks:
- id: isort
3 changes: 2 additions & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ def pytest_collection_modifyitems(session, config, items):
# Avoid executing tests when executing `--flake8` flag (pytest-flake8)
try:
from pytest_flake8 import Flake8Item
if config.getoption('--flake8'):

if config.getoption("--flake8"):
items[:] = [item for item in items if isinstance(item, Flake8Item)]
except ImportError:
pass
34 changes: 26 additions & 8 deletions dateparser/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
__version__ = '1.1.8'
__version__ = "1.1.8"

from .date import DateDataParser
from .conf import apply_settings
from .date import DateDataParser

_default_parser = DateDataParser()


@apply_settings
def parse(date_string, date_formats=None, languages=None, locales=None,
region=None, settings=None, detect_languages_function=None):
def parse(
date_string,
date_formats=None,
languages=None,
locales=None,
region=None,
settings=None,
detect_languages_function=None,
):
"""Parse date and time from given date string.
:param date_string:
Expand Down Expand Up @@ -54,11 +61,22 @@ def parse(date_string, date_formats=None, languages=None, locales=None,
"""
parser = _default_parser

if languages or locales or region or detect_languages_function or not settings._default:
parser = DateDataParser(languages=languages, locales=locales,
region=region, settings=settings, detect_languages_function=detect_languages_function)
if (
languages
or locales
or region
or detect_languages_function
or not settings._default
):
parser = DateDataParser(
languages=languages,
locales=locales,
region=region,
settings=settings,
detect_languages_function=detect_languages_function,
)

data = parser.get_date_data(date_string, date_formats)

if data:
return data['date_obj']
return data["date_obj"]
48 changes: 25 additions & 23 deletions dateparser/calendars/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from datetime import datetime

from dateparser.conf import settings
from dateparser.date import DateData
from dateparser.parser import _parser
from dateparser.conf import settings
from datetime import datetime


class CalendarBase:
Expand All @@ -26,7 +27,6 @@ def get_date(self):


class non_gregorian_parser(_parser):

calendar_converter = NotImplemented
default_year = NotImplemented
default_month = NotImplemented
Expand Down Expand Up @@ -77,15 +77,16 @@ def to_latin(cls, source):
return result

def _get_datetime_obj(self, **params):
day = params['day']
year = params['year']
month = params['month']
if (
not(0 < day <= self.calendar_converter.month_length(year, month))
and not(self._token_day or hasattr(self, '_token_weekday'))
day = params["day"]
year = params["year"]
month = params["month"]
if not (0 < day <= self.calendar_converter.month_length(year, month)) and not (
self._token_day or hasattr(self, "_token_weekday")
):
day = self.calendar_converter.month_length(year, month)
year, month, day = self.calendar_converter.to_gregorian(year=year, month=month, day=day)
year, month, day = self.calendar_converter.to_gregorian(
year=year, month=month, day=day
)
c_params = params.copy()
c_params.update(dict(year=year, month=month, day=day))
return datetime(**c_params)
Expand All @@ -94,38 +95,39 @@ def _get_datetime_obj_params(self):
if not self.now:
self._set_relative_base()
now_year, now_month, now_day = self.calendar_converter.from_gregorian(
self.now.year, self.now.month, self.now.day)
self.now.year, self.now.month, self.now.day
)
params = {
'day': self.day or now_day,
'month': self.month or now_month,
'year': self.year or now_year,
'hour': 0, 'minute': 0, 'second': 0, 'microsecond': 0,
"day": self.day or now_day,
"month": self.month or now_month,
"year": self.year or now_year,
"hour": 0,
"minute": 0,
"second": 0,
"microsecond": 0,
}
return params

def _get_date_obj(self, token, directive):
year, month, day = self.default_year, self.default_month, self.default_day
token_len = len(token)
is_digit = token.isdigit()
if directive == '%A' and self._weekdays and token.title() in self._weekdays:
if directive == "%A" and self._weekdays and token.title() in self._weekdays:
pass
elif (
directive == '%m'
and token_len <= 2
and is_digit
and 1 <= int(token) <= 12
directive == "%m" and token_len <= 2 and is_digit and 1 <= int(token) <= 12
):
month = int(token)
elif directive == '%B' and self._months and token in self._months:
elif directive == "%B" and self._months and token in self._months:
month = list(self._months.keys()).index(token) + 1
elif (
directive == '%d'
directive == "%d"
and token_len <= 2
and is_digit
and 0 < int(token) <= self.calendar_converter.month_length(year, month)
):
day = int(token)
elif directive == '%Y' and token_len == 4 and is_digit:
elif directive == "%Y" and token_len == 4 and is_digit:
year = int(token)
else:
raise ValueError
Expand Down
10 changes: 5 additions & 5 deletions dateparser/calendars/hijri_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@


class hijri:

@classmethod
def to_gregorian(cls, year=None, month=None, day=None):
g = convert.Hijri(year=year, month=month, day=day, validate=False).to_gregorian()
g = convert.Hijri(
year=year, month=month, day=day, validate=False
).to_gregorian()
return g.datetuple()

@classmethod
Expand Down Expand Up @@ -35,16 +36,15 @@ def weekday(self):


class hijri_parser(non_gregorian_parser):

calendar_converter = hijri
default_year = 1389
default_month = 1
default_day = 1
non_gregorian_date_cls = HijriDate

_time_conventions = {
'am': ["صباحاً"],
'pm': ["مساءً"],
"am": ["صباحاً"],
"pm": ["مساءً"],
}

@classmethod
Expand Down
3 changes: 2 additions & 1 deletion dateparser/calendars/jalali.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from . import CalendarBase
from dateparser.calendars.jalali_parser import jalali_parser

from . import CalendarBase


class JalaliCalendar(CalendarBase):
"""Calendar class for Jalali calendar."""
Expand Down
Loading

0 comments on commit 3173d73

Please sign in to comment.