Commit 2b14c9c4 authored by Лошкарев Сергей Алексеевич's avatar Лошкарев Сергей Алексеевич
Browse files

replace file and add docstrings, annotations

1 merge request!5Edit repo
Showing with 99 additions and 54 deletions
+99 -54
venv/
/*.ini
/__pycache__/
__pycache__/
/.vscode/
\ No newline at end of file
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')
'''
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
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
'''
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
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),
......
src/person.py 0 → 100644
'''
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
File moved
File moved
File moved
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment