Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #34 from SafetyCulture/INTG-539-pip_install
Browse files Browse the repository at this point in the history
INTG-539 pip install
  • Loading branch information
Dimitri Koubaroulis authored Sep 8, 2017
2 parents 8ad0af1 + 4979445 commit cbaf224
Show file tree
Hide file tree
Showing 47 changed files with 313 additions and 174 deletions.
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include README.md
include tools/exporter/ReadMe.md
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# safetypy (SafetyCulture Python SDK)

Python SDK for interacting with the SafetyCulture API

Supports [Python 2](https://www.python.org/downloads/mac-osx/).
This SDK is not compatible with Python 3.


## Installation
You will need to have [Python](https://www.python.org/downloads/) and [Pip](https://pip.pypa.io/en/stable/installing/) installed on your computer.

Then run:
```
pip install safetyculture-sdk-python
```

This will install
* SafetyCulture Python SDK
* iAuditor Exporter Tool
* README files

### Basic Usage of the SafetyCulture Python SDK
1. Import `safetypy` into a Python module or Python interpreter:
```
import safetypy
```
2. Create an instance of the SafetyCulture class:
```
sc = safetypy.SafetyCulture(YOUR_IAUDITOR_API_TOKEN)
```
#### For more information regarding the Python SDK functionality
1. To open the Python interpreter, run
```
python
```
2. From the Python interpreter, import the Python SDK by running
```
import safetypy
```
3. For an overview of available functionality, run
```
help(safetypy.SafetyCulture)
```

### Audit Exporter Tool
See [here](https://github.com/SafetyCulture/safetyculture-sdk-python/blob/master/tools/exporter/README.md) for a complete guide on the audit exporter tool.

## License

Copyright 2017 SafetyCulture Pty Ltd

Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0

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.
81 changes: 0 additions & 81 deletions ReadMe.md

This file was deleted.

1 change: 1 addition & 0 deletions safetypy/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .safetypy import *
33 changes: 28 additions & 5 deletions safetypy/safetypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,37 @@
import errno
from datetime import datetime
import requests
from getpass import getpass

DEFAULT_EXPORT_TIMEZONE = 'Etc/UTC'
DEFAULT_EXPORT_FORMAT = 'pdf'
GUID_PATTERN = '[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}$'
HTTP_USER_AGENT_ID = 'safetyculture-python-sdk'

def get_user_api_token(logger):
"""
Generate iAuditor API Token
:param logger: the logger
:return: API Token if authenticated else None
"""
username = raw_input("iAuditor username: ")
password = getpass()
generate_token_url = "https://api.safetyculture.io/auth"
payload = "username=" + username + "&password=" + password + "&grant_type=password"
headers = {
'content-type': "application/x-www-form-urlencoded",
'cache-control': "no-cache",
}
response = requests.request("POST", generate_token_url, data=payload, headers=headers)
if response.status_code == requests.codes.ok:
return response.json()['access_token']
else:
logger.error('An error occurred calling ' + generate_token_url + ': ' + str(response.json()))
return None


class SafetyCulture:
def __init__(self, api_token):

self.current_dir = os.getcwd()
self.log_dir = self.current_dir + '/log/'

Expand All @@ -32,9 +53,11 @@ def __init__(self, api_token):
self.create_directory_if_not_exists(self.log_dir)
self.configure_logging()
logger = logging.getLogger('sp_logger')

token_is_valid = re.match('^[a-f0-9]{64}$', api_token)

try:
token_is_valid = re.match('^[a-f0-9]{64}$', api_token)
except Exception as ex:
self.log_critical_error(ex, 'Error occurred while validating API token in config.yaml file. Exiting.')
exit()
if token_is_valid:
self.api_token = api_token
else:
Expand All @@ -47,7 +70,7 @@ def __init__(self, api_token):
'Authorization': 'Bearer ' + self.api_token
}
else:
logger.error('No valid API token parsed! Exiting!')
logger.error('No valid API token parsed! Exiting.')
sys.exit(1)

def authenticated_request_get(self, url):
Expand Down
26 changes: 26 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from setuptools import setup

setup(name = 'safetyculture-sdk-python',
version = '2.0.2',
description = 'SafetyCulture Python SDK and iAuditor integration tools',
url = 'https://github.com/SafetyCulture/safetyculture-sdk-python',
author = 'SafetyCulture',
author_email = '[email protected]',
include_package_data=True,
packages = ['safetypy', 'tools', 'tools/exporter'],
entry_points = {
'console_scripts': [
'iauditor_exporter = tools.exporter.exporter:main',
],
},
long_description=open('README.md', 'r').read(),
install_requires = [
'python-dateutil>=2.5.0',
'pytz>=2015.7',
'tzlocal>=1.3',
'unicodecsv>=0.14.1',
'requests>=2.10.0',
'pyyaml>=3.11'
],
python_requires = "!=3.*"
)
3 changes: 3 additions & 0 deletions tools/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .exporter import csvExporter
from .exporter import exporter

Loading

0 comments on commit cbaf224

Please sign in to comment.