This repository was archived by the owner on Feb 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathasana2sql.py
More file actions
executable file
·172 lines (135 loc) · 5.03 KB
/
Copy pathasana2sql.py
File metadata and controls
executable file
·172 lines (135 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python
import argparse
import pyodbc
import requests
from asana2sql.fields import default_fields
from asana2sql.project import Project
from asana2sql.workspace import Workspace
from asana2sql.db_wrapper import DatabaseWrapper
from asana import Client, session
def arg_parser():
parser = argparse.ArgumentParser()
# Global options
parser.add_argument(
'--project_id',
type=int,
required=True,
help="Asana project ID.")
parser.add_argument(
'--table_name',
help=("Name of the SQL table to use for tasks."
"If not specified it will be derived from the project name."))
parser.add_argument(
'--dump_perf',
action="store_true",
default=False,
help="Print performance information on completion.")
parser.add_argument("--projects_table_name")
parser.add_argument("--project_memberships_table_name")
parser.add_argument("--users_table_name")
parser.add_argument("--followers_table_name")
parser.add_argument("--custom_fields_table_name")
parser.add_argument("--custom_field_enum_values_table_name")
parser.add_argument("--custom_field_values_table_name")
# Asana Client options
asana_args = parser.add_argument_group('Asana Client Options')
asana_args.add_argument(
"--access_token",
required=True,
help="Asana Personal Access Token for authentication.")
asana_args.add_argument(
"--base_url",
default="https://app.asana.com/api/1.0",
help="URL of the Asana API server.")
asana_args.add_argument(
"--no_verify",
dest="verify",
default=True,
action="store_false",
help="Turn off HTTPS verification.")
asana_args.add_argument(
"--dump_api",
action="store_true",
default=False,
help="Dump API requests to STDOUT")
# DB options
db_args = parser.add_argument_group('Database Options')
db_args.add_argument(
"--odbc_string",
help="ODBC connection string.")
db_args.add_argument(
"--dump_sql",
action="store_true",
default=False,
help="Dump SQL commands to STDOUT.")
db_args.add_argument(
"--dry",
action="store_true",
default=False,
help="Dry run. Do not actually run any writes to the database.")
# Commands
subparsers = parser.add_subparsers(
title="Commands",
dest="command")
create_table_parser = subparsers.add_parser(
'create',
help="Create tables for the project.")
export_parser = subparsers.add_parser(
'export',
help="Export the tasks in the project, "
"not deleting deleted tasks from the database.")
export_parser = subparsers.add_parser(
'synchronize',
help="Syncrhonize the tasks in the project with the database.")
return parser
def build_asana_client(args):
options = {
'session': session.AsanaOAuth2Session(
token={'access_token': args.access_token})}
if args.base_url:
options['base_url'] = args.base_url
if args.verify is not None:
# urllib3.disable_warnings()
options['verify'] = args.verify
if args.dump_api:
options['dump_api'] = args.dump_api
return RequestCountingClient(**options);
class RequestCountingClient(Client):
def __init__(self, dump_api=False, session=None, auth=None, **options):
Client.__init__(self, session=session, auth=auth, **options)
self._dump_api = dump_api
self._num_requests = 0
@property
def num_requests(self):
return self._num_requests
def request(self, method, path, **options):
if self._dump_api:
print("{}: {}".format(method, path))
self._num_requests += 1
return Client.request(self, method, path, **options)
def main():
args = arg_parser().parse_args()
client = build_asana_client(args)
db_client = None
if args.odbc_string:
print("Connecting to database.")
db_client = pyodbc.connect(args.odbc_string)
db_wrapper = DatabaseWrapper(db_client, dump_sql=args.dump_sql, dry=args.dry)
workspace = Workspace(client, db_wrapper, args)
project = Project(
client, db_wrapper, workspace, args, default_fields(workspace))
if args.command == 'create':
project.create_table()
workspace.create_tables()
elif args.command == 'export':
project.export()
elif args.command == 'synchronize':
project.synchronize()
if not args.dry:
db_client.commit()
if args.dump_perf:
print("API Requests: {}".format(client.num_requests))
print("DB Commands: reads = {}, writes = {}, executed = {}".format(
db_wrapper.num_reads, db_wrapper.num_writes, db_wrapper.num_commands_executed))
if __name__ == '__main__':
main()