This repository was archived by the owner on Nov 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy path__init__.py
More file actions
112 lines (97 loc) · 3.59 KB
/
Copy path__init__.py
File metadata and controls
112 lines (97 loc) · 3.59 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
"""Scripts that can be run from the CLI"""
from __future__ import print_function
import logging.config
from pyramid.paster import get_appsettings, bootstrap
from ..lib.sqla import configure_engine, get_session_maker
from ..lib.zmqlib import configure_zmq
from ..lib.model_watcher import configure_model_watcher
from ..lib.config import set_config
from ..indexing.changes import configure_indexing
def get_exported_file_by_func(func, discussion, lang='fr', anon='false', social_columns=True, start=None, end=None, interval=None):
"""
Helper function to get a specific export a file to be downloaded. Downloaded into the S3 bucket.
To be used within a shell environment
"""
import boto3
from datetime import datetime
from pyramid.threadlocal import manager
from assembl.lib.config import get
class FakeContext(object):
def __init__(self, discussion):
self._instance = discussion
class FakeLocalizer(object):
def translate(self, message):
return message
class FakeRequest(object):
authenticated_userid = None
def __init__(self, discussion, lang='fr'):
self.discussion = discussion
self.GET = {}
self.context = FakeContext(discussion)
self.localizer = FakeLocalizer()
self.locale_name = lang
self.matchdict = {'discussion_id': discussion.id}
r = FakeRequest(discussion, lang=lang)
manager.push({'request': r})
r.GET['as_buffer'] = 'true'
r.GET['lang'] = lang
r.GET['anon'] = anon
if not social_columns:
r.GET['no_extra_columns'] = 'true'
if start:
r.GET['start'] = start
if end:
r.GET['end'] = end
if interval:
r.GET['interval'] = interval
try:
output, mime_type = func(r)
finally:
manager.pop()
account_number = get('aws_client', None)
_now = datetime.utcnow()
func_name = func.__name__
name = "%s-%s" % (func_name, _now.isoformat())
def write(o):
with open(name, mode='w') as f:
f.write(o)
if account_number:
try:
s3 = boto3.resource('s3')
bucket = s3.Bucket('assembl-data-%s' % account_number.strip())
bucket.put_object(Body=output, ContentType=mime_type, Key=name)
print("The file was uploaded as %s" % name)
except Exception:
write(output)
print("Failed to upload, creating file locally with name %s" % name)
else:
write(output)
print("The account number could not be found. Saving the file to disk as %s" % name)
def get_multimodule_extracts_file(discussion, lang='fr', anon='false', social_columns=True, start=None, end=None, interval=None):
"""
Helper function to get all of the exports as a file to be downloaded. Downloaded into the S3 bucket.
To be used within a shell environment
"""
from assembl.views.api2.discussion import multi_module_csv_export
get_exported_file_by_func(
multi_module_csv_export,
discussion,
lang=lang,
anon=anon,
social_columns=social_columns,
start=start,
end=end,
interval=interval
)
def boostrap_configuration(config, do_bootstrap=True):
logging.config.fileConfig(config)
settings = get_appsettings(config, 'assembl')
if do_bootstrap:
env = bootstrap(config)
configure_model_watcher(env, 'assembl')
set_config(settings)
configure_zmq(settings['changes_socket'], False)
configure_engine(settings, True)
configure_indexing()
session = get_session_maker()()
return session