Skip to content

Commit

Permalink
- Major refactoring to the import logic to be more pythonic #13
Browse files Browse the repository at this point in the history
- fix for regression bug from renaming piaplib.keyring.clarify
- many regression fixes for #32 as part of refactoring #13
- rotated key for october
- added some work for upgrade path as part of #34

Changes in this commit:
	modified:   piaplib/__init__.py
	new file:   piaplib/__main__.py
	modified:   piaplib/book/__init__.py
	modified:   piaplib/book/__main__.py
	modified:   piaplib/book/logs.py
	modified:   piaplib/book/version.py
	modified:   piaplib/keyring/__init__.py
	modified:   piaplib/keyring/__main__.py
	modified:   piaplib/keyring/clarify.py
	modified:   piaplib/keyring/rand.py
	modified:   piaplib/lint/__init__.py
	modified:   piaplib/pku/__init__.py
	modified:   piaplib/pku/__main__.py
	modified:   piaplib/pku/upgrade.py
	modified:   piaplib/pku/utils.py
	modified:   piaplib/pocket.py
	modified:   tests/test_book.py
	modified:   tests/test_interface.py
	modified:   tests/test_keyring.py
	modified:   tests/test_pku.py
	modified:   tests/test_usage.py
  • Loading branch information
reactive-firewall committed Oct 7, 2017
1 parent b2cfa62 commit 8d27ca7
Show file tree
Hide file tree
Showing 21 changed files with 348 additions and 186 deletions.
13 changes: 11 additions & 2 deletions piaplib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,17 @@
try:
import sys
import os
if 'piaplib' in __file__:
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
try:
if str("piaplib") in __file__:
search_list = [
os.path.abspath(os.path.join(os.path.dirname(__file__), '..')),
os.path.abspath(os.path.join(os.path.dirname(__file__), '.'))
]
for __sys_path__ in search_list:
if __sys_path__ not in sys.path:
sys.path.insert(0, __sys_path__)
except Exception:
raise ImportError("PiAPlib failed to import.")
except Exception as ImportErr:
print(str(type(ImportErr)))
print(str(ImportErr))
Expand Down
31 changes: 31 additions & 0 deletions piaplib/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-

# Pocket PiAP
# ......................................................................
# Copyright (c) 2017, Kendrick Walls
# ......................................................................
# Licensed under MIT (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# ......................................................................
# http://www.github.com/reactive-firewall/PiAP-python-tools/LICENSE.rst
# ......................................................................
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ......................................................................

try:
from . import pocket as pocket
except Exception as importErr:
del importErr
import pocket as pocket

if __name__ in u'__main__':
import sys
pocket.main(sys.argv[1:])
exit(0)

18 changes: 11 additions & 7 deletions piaplib/book/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,24 @@
# limitations under the License.
# ......................................................................


try:
import sys
import os
if str("book") in __file__:
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
__sys_path__ = os.path.abspath(os.path.join(os.path.dirname(__file__), '.'))
if __sys_path__ not in sys.path:
sys.path.insert(0, __sys_path__)
except Exception:
raise ImportError("Pocket Book failed to import.")


try:
from . import book as book
if book.__name__ is False:
raise ImportError("Pocket Book Failed to import.")
except Exception:
import book as book
def main(argv=None):
"""The main event"""
import piaplib.book.__main__
return piaplib.book.__main__.main(argv)


if __name__ in u'__main__':
main(sys.argv[1:])

9 changes: 6 additions & 3 deletions piaplib/book/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@


try:
import os
import sys
import os
import argparse
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
if str("book") in __file__:
__sys_path__ = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
if str(__sys_path__) not in sys.path:
sys.path.insert(0, __sys_path__)
except Exception as importErr:
print(str(importErr))
print(str(importErr.args))
Expand All @@ -37,7 +40,7 @@
try:
try:
import piaplib as piaplib
except Exception:
except Exception as err:
from . import piaplib as piaplib
try:
from piaplib.pku import baseconfig as baseconfig
Expand Down
11 changes: 11 additions & 0 deletions piaplib/book/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@
exit(3)


try:
import sys
import os
if str("book") in __file__:
__sys_path__ = os.path.abspath(os.path.join(os.path.dirname(__file__), '.'))
if __sys_path__ not in sys.path:
sys.path.insert(0, __sys_path__)
except Exception:
raise ImportError("Pocket Book failed to import.")


class ANSIColors:
"""ANSI colored text"""
ENDC = """\033[0m"""
Expand Down
22 changes: 13 additions & 9 deletions piaplib/book/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
raise ImportError(err)
exit(3)


try:
import sys
if sys.__name__ is None:
Expand All @@ -54,13 +55,13 @@
try:
import piaplib as piaplib
except Exception:
from . import piaplib as piaplib
from .. import piaplib as piaplib
try:
from pku import utils as utils
from piaplib.pku import utils as utils
except Exception:
import piaplib.pku.utils as utils
try:
from pku import remediation as remediation
from piplib.pku import remediation as remediation
except Exception:
import piaplib.pku.remediation as remediation
try:
Expand All @@ -69,7 +70,7 @@
impErr = None
del(impErr)
try:
import logs.logs as logs
import piaplib.book.logs.logs as logs
except Exception:
raise ImportError("Error Importing logs for version")

Expand Down Expand Up @@ -97,17 +98,20 @@
def getKeyringVersion(verbose=False):
"""returns the keyring version."""
try:
from keyring import clarify as clarify
from piaplib import keyring
if keyring.__name__ is False:
raise NotImplementedError("Failed to import keyring")
except Exception:
import piaplib.keyring.clarify as clarify
import piaplib.keyring
try:
from keyring import keyring as keyring
from keyring import clarify as clarify
except Exception:
import piaplib.keyring.keyring as keyring
import piaplib.keyring.clarify as clarify
import piaplib.keyring.__main__
keyring_version = str(
"{name} {version}"
).format(
name=str(keyring.__prog__),
name=str(piaplib.keyring.__main__.__prog__),
version=str(piaplib.__version__)
)
if verbose:
Expand Down
26 changes: 17 additions & 9 deletions piaplib/keyring/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@
# limitations under the License.
# ......................................................................


