Skip to content

Commit

Permalink
New generator (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
tolstislon committed Jul 11, 2024
1 parent 95b2b39 commit e233b39
Show file tree
Hide file tree
Showing 19 changed files with 744 additions and 1,166 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
flake8 phone_gen
- name: Black
run: |
black --check phone_gen
black --line-length=120 --check phone_gen
- name: Test with pytest
run: |
pytest tests
6 changes: 5 additions & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ pytest-cov = "==5.0.0"
requests = "==2.32.3"

[packages]
phone-gen = {editable = true, path = "."}
phone-gen = { editable = true, path = "." }

[requires]
python_version = "3.11"

[scripts]
tests = "pytest ./tests"
black = "black --line-length=120 phone_gen"
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ Resources
----
* [Google's libphonenumber](https://github.com/google/libphonenumber)
* Modified [strgen](https://github.com/paul-wolf/strgen) library

Changelog
----
Expand Down
43 changes: 7 additions & 36 deletions dev_tools/patterns_generator.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import argparse
import io
import json
import re
import tarfile
import tempfile
import xml.etree.ElementTree as ElementTree
from datetime import datetime
from pathlib import Path
from re import findall, match
from re import match
from typing import Dict, Generator, Tuple, Final

import requests
Expand All @@ -15,7 +16,7 @@

TEMPLATE: Final[str] = """# -*- coding: utf-8 -*-
\"""
Auto-generated file {datetime} UTC
Auto-generated file {datetime} UTC (v3)
Resource: https://github.com/google/libphonenumber {version}
\"""
Expand All @@ -35,46 +36,16 @@ def arg_parser() -> argparse.Namespace:
add_help=True,
description="Pattern generator to phone-gen",
)
parser.add_argument(
"-t", "--tag", dest="tag", help="libphonenumber tag", default="latest"
)
parser.add_argument("-t", "--tag", dest="tag", help="libphonenumber tag", default="latest")
return parser.parse_args()


class RegexCompiler:
_replace_values: Tuple[Tuple[str, str], ...] = (
("\n", ""),
(" ", ""),
("?:", ""),
(r"\d", r"[\d]"),
(",", ":"),
)

def __init__(self, pattern: str):
self.pattern = f"({self._replace(pattern)})"

def _replace(self, pattern: str) -> str:
for i in self._replace_values:
pattern = pattern.replace(*i)
return pattern

def _group(self, group: str) -> str:
groups = findall(r"\((.*)\)", group)
if groups:
for i in groups:
group.replace(group, self._group(i))
return "|".join(f"({i})" for i in group.split("|"))

def compile(self) -> str:
return self.pattern.replace(self.pattern, self._group(self.pattern))


class Parser:
def __init__(self, source: str):
self.root = ElementTree.fromstring(source)
self.line_tag = "fixedLine"
self.pattern_tag = "nationalNumberPattern"
self.mobile_tag = 'mobile'
self.mobile_tag = "mobile"

def render(self) -> Generator[Tuple[str, Dict[str, str]], None, None]:
for territory in self.root.iter("territory"):
Expand All @@ -84,10 +55,10 @@ def render(self) -> Generator[Tuple[str, Dict[str, str]], None, None]:
value = {"code": attrs.get("countryCode", "")}
for fixed_line in territory.iter(self.line_tag):
for national_number_pattern in fixed_line.iter(self.pattern_tag):
value["pattern"] = RegexCompiler(national_number_pattern.text).compile()
value["pattern"] = re.sub(r"\s", "", national_number_pattern.text)
for mobile_tag in territory.iter(self.mobile_tag):
for national_number_pattern in mobile_tag.iter(self.pattern_tag):
value["mobile"] = RegexCompiler(national_number_pattern.text).compile()
value["mobile"] = re.sub(r"\s", "", national_number_pattern.text)

yield code, value

Expand Down
83 changes: 39 additions & 44 deletions dev_tools/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from dev_tools.patterns_generator import get_latest, parsing_version, main

ROOT_DIR: Final[Path] = Path(__file__).absolute().parent.parent
logging.basicConfig(format='[%(asctime)s] %(levelname)s:%(message)s', level=logging.INFO)
logging.basicConfig(format="[%(asctime)s] %(levelname)s:%(message)s", level=logging.INFO)


def arg_parser() -> argparse.Namespace:
Expand All @@ -19,97 +19,92 @@ def arg_parser() -> argparse.Namespace:
add_help=True,
description="Update phone-gen patterns",
)
parser.add_argument("-u", "--update", dest="update", help="Need to update?", action="store_true")
parser.add_argument(
"-u", "--update", dest="update", help="Need to update?", action='store_true'
)
parser.add_argument(
"-p", "--ignore-phonenumbers", dest="ignore_phonenumbers", help="Ignore phonenumbers versions",
action='store_true'
)
parser.add_argument(
"-b", "--no-black", dest="no_black", help="Do not run black", action='store_true'
)
parser.add_argument(
"-f", "--no-flake", dest="no_flake", help="Do not run flake8", action='store_true'
)
parser.add_argument(
"-t", "--no-tests", dest="no_tests", help="Do not run tests", action='store_true'
"-p",
"--ignore-phonenumbers",
dest="ignore_phonenumbers",
help="Ignore phonenumbers versions",
action="store_true",
)
parser.add_argument("-b", "--no-black", dest="no_black", help="Do not run black", action="store_true")
parser.add_argument("-f", "--no-flake", dest="no_flake", help="Do not run flake8", action="store_true")
parser.add_argument("-t", "--no-tests", dest="no_tests", help="Do not run tests", action="store_true")
return parser.parse_args()


def is_need_update(version: str) -> bool:
if (pattern_file := ROOT_DIR / 'phone_gen' / 'patterns.py').exists():
with pattern_file.open('r') as _file:
if (pattern_file := ROOT_DIR / "phone_gen" / "patterns.py").exists():
with pattern_file.open("r") as _file:
file = _file.read()
if match := re.findall(r'libphonenumber v([\d.]+)"', file):
return version != match[0]
return True


def check_phonenumbers_libs(version: str) -> bool:
response = requests.get('https://pypi.org/pypi/phonenumbers/json')
response = requests.get("https://pypi.org/pypi/phonenumbers/json")
response.raise_for_status()
releases = response.json()['releases']
releases = response.json()["releases"]
return version in releases.keys()


def update_pipfile(version: str) -> None:
logging.info('Update Pipfile')
if (pipfile_path := ROOT_DIR / 'Pipfile').exists():
with pipfile_path.open('r') as _file:
logging.info("Update Pipfile")
if (pipfile_path := ROOT_DIR / "Pipfile").exists():
with pipfile_path.open("r") as _file:
pipfile = _file.read()
pipfile = re.sub(r'phonenumbers\s?=\s?"==[\d.]+"', f'phonenumbers = "=={version}"', pipfile)
with pipfile_path.open('w') as _file:
with pipfile_path.open("w") as _file:
_file.write(pipfile)
return
logging.critical('Not found Pipfile')
logging.critical("Not found Pipfile")
exit(-1)


def update_workflow(version: str) -> None:
logging.debug('Update workflow')
if (workflow_path := ROOT_DIR / '.github' / 'workflows' / 'python-package.yml').exists():
with workflow_path.open('r') as _file:
logging.debug("Update workflow")
if (workflow_path := ROOT_DIR / ".github" / "workflows" / "python-package.yml").exists():
with workflow_path.open("r") as _file:
workflow = _file.read()
workflow = re.sub(r'phonenumbers==[\d.]+', f'phonenumbers=={version}', workflow)
with workflow_path.open('w') as _file:
workflow = re.sub(r"phonenumbers==[\d.]+", f"phonenumbers=={version}", workflow)
with workflow_path.open("w") as _file:
_file.write(workflow)
return
logging.critical('Not found workflow file')
logging.critical("Not found workflow file")
exit(-1)


def run(args: argparse.Namespace) -> None:
version = parsing_version(get_latest())
if not args.update and not is_need_update(version=version):
logging.info('No update required')
logging.info("No update required")
exit(0)
is_phonenumbers = check_phonenumbers_libs(version=version)
if not is_phonenumbers and not args.ignore_phonenumbers:
logging.critical(f'No actual version phonenumbers. {version}')
logging.critical(f"No actual version phonenumbers. {version}")
exit(-1)
if is_phonenumbers:
update_pipfile(version=version)
update_workflow(version=version)

logging.info('Update requirements')
cmd(['pipenv', 'update', '-d'], cwd=ROOT_DIR)
logging.info("Update requirements")
cmd(["pipenv", "update", "-d"], cwd=ROOT_DIR)

logging.info('Update patterns')
main(patterns_tag=f'v{version}')
logging.info("Update patterns")
main(patterns_tag=f"v{version}")

if not args.no_black:
logging.info('Run black')
cmd(['black', 'phone_gen/'], cwd=ROOT_DIR)
logging.info("Run black")
cmd(["black", "--line-length=120", "phone_gen/"], cwd=ROOT_DIR)
if not args.no_flake:
logging.info('Run flake8')
cmd(['flake8', 'phone_gen/'], cwd=ROOT_DIR)
logging.info("Run flake8")
cmd(["flake8", "phone_gen/"], cwd=ROOT_DIR)
if not args.no_tests:
logging.info('Run tests')
cmd(['pytest', 'tests/'], cwd=ROOT_DIR)
logging.info('Done')
logging.info("Run tests")
cmd(["pytest", "tests/"], cwd=ROOT_DIR)
logging.info("Done")


if __name__ == '__main__':
if __name__ == "__main__":
run(args=arg_parser())
11 changes: 4 additions & 7 deletions phone_gen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,10 @@
Resources:
* libphonenumber https://github.com/google/libphonenumber
* Modified strgen https://github.com/paul-wolf/strgen
"""

from ._generator import (
clean_alt_patters,
load_alt_patters,
PhoneNumber,
PhoneNumberNotFound,
)
from ._generator import RegEx, PatternError
from ._phone_number import clean_alt_patters, load_alt_patters, PhoneNumber, PhoneNumberNotFound

try:
from .__version__ import version as __version__
Expand All @@ -45,4 +40,6 @@
"load_alt_patters",
"PhoneNumber",
"PhoneNumberNotFound",
"RegEx",
"PatternError",
]
Loading

0 comments on commit e233b39

Please sign in to comment.