提交 3f6080bf 编写于 作者: S solarhu 提交者: Gitee

!32 修改注释

Merge pull request !32 from 魔箭胖胖/master
'''
Integration of multiple sqlite file data, including
reading sqlite database and inserting data
'''
#!/usr/bin/python3
"""
Description: Integration of multiple sqlite file data, including reading sqlite database and inserting data
Class: MergeData
"""
from sqlalchemy.exc import SQLAlchemyError
from packageship.application.models.temporarydb import src_package
from packageship.application.models.temporarydb import src_requires
......@@ -16,30 +17,50 @@ LOGGER = Log(__name__)
class MergeData():
'''
Load data from sqlite database
'''
"""
Description: Load data from sqlite database
Attributes:
db_file: Database file
db_type: Connected database type
datum_database: Base database name
"""
def __init__(self, db_file):
"""
Description: Class instance initialization
Args:
db_file: Database file
"""
self.db_file = db_file
self.db_type = 'sqlite:///'
self.datum_database = 'maintenance.information'
@staticmethod
def __columns(cursor):
'''
functional description:Returns all the column names queried by the current cursor
'''
"""
Description: functional description:Returns all the column names queried by the current cursor
Args:
cursor: Cursor
Returns:
The first columns
Raises:
"""
return [col[0] for col in cursor.description]
def get_package_data(self):
'''
get binary package or source package data
'''
"""
Description: get binary package or source package data
Args:
Returns:
All source package data queried
Raises:
SQLAlchemyError: An error occurred while executing the sql statement
"""
try:
with DBHelper(db_name=self.db_file, db_type=self.db_type, import_database=True)\
with DBHelper(db_name=self.db_file, db_type=self.db_type, import_database=True) \
as database:
src_packages_data = database.session.execute(
"select pkgKey,name,version,rpm_license,url,rpm_sourcerpm from packages")
......@@ -51,11 +72,17 @@ class MergeData():
return None
def get_requires_data(self):
'''
get dependent package data of binary package or source package
'''
"""
Description: get dependent package data of binary package or source package
Args:
Returns:
All dependent data queried
Raises:
SQLAlchemyError: An error occurred while executing the sql statement
"""
try:
with DBHelper(db_name=self.db_file, db_type=self.db_type, import_database=True)\
with DBHelper(db_name=self.db_file, db_type=self.db_type, import_database=True) \
as database:
requires = database.session.execute(
"select pkgKey,name from requires")
......@@ -66,11 +93,17 @@ class MergeData():
return None
def get_provides(self):
'''
get the dependency package provided by the binary package
'''
"""
Description: get the dependency package provided by the binary package
Args:
Returns:
Query the component data provided by all binary packages
Raises:
SQLAlchemyError: An error occurred while executing the sql statement
"""
try:
with DBHelper(db_name=self.db_file, db_type=self.db_type, import_database=True)\
with DBHelper(db_name=self.db_file, db_type=self.db_type, import_database=True) \
as database:
requires = database.session.execute(
"select pkgKey,name from provides")
......@@ -81,9 +114,15 @@ class MergeData():
return None
def get_maintenance_info(self):
'''
Obtain the information of the maintainer
'''
"""
Description: Obtain the information of the maintainer
Args:
Returns:
Maintainer related information
Raises:
SQLAlchemyError: An error occurred while executing the sql statement
"""
try:
if not hasattr(self, 'mainter_infos'):
self.mainter_infos = dict()
......@@ -99,9 +138,17 @@ class MergeData():
LOGGER.logger.error(sql_error)
def src_file_merge(self, src_package_key, db_file):
'''
Source code related data integration
'''
"""
Description: Source code related data integration
Args:
src_package_key: The relevant key value of the source package
db_file: Database file
Returns:
Key value after successful data combination
(0, False) or (src_package_key, True)
Raises:
SQLAlchemyError: An error occurred while executing the sql statement
"""
self.get_maintenance_info()
self.__compose_src_package()
......@@ -131,9 +178,15 @@ class MergeData():
return (src_package_key, True)
def __compose_src_package(self):
'''
Combine source package data
'''
"""
Description: Combine source package data
Args:
Returns:
Raises:
"""
if getattr(self, 'src_package_datas', None) is None:
self.src_package_datas = []
......@@ -160,9 +213,15 @@ class MergeData():
)
def __compose_src_rquires(self):
'''
Combine source package dependent package data
'''
"""
Description: Combine source package dependent package data
Args:
Returns:
Raises:
"""
if getattr(self, 'src_requires_dicts', None) is None:
self.src_requires_dicts = dict()
......@@ -179,9 +238,15 @@ class MergeData():
)
def __compose_bin_package(self):
'''
Combine binary package data
'''
"""
Description: Combine binary package data
Args:
Returns:
Raises:
AttributeError
"""
if getattr(self, 'bin_package_datas', None) is None:
self.bin_package_datas = []
......@@ -205,9 +270,14 @@ class MergeData():
)
def __compose_bin_requires(self):
'''
Combining binary dependent package data
'''
"""
Description: Combining binary dependent package data
Args:
Returns:
Raises:
"""
if getattr(self, 'bin_requires_dicts', None) is None:
self.bin_requires_dicts = dict()
......@@ -222,9 +292,15 @@ class MergeData():
})
def __compose_bin_provides(self):
'''
Combine binary package data
'''
"""
Description: Combine binary package data
Args:
Returns:
Raises:
"""
if getattr(self, 'bin_provides_dicts', None) is None:
self.bin_provides_dicts = dict()
......@@ -239,10 +315,17 @@ class MergeData():
})
def bin_file_merge(self, bin_package_key, db_file):
'''
Binary package related data integration
'''
"""
Description: Binary package related data integration
Args:
bin_package_key: Primary key of binary package
db_file: Database file
Returns:
Key value after successful data combination
(0, False) or (bin_package_key, True)
Raises:
SQLAlchemyError: An error occurred while executing the sql statement
"""
self.__compose_bin_package()
# binary package dependent package integration
......
'''
Simple encapsulation of sqlalchemy orm framework operation database
'''
#!/usr/bin/python3
"""
Description: Simple encapsulation of sqlalchemy orm framework operation database
Class: DBHelper
"""
import os
from sqlalchemy import create_engine
from sqlalchemy import MetaData
......@@ -18,22 +19,34 @@ from packageship import system_config
class DBHelper():
'''
Database connection, operation public class
'''
"""
Description: Database connection, operation public class
Attributes:
user_name: Username
password: Password
ip_address: Ip address
port: Port
db_name: Database name
db_type: Database type
session: Session
"""
# The base class inherited by the data model
BASE = declarative_base()
def __init__(self, user_name=None, passwrod=None, ip_address=None, # pylint: disable=R0913
port=None, db_name=None, db_type=None, **kwargs):
"""
Description: Class instance initialization
"""
self.user_name = user_name
self._readconfig = ReadConfig()
if self.user_name is None:
self.user_name = self._readconfig.get_database('user_name')
self.passwrod = passwrod
if self.passwrod is None:
self.passwrod = self._readconfig.get_database('password')
self.password = passwrod
if self.password is None:
self.password = self._readconfig.get_database('password')
self.ip_address = ip_address
......@@ -67,9 +80,15 @@ class DBHelper():
self.session = None
def _create_engine(self):
'''
Create a database connection object
'''
"""
Description: Create a database connection object
Args:
Returns:
Raises:
DisconnectionError: A disconnect is detected on a raw DB-API connection.
"""
if self.db_type.startswith('sqlite'):
if not self.db_name:
raise DbnameNoneException(
......@@ -78,11 +97,11 @@ class DBHelper():
self.db_type + self.db_name, encoding='utf-8', convert_unicode=True,
connect_args={'check_same_thread': False})
else:
if all([self.user_name, self.passwrod, self.ip_address, self.port, self.db_name]):
if all([self.user_name, self.password, self.ip_address, self.port, self.db_name]):
# create connection object
self.engine = create_engine(URL(**{'database': self.db_name,
'username': self.user_name,
'password': self.passwrod,
'password': self.password,
'host': self.ip_address,
'port': self.port,
'drivername': self.db_type}),
......@@ -93,9 +112,14 @@ class DBHelper():
'A disconnect is detected on a raw DB-API connection')
def _db_file_path(self):
'''
load the path stored in the sqlite database
'''
"""
Description: load the path stored in the sqlite database
Args:
Returns:
Raises:
"""
self.database_file_path = self._readconfig.get_system(
'data_base_path')
if not self.database_file_path:
......@@ -104,9 +128,15 @@ class DBHelper():
os.makedirs(self.database_file_path)
def __enter__(self):
'''
functional description:Create a context manager for the database connection
'''
"""
Description: functional description:Create a context manager for the database connection
Args:
Returns:
Class instance
Raises:
"""
session = sessionmaker()
if getattr(self, 'engine') is None:
......@@ -117,28 +147,42 @@ class DBHelper():
return self
def __exit__(self, exc_type, exc_val, exc_tb):
'''
functional description:Release the database connection pool and close the connection
'''
"""
Description: functional description:Release the database connection pool and close the connection
Args:
Returns:
exc_type: Abnormal type
exc_val: Abnormal value
exc_tb: Abnormal table
Raises:
"""
self.session.close()
@classmethod
def create_all(cls, db_name=None):
'''
functional description:Create all database tables
parameter:
return value:
exception description:
modify record:
'''
"""
Description: functional description:Create all database tables
Args:
db_name: Database name
Returns:
Raises:
"""
cls.BASE.metadata.create_all(bind=cls(db_name=db_name).engine)
def create_table(self, tables):
'''
Create a single table
'''
"""
Description: Create a single table
Args:
tables: Table
Returns:
Raises:
"""
meta = MetaData(self.engine)
for table_name in DBHelper.BASE.metadata.tables.keys():
if table_name in tables:
......@@ -147,12 +191,16 @@ class DBHelper():
table.create()
def add(self, entity):
'''
functional description:Insert a single data entity
parameter:
return value:
"""
Description: Insert a single data entity
Args:
entity: Data entity
Return:
If the addition is successful, return the corresponding entity, otherwise return None
'''
Raises:
ContentNoneException: An exception occurred while content is none
SQLAlchemyError: An exception occurred while creating the database
"""
if entity is None:
raise ContentNoneException(
......@@ -168,12 +216,17 @@ class DBHelper():
return entity
def batch_add(self, dicts, model):
'''
functional description:tables for adding databases in bulk
parameter:
:param dicts:Entity dictionary data to be added
:param model:Solid model class
'''
"""
Description:tables for adding databases in bulk
Args:
dicts:Entity dictionary data to be added
model:Solid model class
Returns:
Raises:
TypeError: An exception occurred while incoming type does not meet expectations
SQLAlchemyError: An exception occurred while creating the database
"""
if model is None:
raise ContentNoneException('solid model must be specified')
......
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册