try:
import sys
import os
if 'keyring' in __file__:
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
try:
if str("keyring") in __file__:
__sys_path__ = os.path.abspath(os.path.dirname(__file__))
if __sys_path__ not in sys.path:
sys.path.insert(0, __sys_path__)
except Exception:
raise ImportError("PiAPlib keyring failed to import.")
except Exception as ImportErr:
print(str(type(ImportErr)))
print(str(ImportErr))
Expand All @@ -31,12 +37,14 @@
del ImportErr
raise ImportError(u'Keyring Failed to Import')

try:
from . import keyring as keyring
except Exception as importErr:
del importErr
import keyring as keyring

if keyring.__name__ is None:
raise ImportError(u'Keyring Failed to Import')
def main(argv=None):
"""The main event"""
import piaplib.keyring.__main__
return piaplib.keyring.__main__.main(argv)


if __name__ in u'__main__':
main(sys.argv[1:])


29 changes: 20 additions & 9 deletions piaplib/keyring/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,38 @@
# ......................................................................

try:
import os
import sys
import os
import argparse
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
except Exception:
raise NotImplementedError("OMG! We could not import the os. We're like in the matrix!")
exit(3)
try:
if str("keyring") in __file__:
__sys_path__ = os.path.abspath(os.path.dirname(__file__))
if __sys_path__ not in sys.path:
sys.path.insert(0, __sys_path__)
except Exception:
raise ImportError("PiAPlib Keyring failed to import.")
except Exception as ImportErr:
print(str(type(ImportErr)))
print(str(ImportErr))
print(str((ImportErr.args)))
ImportErr = None
del ImportErr
raise ImportError(u'Keyring Failed to Import')

try:
from . import saltify as saltify
except Exception:
import saltify as saltify
import piaplib.keyring.saltify as saltify

try:
from . import rand as rand
except Exception:
import rand as rand
import piaplib.keyring.rand as rand

try:
from ..pku import remediation as remediation
from piaplib.pku import remediation as remediation
except Exception:
import pku.remediation as remediation
import piaplib.pku.remediation as remediation


__prog__ = """piaplib.keyring"""
Expand All @@ -51,6 +61,7 @@
KEYRING_UNITS = {u'saltify': saltify, u'rand': rand, u'keys': None}
""" The Pocket Knife Unit actions.
saltify - HMAC salt functions.
rand - convenience random functions.
keys - (FUTURE/RESERVED)
"""

Expand Down
8 changes: 4 additions & 4 deletions piaplib/keyring/clarify.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@


try:
from ..pku import remediation as remediation
from piaplib.pku import remediation as remediation
except Exception:
import pku.remediation as remediation
import piaplib.pku.remediation as remediation

try:
from remediation import PiAPError as PiAPError
Expand All @@ -63,9 +63,9 @@
raise ImportError("Error Importing PiAPError")

try:
from ..pku import utils as utils
from piaplib.pku import utils as utils
except Exception:
import pku.utils as utils
import piaplib.pku.utils as utils


__prog__ = """piaplib.keyring.clarify"""
Expand Down
9 changes: 5 additions & 4 deletions piaplib/keyring/rand.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@


try:
from ..pku import remediation as remediation
from piaplib.pku import remediation as remediation
except Exception:
try:
import pku.remediation as remediation
import piaplib.pku.remediation as remediation
except Exception:
raise ImportError("Error Importing remediation")


RAND_CHARS = [
str("""a"""), str("""b"""), str("""c"""), str("""d"""), str("""e"""), str("""f"""),
str("""g"""), str("""h"""), str("""i"""), str("""j"""), str("""k"""), str("""l"""),
Expand Down Expand Up @@ -303,9 +304,9 @@ def randChar(count=None):


def ensurePositiveCount(count=None):
"""Ensures the count is greater than zero"""
"""Ensures the given count is an integer with value greater than or equal tozero."""
if count is None or (isinstance(count, int) is False) or count <= 1:
p_count = 1
p_count = int(1)
else:
p_count = int(count)
return p_count
Expand Down
10 changes: 8 additions & 2 deletions piaplib/lint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@
# limitations under the License.
# ......................................................................


try:
import sys
import os
if u'lint' in __file__:
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
try:
if str("lint") in __file__:
__sys_path__ = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
if __sys_path__ not in sys.path:
sys.path.insert(0, __sys_path__)
except Exception:
raise ImportError("Pocket Knife Unit Lint failed to import.")
except Exception as ImportErr:
print(str(type(ImportErr)))
print(str(ImportErr))
Expand Down
21 changes: 11 additions & 10 deletions piaplib/pku/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,20 @@
import sys
import os
if str("pku") in __file__:
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

__sys_path__ = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
if __sys_path__ not in sys.path:
sys.path.insert(0, __sys_path__)
except Exception:
raise ImportError("Pocket Knife Unit PKU failed to import.")


try:
from . import pku as pku
if pku.__name__ is False:
raise ImportError("pku Failed to import.")
except Exception as impErr:
impErr = None
del impErr
import pku as pku
def main(argv=None):
"""The main event"""
import piaplib.pku.__main__
return piaplib.pku.__main__.main(argv)


if __name__ in u'__main__':
main(sys.argv[1:])


Loading

0 comments on commit 8d27ca7

Please sign in to comment.