generated from NicolaiRuckel/python-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sebastian Simon
committed
Oct 10, 2024
1 parent
9ed7852
commit da4111f
Showing
7 changed files
with
243 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
# This file is part of the CfgNet module. | ||
# | ||
# This program is free software: you can redistribute it and/or modify it under | ||
# the terms of the GNU General Public License as published by the Free Software | ||
# Foundation, either version 3 of the License, or (at your option) any later | ||
# version. | ||
# | ||
# This program is distributed in the hope that it will be useful, but WITHOUT | ||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
# details. | ||
# | ||
# You should have received a copy of the GNU General Public License along with | ||
# this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
import logging | ||
import os | ||
|
||
from typing import Optional | ||
from lxml import etree as ET | ||
from lxml.etree import _Element, _Comment | ||
|
||
from cfgnet.config_types.config_types import ConfigType | ||
from cfgnet.network.nodes import ( | ||
ArtifactNode, | ||
Node, | ||
OptionNode, | ||
ProjectNode, | ||
ValueNode, | ||
) | ||
from cfgnet.plugins.plugin import Plugin | ||
|
||
|
||
class AndroidPlugin(Plugin): | ||
def __init__(self): | ||
super().__init__("android") | ||
self.lines = None | ||
|
||
def is_responsible(self, abs_file_path: str) -> bool: | ||
file_name = os.path.basename(abs_file_path) | ||
return file_name == "AndroidManifest.xml" | ||
|
||
def _parse_config_file( | ||
self, | ||
abs_file_path: str, | ||
rel_file_path: str, | ||
root: Optional[ProjectNode], | ||
) -> ArtifactNode: | ||
artifact = ArtifactNode( | ||
file_path=abs_file_path, | ||
rel_file_path=rel_file_path, | ||
concept_name=self.concept_name, | ||
project_root=root, | ||
) | ||
|
||
with open(abs_file_path, "r", encoding="utf-8") as src: | ||
self.lines = src.readlines() | ||
|
||
try: | ||
tree = ET.parse(abs_file_path) | ||
root = tree.getroot() | ||
|
||
self.parse_tree(root, parent_node=artifact) | ||
|
||
except ET.Error as error: | ||
logging.warning( | ||
'Failed to parse xml file "%s" due to %s', rel_file_path, error | ||
) | ||
|
||
return artifact | ||
|
||
def parse_tree(self, element: _Element, parent_node: Node): | ||
option_name = element.tag | ||
config_type = self.get_config_type(option_name=option_name) | ||
parent_option = OptionNode( | ||
option_name, str(element.sourceline), config_type | ||
) | ||
parent_node.add_child(parent_option) | ||
|
||
for attr_name, attr_value in element.attrib.items(): | ||
key = attr_name.split("}")[-1] | ||
config_type = self.get_config_type(key) | ||
line_number = self.find_attribute_line(key, attr_value, self.lines) | ||
|
||
if not line_number: | ||
line_number = str(element.sourceline) | ||
|
||
option = OptionNode(key, line_number, config_type) | ||
parent_option.add_child(option) | ||
value_node = ValueNode(attr_value) | ||
option.add_child(value_node) | ||
|
||
for child in element: | ||
if not isinstance(child, _Comment): | ||
self.parse_tree(child, parent_option) | ||
|
||
def find_attribute_line( | ||
self, attr_name, attr_value, lines | ||
) -> Optional[str]: | ||
for i in range(len(lines) - 1): | ||
if attr_name in lines[i] and attr_value in lines[i]: | ||
return str(i + 1) | ||
return None | ||
|
||
# pylint: disable=too-many-return-statements | ||
def get_config_type(self, option_name: str) -> ConfigType: | ||
""" | ||
Get config type based on the option name. | ||
:param name: option name | ||
:return: ConfigType | ||
""" | ||
option_name = option_name.lower() | ||
|
||
if option_name.endswith("version"): | ||
return ConfigType.VERSION_NUMBER | ||
|
||
if option_name.endswith("mode"): | ||
return ConfigType.MODE | ||
|
||
if option_name.endswith("size"): | ||
return ConfigType.SIZE | ||
|
||
if option_name.endswith("port"): | ||
return ConfigType.PORT | ||
|
||
if option_name.endswith("host"): | ||
return ConfigType.HOST | ||
|
||
if option_name.endswith("path"): | ||
return ConfigType.PATH | ||
|
||
if option_name.endswith("permission"): | ||
return ConfigType.PERMISSION | ||
|
||
if option_name in ( | ||
"maxrecents", | ||
"maxaspectratio", | ||
"priority", | ||
"height", | ||
"width", | ||
): | ||
return ConfigType.NUMBER | ||
|
||
if option_name in ( | ||
"name", | ||
"label", | ||
"description", | ||
"process", | ||
"package", | ||
): | ||
return ConfigType.NAME | ||
|
||
return ConfigType.UNKNOWN |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# This file is part of the CfgNet module. | ||
# | ||
# This program is free software: you can redistribute it and/or modify it under | ||
# the terms of the GNU General Public License as published by the Free Software | ||
# Foundation, either version 3 of the License, or (at your option) any later | ||
# version. | ||
# | ||
# This program is distributed in the hope that it will be useful, but WITHOUT | ||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
# details. | ||
# | ||
# You should have received a copy of the GNU General Public License along with | ||
# this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
import os | ||
|
||
import pytest | ||
|
||
from cfgnet.plugins.concept.android_plugin import AndroidPlugin | ||
from cfgnet.config_types.config_types import ConfigType | ||
from tests.utility.id_creator import make_id | ||
|
||
|
||
@pytest.fixture(name="get_plugin") | ||
def get_plugin_(): | ||
plugin = AndroidPlugin() | ||
return plugin | ||
|
||
|
||
def test_is_responsible(get_plugin): | ||
plugin = get_plugin | ||
|
||
assert plugin.is_responsible("tests/files/AndroidManifest.xml") | ||
assert not plugin.is_responsible("tests/files/pom.xml") | ||
|
||
|
||
def test_config_types(get_plugin): | ||
maven_plugin = get_plugin | ||
maven_file = os.path.abspath("tests/files/AndroidManifest.xml") | ||
artifact = maven_plugin.parse_file(maven_file, "AndroidManifest.xml") | ||
nodes = artifact.get_nodes() | ||
|
||
name_node = next(filter(lambda x: x.id == make_id("AndroidManifest.xml", "manifest", "package", "com.example.myfirstapp"), nodes)) | ||
bool_node = next(filter(lambda x: x.id == make_id("AndroidManifest.xml", "manifest", "application", "allowBackup", "true"), nodes)) | ||
version_node = next(filter(lambda x: x.id == make_id("AndroidManifest.xml", "manifest", "uses-sdk", "minSdkVersion", "21"), nodes)) | ||
|
||
assert version_node.config_type == ConfigType.VERSION_NUMBER | ||
assert bool_node.config_type == ConfigType.BOOLEAN | ||
assert name_node.config_type == ConfigType.NAME |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.example.myfirstapp"> | ||
|
||
<!-- Permissions --> | ||
<uses-permission android:name="android.permission.INTERNET" /> | ||
|
||
<!-- Specify the minimum and target SDK version --> | ||
<uses-sdk | ||
android:minSdkVersion="21" | ||
android:targetSdkVersion="33" /> | ||
|
||
<!-- Declare the application --> | ||
<application | ||
android:allowBackup="true" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/AppTheme"> | ||
|
||
<!-- Define an activity that serves as the entry point --> | ||
<activity android:name=".MainActivity"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
|
||
<activity | ||
android:name=".DisplayMessageActivity" | ||
android:parentActivityName=".MainActivity" /> | ||
</application> | ||
</manifest> |