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

Commit cbaf224

Browse files
author
Dimitri Koubaroulis
authored
Merge pull request #34 from SafetyCulture/INTG-539-pip_install
INTG-539 pip install
2 parents 8ad0af1 + 4979445 commit cbaf224

File tree

47 files changed

+313
-174
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+313
-174
lines changed

MANIFEST.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
include README.md
2+
include tools/exporter/ReadMe.md

README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# safetypy (SafetyCulture Python SDK)
2+
3+
Python SDK for interacting with the SafetyCulture API
4+
5+
Supports [Python 2](https://www.python.org/downloads/mac-osx/).
6+
This SDK is not compatible with Python 3.
7+
8+
9+
## Installation
10+
You will need to have [Python](https://www.python.org/downloads/) and [Pip](https://pip.pypa.io/en/stable/installing/) installed on your computer.
11+
12+
Then run:
13+
```
14+
pip install safetyculture-sdk-python
15+
```
16+
17+
This will install
18+
* SafetyCulture Python SDK
19+
* iAuditor Exporter Tool
20+
* README files
21+
22+
### Basic Usage of the SafetyCulture Python SDK
23+
1. Import `safetypy` into a Python module or Python interpreter:
24+
```
25+
import safetypy
26+
```
27+
2. Create an instance of the SafetyCulture class:
28+
```
29+
sc = safetypy.SafetyCulture(YOUR_IAUDITOR_API_TOKEN)
30+
```
31+
#### For more information regarding the Python SDK functionality
32+
1. To open the Python interpreter, run
33+
```
34+
python
35+
```
36+
2. From the Python interpreter, import the Python SDK by running
37+
```
38+
import safetypy
39+
```
40+
3. For an overview of available functionality, run
41+
```
42+
help(safetypy.SafetyCulture)
43+
```
44+
45+
### Audit Exporter Tool
46+
See [here](https://github.com/SafetyCulture/safetyculture-sdk-python/blob/master/tools/exporter/README.md) for a complete guide on the audit exporter tool.
47+
48+
## License
49+
50+
Copyright 2017 SafetyCulture Pty Ltd
51+
52+
Licensed under the Apache License, Version 2.0 (the "License");
53+
you may not use this file except in compliance with the License.
54+
You may obtain a copy of the License at
55+
56+
http://www.apache.org/licenses/LICENSE-2.0
57+
58+
Unless required by applicable law or agreed to in writing, software
59+
distributed under the License is distributed on an "AS IS" BASIS,
60+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
61+
See the License for the specific language governing permissions and
62+
limitations under the License.

ReadMe.md

Lines changed: 0 additions & 81 deletions
This file was deleted.

safetypy/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .safetypy import *

safetypy/safetypy.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,37 @@
1212
import errno
1313
from datetime import datetime
1414
import requests
15+
from getpass import getpass
1516

1617
DEFAULT_EXPORT_TIMEZONE = 'Etc/UTC'
1718
DEFAULT_EXPORT_FORMAT = 'pdf'
1819
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}$'
1920
HTTP_USER_AGENT_ID = 'safetyculture-python-sdk'
2021

22+
def get_user_api_token(logger):
23+
"""
24+
Generate iAuditor API Token
25+
:param logger: the logger
26+
:return: API Token if authenticated else None
27+
"""
28+
username = raw_input("iAuditor username: ")
29+
password = getpass()
30+
generate_token_url = "https://api.safetyculture.io/auth"
31+
payload = "username=" + username + "&password=" + password + "&grant_type=password"
32+
headers = {
33+
'content-type': "application/x-www-form-urlencoded",
34+
'cache-control': "no-cache",
35+
}
36+
response = requests.request("POST", generate_token_url, data=payload, headers=headers)
37+
if response.status_code == requests.codes.ok:
38+
return response.json()['access_token']
39+
else:
40+
logger.error('An error occurred calling ' + generate_token_url + ': ' + str(response.json()))
41+
return None
42+
2143

2244
class SafetyCulture:
2345
def __init__(self, api_token):
24-
2546
self.current_dir = os.getcwd()
2647
self.log_dir = self.current_dir + '/log/'
2748

@@ -32,9 +53,11 @@ def __init__(self, api_token):
3253
self.create_directory_if_not_exists(self.log_dir)
3354
self.configure_logging()
3455
logger = logging.getLogger('sp_logger')
35-
36-
token_is_valid = re.match('^[a-f0-9]{64}$', api_token)
37-
56+
try:
57+
token_is_valid = re.match('^[a-f0-9]{64}$', api_token)
58+
except Exception as ex:
59+
self.log_critical_error(ex, 'Error occurred while validating API token in config.yaml file. Exiting.')
60+
exit()
3861
if token_is_valid:
3962
self.api_token = api_token
4063
else:
@@ -47,7 +70,7 @@ def __init__(self, api_token):
4770
'Authorization': 'Bearer ' + self.api_token
4871
}
4972
else:
50-
logger.error('No valid API token parsed! Exiting!')
73+
logger.error('No valid API token parsed! Exiting.')
5174
sys.exit(1)
5275

5376
def authenticated_request_get(self, url):

setup.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from setuptools import setup
2+
3+
setup(name = 'safetyculture-sdk-python',
4+
version = '2.0.2',
5+
description = 'SafetyCulture Python SDK and iAuditor integration tools',
6+
url = 'https://github.com/SafetyCulture/safetyculture-sdk-python',
7+
author = 'SafetyCulture',
8+
author_email = 'integrations@safetyculture.io',
9+
include_package_data=True,
10+
packages = ['safetypy', 'tools', 'tools/exporter'],
11+
entry_points = {
12+
'console_scripts': [
13+
'iauditor_exporter = tools.exporter.exporter:main',
14+
],
15+
},
16+
long_description=open('README.md', 'r').read(),
17+
install_requires = [
18+
'python-dateutil>=2.5.0',
19+
'pytz>=2015.7',
20+
'tzlocal>=1.3',
21+
'unicodecsv>=0.14.1',
22+
'requests>=2.10.0',
23+
'pyyaml>=3.11'
24+
],
25+
python_requires = "!=3.*"
26+
)

tools/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .exporter import csvExporter
2+
from .exporter import exporter
3+

0 commit comments

Comments
 (0)