Skip to content

Commit

Permalink
added command check_file_format to pydabu.py
Browse files Browse the repository at this point in the history
(just a small start to be enhanced)
  • Loading branch information
daniel-mohr committed Jan 19, 2021
1 parent e37d8e3 commit e177f06
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 12 deletions.
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def run(self):
packages=[
'dabu',
'dabu.analyse_data_structure',
'dabu.analyse_file_format',
'dabu.scripts'],
scripts=[
'src/scripts/pydabu.py'],
Expand All @@ -116,8 +117,9 @@ def run(self):
requires=[
'argparse',
'distutils',
'json',
'os',
'os.path',
'json'],
're'],
provides=['dabu']
)
38 changes: 32 additions & 6 deletions src/dabu/analyse_data_structure/analyse_data_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""

import os
import re


def check_file_available(files, key):
Expand All @@ -19,10 +20,11 @@ def check_file_available(files, key):
for f in files:
if f.lower().startswith(key):
res = f
break
return res


def analyse_data_structure(path):
def analyse_data_structure(path, result=dict()):
"""
:Author: Daniel Mohr
:Email: [email protected]
Expand All @@ -31,12 +33,36 @@ def analyse_data_structure(path):
Analyse the data structure of the given path.
:param path: directory path to analyse
:param result: you can give a dict, where the results are appended
or overridden
"""
result = dict()
files = os.listdir(path)
analysed_files = []
# find README, LICENSE, MANIFEST
for key in ['README', 'LICENSE', 'MANIFEST']:
result[key] = check_file_available(files, key)
result['author'] = [{'name': 'foo', 'email': 'bar'},
{'name': 'a', 'email': 'b'}]
for key in ['readme', 'license', 'manifest']:
res = check_file_available(files, key)
result[key] = res
if res is not None:
analysed_files.append(res)
# analyse if directory is a repository
result['repository'] = None
for f in files:
if (f in ['.git', '.bzr']) and os.path.isdir(f):
# assume repository
result['repository'] = f
analysed_files.append(f)
break
# analyse if checksums are available (look for checksums)
result['checksums'] = None
regexp = re.compile(
'.*checksum.*|.*\.md5|.*\.sha256|.*\.sha512|.*\.sha1',
flags=re.IGNORECASE)
for f in files:
if regexp.findall(f):
result['CHECKSUMS'] = f
analysed_files.append(f)
break
result['data'] = list(set(files).difference(analysed_files))
# result['author'] = [{'name': 'foo', 'email': 'bar'},
# {'name': 'a', 'email': 'b'}]
return result
37 changes: 37 additions & 0 deletions src/dabu/analyse_file_format/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""
:mod:`dabu.analyse_file_format`
===============================
.. moduleauthor:: Daniel Mohr
.. contents::
functions
---------
.. currentmodule:: dabu.analyse_file_format
.. autofunction:: analyse_file_format
.. autofunction:: analyse_file_format_dict
copyright + license
===================
Author: Daniel Mohr
Date: 2021-01-19 (last change).
License: GNU GENERAL PUBLIC LICENSE, Version 3, 29 June 2007.
Copyright (C) 2021 Daniel Mohr
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
http://www.gnu.org/licenses/
"""
from .analyse_file_format import analyse_file_format
from .analyse_file_format_dict import analyse_file_format_dict
22 changes: 22 additions & 0 deletions src/dabu/analyse_file_format/analyse_file_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
:Author: Daniel Mohr
:Email: [email protected]
:Date: 2021-01-19 (last change).
:License: GNU GENERAL PUBLIC LICENSE, Version 3, 29 June 2007.
"""

import os.path


def analyse_file_format(path):
"""
:Author: Daniel Mohr
:Email: [email protected]
:Date: 2021-01-19 (last change).
Analyse the file format of the files stored in result.
:param path: directory path to analyse
"""
_, file_extension = os.path.splitext(path)
return file_extension
29 changes: 29 additions & 0 deletions src/dabu/analyse_file_format/analyse_file_format_dict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
:Author: Daniel Mohr
:Email: [email protected]
:Date: 2021-01-19 (last change).
:License: GNU GENERAL PUBLIC LICENSE, Version 3, 29 June 2007.
"""

import os
import re

from .analyse_file_format import analyse_file_format


def analyse_file_format_dict(path, result):
"""
:Author: Daniel Mohr
:Email: [email protected]
:Date: 2021-01-19 (last change).
Analyse the file format of the files stored in result.
:param path: directory path to analyse
:param result: a dict; only the key 'data' will be read
"""
files = result['data'].copy()
result['data'] = []
for f in files:
result['data'].append({'name': f, 'format': analyse_file_format(f)})
return result
19 changes: 14 additions & 5 deletions src/dabu/scripts/pydabu_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import os.path

from .run_check_data_structure import run_check_data_structure
from .run_check_file_format import run_check_file_format


def check_arg_directory(data):
Expand All @@ -35,8 +36,8 @@ def pydabu_main():
epilog=epilog,
formatter_class=argparse.RawDescriptionHelpFormatter)
# parent parser to describe common argument
parent_parser = argparse.ArgumentParser(add_help=False)
parent_parser.add_argument(
common_parser1 = argparse.ArgumentParser(add_help=False)
common_parser1.add_argument(
'-directory',
nargs="+",
type=check_arg_directory,
Expand All @@ -47,7 +48,7 @@ def pydabu_main():
'You can also give a list of directories separated by space. ' +
'default: .',
metavar='d')
parent_parser.add_argument(
common_parser1.add_argument(
'-output_format',
nargs="+",
type=str,
Expand All @@ -69,9 +70,17 @@ def pydabu_main():
help='For more help: pydabu.py check_data_structure -h',
description='',
epilog='',
parents=[parent_parser])
parents=[common_parser1])
parser_check_data_structure.set_defaults(func=run_check_data_structure)
# -directory
# subparser check_file_format
parser_check_file_format = subparsers.add_parser(
'check_file_format',
formatter_class=argparse.RawDescriptionHelpFormatter,
help='For more help: pydabu.py check_file_format -h',
description='',
epilog='',
parents=[common_parser1])
parser_check_file_format.set_defaults(func=run_check_file_format)
# parse arguments
args = parser.parse_args()
if args.subparser_name is not None:
Expand Down
30 changes: 30 additions & 0 deletions src/dabu/scripts/run_check_file_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
:Author: Daniel Mohr
:Email: [email protected]
:Date: 2021-01-19 (last change).
:License: GNU GENERAL PUBLIC LICENSE, Version 3, 29 June 2007.
"""

import json

from dabu.analyse_data_structure import analyse_data_structure
from dabu.analyse_file_format import analyse_file_format_dict


def run_check_file_format(args):
"""
:Author: Daniel Mohr
:Email: [email protected]
:Date: 2021-01-19 (last change).
:param args: namespace return from ArgumentParser.parse_args
"""
# print(args);exit()
for path in args.directory: # for every given directory
result = analyse_data_structure(path)
result = analyse_file_format_dict(path, result)
# print(result)
if 'json' in args.output_format:
print(json.dumps(result))
if 'human_readable' in args.output_format:
print(json.dumps(result, indent=1))

0 comments on commit e177f06

Please sign in to comment.