Skip to content

Commit 4ea1146

Browse files
committed
Update snippets script to remove them first. Regenerate Storage snippets.
1 parent 9a291ce commit 4ea1146

2 files changed

Lines changed: 77 additions & 13 deletions

File tree

gcloud-java-storage/src/main/java/com/google/cloud/storage/Storage.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1638,6 +1638,14 @@ public static Builder builder() {
16381638
* byte[] content = storage.readAllBytes(blobId);
16391639
* }</pre>
16401640
*
1641+
* @return the blob's content
1642+
* @throws StorageException upon failure
1643+
*/
1644+
byte[] readAllBytes(BlobId blob, BlobSourceOption... options);
1645+
1646+
/**
1647+
* Creates a new empty batch for grouping multiple service calls in one underlying RPC call.
1648+
*
16411649
* <p>Example of using a batch request to delete, update and get a blob.
16421650
* <pre> {@code
16431651
* String bucketName = "my_unique_bucket";
@@ -1661,13 +1669,6 @@ public static Builder builder() {
16611669
* Blob blob = result.get(); // returns get result or throws StorageException
16621670
* }</pre>
16631671
*
1664-
* @return the blob's content
1665-
* @throws StorageException upon failure
1666-
*/
1667-
byte[] readAllBytes(BlobId blob, BlobSourceOption... options);
1668-
1669-
/**
1670-
* Creates a new empty batch for grouping multiple service calls in one underlying RPC call.
16711672
*/
16721673
StorageBatch batch();
16731674

@@ -1788,7 +1789,7 @@ public static Builder builder() {
17881789
* String bucketName = "my_unique_bucket";
17891790
* String blobName = "my_blob_name";
17901791
* String keyPath = "/path/to/key.json";
1791-
* URL signedUrl = storage.signUrl(BlobInfo.builder("my_unique_bucket", "my_blob_name").build(),
1792+
* URL signedUrl = storage.signUrl(BlobInfo.builder(bucketName, blobName).build(),
17921793
* 14, TimeUnit.DAYS, SignUrlOption.signWith(
17931794
* AuthCredentials.createForJson(new FileInputStream(keyPath))));
17941795
* }</pre>

utilities/add_snippets_to_file.py

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,53 @@ def parse_code(cls, index, lines, name):
162162
index += 1
163163
raise ValueError('Snippet {} is missing an END tag'.format(name))
164164

165+
@classmethod
166+
def parse_snippet_javadoc(csl, index, lines):
167+
javadoc = []
168+
while index >= 0:
169+
line = lines[index]
170+
index -= 1
171+
javadoc.append(line)
172+
stripped_line = line.lstrip()
173+
if not stripped_line.startswith('*'):
174+
raise ValueError('Could not parse javadoc snippet:\n{}'.format(javadoc))
175+
if stripped_line.startswith('* <p>'):
176+
if index >= 0:
177+
next_line = lines[index].strip()
178+
if next_line == '*':
179+
javadoc.append(next_line)
180+
return javadoc
181+
raise ValueError('Could not parse javadoc snippet:\n{}'.format(javadoc))
182+
183+
@classmethod
184+
def remove_snippets(csl, signature, lines, line_numbers):
185+
"""Removes javadoc snippets for a method with the provided signature.
186+
187+
This method removes the lines that correspond to javadoc snippets, returns the updated
188+
lines and updates the line_numbers data structure in the process.
189+
190+
A snippet's javadoc should have the following format (as generated by to_javadoc()):
191+
* <p>...
192+
* ...
193+
* ...
194+
* ...}</pre>
195+
"""
196+
index = line_numbers.get(signature)
197+
LOGGER.info('Removing snippets for method %s', signature)
198+
while index >= 0:
199+
line = lines[index].lstrip()
200+
if line.startswith('/**'):
201+
return lines
202+
if line.rstrip().endswith('}</pre>'):
203+
javadoc = Snippet.parse_snippet_javadoc(index, lines);
204+
new_index = index - len(javadoc)
205+
line_numbers.update(index, - len(javadoc))
206+
lines = lines[:new_index + 1] + lines[index + 1:]
207+
index = new_index
208+
else:
209+
index -= 1
210+
raise ValueError('Could not parse javadoc snippets for method {}'.format(signautre))
211+
165212
@classmethod
166213
def parse_snippets(cls, snippets_filename):
167214
"""Parses a file looking for code snippets. Returns a list of Snippet objects.
@@ -240,21 +287,36 @@ def write_snippets(cls, snippets, filename):
240287
class_name = os.path.splitext(os.path.basename(filename))[0]
241288
methods = Method.parse_methods(string, class_name)
242289
line_numbers = LineNumbers(string, methods)
290+
for method in methods:
291+
lines = Snippet.remove_snippets(method.signature, lines, line_numbers)
243292
for snippet in snippets:
244293
target = snippet.target
245294
LOGGER.info('Building snippet for method %s#%s.', class_name, target)
246295
target_line = line_numbers.get(target)
247296
index = target_line - 2
297+
javadoc = snippet.to_javadoc()
248298
while index >= 0:
249299
stripped_line = lines[index].strip()
250-
if (stripped_line == '*'):
300+
if (stripped_line.startswith('/**')):
251301
break
252302
index -= 1
253-
javadoc = snippet.to_javadoc()
303+
previous_line = ''
304+
while index <= len(lines):
305+
stripped_line = lines[index].strip()
306+
if stripped_line == '*/' or stripped_line.startswith('* @'):
307+
if previous_line.strip() != '*':
308+
indent = len(previous_line) - len(previous_line.lstrip())
309+
javadoc = javadoc + [previous_line[:indent] + '*\n']
310+
else:
311+
index -= 1
312+
break
313+
previous_line = lines[index]
314+
index += 1
254315
lines[index:index] = javadoc
255-
line_numbers.insert(target_line, len(javadoc))
316+
line_numbers.update(target_line, len(javadoc))
256317
java_file.seek(0)
257318
java_file.writelines(lines)
319+
java_file.truncate()
258320

259321
class LineNumbers(object):
260322

@@ -274,9 +336,10 @@ def get(self, signature):
274336
"""
275337
return self.line_numbers[signature]
276338

277-
def insert(self, at, count):
339+
def update(self, at, count):
278340
"""
279-
Inserts count lines at the provided position. Line numbers are updated accordingly.
341+
Inserts (if count is positive) or removes (if count is negative) count lines at the
342+
provided position. Line numbers are updated accordingly.
280343
"""
281344
updated_line_numbers = {}
282345
for target in self.line_numbers:

0 commit comments

Comments
 (0)