From 2b14c9c4a621cf1dad86f632fb36e1dde41146c6 Mon Sep 17 00:00:00 2001 From: Zergey Loshkarev <saloshkarev@miem.hse.ru> Date: Sat, 3 Jul 2021 02:13:49 +0300 Subject: [PATCH] replace file and add docstrings, annotations --- .gitignore | 2 +- main.py | 12 ++-- src/__init__.py | 8 +++ config.py => src/config.py | 17 +++--- get_from_tagia.py => src/get_from_tagia.py | 30 +++------- page_writer.py => src/page_writer.py | 58 ++++++++++++++----- src/person.py | 26 +++++++++ list_page.tmpl => templates/list_page.tmpl | 0 .../person_page.tmpl | 0 query.tmpl => templates/query.tmpl | 0 10 files changed, 99 insertions(+), 54 deletions(-) create mode 100644 src/__init__.py rename config.py => src/config.py (82%) rename get_from_tagia.py => src/get_from_tagia.py (62%) rename page_writer.py => src/page_writer.py (59%) create mode 100644 src/person.py rename list_page.tmpl => templates/list_page.tmpl (100%) rename person_page.tmpl => templates/person_page.tmpl (100%) rename query.tmpl => templates/query.tmpl (100%) diff --git a/.gitignore b/.gitignore index 07f073f..4c9f33a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ venv/ /*.ini -/__pycache__/ +__pycache__/ /.vscode/ \ No newline at end of file diff --git a/main.py b/main.py index b3f50bb..3035f1a 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,8 @@ -from config import read_config -from get_from_tagia import TaigaReader -from page_writer import PageWriter +''' +file for running this app +''' + +from src import read_config, TaigaReader, PageWriter taiga, wiki = read_config('config.ini') reader = TaigaReader(taiga) @@ -8,7 +10,7 @@ writer = PageWriter(wiki) persons_by_roles = dict() for person in reader.get_persons(): - if not (person.role_name in persons_by_roles): + if person.role_name not in persons_by_roles: persons_by_roles[person.role_name] = [] persons_by_roles[person.role_name].append(person) @@ -18,4 +20,4 @@ for person in reader.get_persons(): writer.write_list_page(persons_by_roles) -print('Pages created') \ No newline at end of file +print('Pages created') diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..5dcd009 --- /dev/null +++ b/src/__init__.py @@ -0,0 +1,8 @@ +''' +init file of package src +''' + +from .config import read_config +from .person import Person +from .page_writer import PageWriter +from .get_from_tagia import TaigaReader diff --git a/config.py b/src/config.py similarity index 82% rename from config.py rename to src/config.py index 9c9f244..fb22abd 100644 --- a/config.py +++ b/src/config.py @@ -1,8 +1,6 @@ from configparser import ConfigParser -import taiga - -_CONFIG_FILE = 'config.ini' # config file +_CONFIG_FILE = 'config.ini' # config file def _valid_taiga_config(config: ConfigParser) -> (bool): @@ -12,14 +10,15 @@ def _valid_taiga_config(config: ConfigParser) -> (bool): if 'taiga' in config.sections(): taiga_dict = dict(config['taiga']) if ( - 'host' in taiga_dict and \ - 'username' in taiga_dict and \ - 'password' in taiga_dict and \ + 'host' in taiga_dict and + 'username' in taiga_dict and + 'password' in taiga_dict and 'project_slug' in taiga_dict ): return True return False + def _valid_wiki_config(config: ConfigParser) -> (bool): ''' Function for checking including wiki configuration in config file @@ -27,13 +26,14 @@ def _valid_wiki_config(config: ConfigParser) -> (bool): if 'wiki' in config.sections(): wiki_dict = dict(config['wiki']) if ( - 'url' in wiki_dict and \ - 'token' in wiki_dict and \ + 'url' in wiki_dict and + 'token' in wiki_dict and 'path' in wiki_dict ): return True return False + def read_config(config_filename: str) -> (ConfigParser): ''' Function for checking existence needed parametres in config file @@ -46,4 +46,3 @@ def read_config(config_filename: str) -> (ConfigParser): if not _valid_wiki_config(config): raise Exception('Unvalid data-format in Wiki\'s config') return config['taiga'], config['wiki'] - \ No newline at end of file diff --git a/get_from_tagia.py b/src/get_from_tagia.py similarity index 62% rename from get_from_tagia.py rename to src/get_from_tagia.py index 178b75a..d6a6d57 100644 --- a/get_from_tagia.py +++ b/src/get_from_tagia.py @@ -1,34 +1,17 @@ ''' Module for getting infromation from taiga ''' - +from .person import Person +from taiga.models.models import Membership from taiga import TaigaAPI -from taiga.models.models import User - -class Person(): - ''' - Person class for describing informatron about users to page writer - ''' - - def __init__(self, member, user, role_name) -> (None): - ''' - Constructor to Person object - ''' - self.full_name = user.full_name - self.username = user.username - self.photo = user.big_photo - self.bio = user.bio - self.role_name = role_name - - def set_link(self, link): - self.link = link +from configparser import SectionProxy class TaigaReader(): ''' object to read information from taiga ''' - def __init__(self, taiga_config): + def __init__(self, taiga_config: SectionProxy) -> (None): ''' Constructor to TaigaReader ''' @@ -36,10 +19,11 @@ class TaigaReader(): self._api.auth(taiga_config['username'], taiga_config['password']) self._project_slug = taiga_config['project_slug'] - def create_person(self, member): + def create_person(self, member: Membership) -> (Person): ''' Create Person with member and request to getting information from user ''' + print(type(member)) user = self._api.users.get(member.user) roles = self._api.projects.get_by_slug(self._project_slug).list_roles() role_name = str(roles.get(id=member.role).name) @@ -51,4 +35,4 @@ class TaigaReader(): and getting information about users from project by project_slug ''' members = self._api.projects.get_by_slug(self._project_slug).list_memberships() - return map(self.create_person, members) + return map(self.create_person, members) \ No newline at end of file diff --git a/page_writer.py b/src/page_writer.py similarity index 59% rename from page_writer.py rename to src/page_writer.py index ab61589..12fbc8a 100644 --- a/page_writer.py +++ b/src/page_writer.py @@ -1,25 +1,41 @@ -from jinja2 import Template -from jinja2 import Environment -from jinja2 import FileSystemLoader -from gql import gql, Client +''' +Module for PageWriter, which used for creating pages in Wiki +''' +from typing import Dict, Tuple +from .person import Person +from jinja2 import Environment, FileSystemLoader +from gql import Client, gql from gql.transport.requests import RequestsHTTPTransport +from configparser import SectionProxy -def _normalise_str(s): + +def _normalise_str(s: str) -> (str): + ''' + Method for normalise string to send it in GraphQl + ''' return s.replace('\\', '\\\\').replace('\n', '\\n').replace('\r', '') + class PageWriter(): + ''' + Class for connecting to wiki and creating new pages + ''' + def __init__( - self, wiki_config, - person_template='person_page.tmpl', - list_template='list_page.tmpl', - query_template='query.tmpl' - ): - env = Environment(loader=FileSystemLoader('./')) + self, wiki_config: SectionProxy, + person_template: str = 'person_page.tmpl', + list_template: str = 'list_page.tmpl', + query_template: str = 'query.tmpl' + ) -> (None): + ''' + Constructor for PageWrite class + ''' + env = Environment(loader=FileSystemLoader('./templates/')) self._user_template = env.get_template(person_template) self._list_template = env.get_template(list_template) self._query_template = env.get_template(query_template) self._path = wiki_config['path'] - sample_transport=RequestsHTTPTransport( + sample_transport = RequestsHTTPTransport( url=wiki_config['url'], use_json=True, headers={ @@ -31,8 +47,11 @@ class PageWriter(): transport=sample_transport, fetch_schema_from_transport=True, ) - - def write_person_page(self, person): + + def write_person_page(self, person: Person) -> (Tuple[bool, str]): + ''' + Method to write person's page + ''' content = self._user_template.render(person=person) query_text = self._query_template.render( content=_normalise_str(content), @@ -42,14 +61,21 @@ class PageWriter(): resp = self._client.execute( gql(query_text) )['pages']['create']['responseResult'] - + link = '' if resp['succeeded']: link = './' + person.username person.set_link(link) return resp['succeeded'], resp['message'] - def write_list_page(self, persons_by_role, title='Список участников'): + def write_list_page( + self, + persons_by_role: Dict, + title: str = 'Список участников' + ) -> (Tuple[bool, str]): + ''' + Method to write list of members page + ''' content = self._list_template.render(persons_by_role=persons_by_role) query_text = self._query_template.render( content=_normalise_str(content), diff --git a/src/person.py b/src/person.py new file mode 100644 index 0000000..9ff0d25 --- /dev/null +++ b/src/person.py @@ -0,0 +1,26 @@ +''' +Module with description of Person class +''' +from taiga.models.models import Membership, User + + +class Person(): + ''' + Person class for describing informatron about users to page writer + ''' + + def __init__(self, member: Membership, user: User, role_name: str) -> (None): + ''' + Constructor to Person object + ''' + self.full_name = user.full_name + self.username = user.username + self.photo = user.big_photo + self.bio = user.bio + self.role_name = role_name + + def set_link(self, link: str) -> (None): + ''' + Method for set link to instances of Person + ''' + self.link = link diff --git a/list_page.tmpl b/templates/list_page.tmpl similarity index 100% rename from list_page.tmpl rename to templates/list_page.tmpl diff --git a/person_page.tmpl b/templates/person_page.tmpl similarity index 100% rename from person_page.tmpl rename to templates/person_page.tmpl diff --git a/query.tmpl b/templates/query.tmpl similarity index 100% rename from query.tmpl rename to templates/query.tmpl -- GitLab